Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I digitalRead a pin that is in pinMode OUTPUT?

Tags:

arduino

I have a very simple test sketch in which I'm trying to set a pin to HIGH and then read its state with digitalRead. Here is my sketch.

void setup() {     Serial.begin(9600); }  void loop() {     delay(1000);      pinMode(3, OUTPUT);     digitalWrite(3, HIGH);     delay(1000);      pinMode(3, INPUT);     Serial.println(digitalRead(3)); } 

Serial monitor result:

0 0 0 0 

I have come to understand that changing the pinMode will stop it from being HIGH. So setting a pin to HIGH in OUTPUT mode and then changing to INPUT mode will change it to LOW. So the digitalRead will always return 0. If I don't change the pinMode it won't be able to read the pin. So how can I read the current setting of a pin that is in OUTPUT mode without losing the value?

like image 655
Bazzz Avatar asked May 28 '11 10:05

Bazzz


People also ask

Can you digitalRead an output pin?

Way faster then doing a digitalRead and then a digitalWrite. Yes, reading an output pin returns the state of the pin (the last thing that was written to the pin).

Can you digitalRead an output pin Arduino?

You can safely drive an led without calling pinmode(output), but It isn't recommended for something like a relay). Looking at the code the digitalRead() function just does a read of the appropriate bit in the appropriate PINx register without modifying anything, so it should work.

Can we set pin to input through pinMode () method?

The pinMode() function is used to configure a specific pin to behave either as an input or an output. It is possible to enable the internal pull-up resistors with the mode INPUT_PULLUP.

What does pinMode output mean?

pinMode. pinMode() configures the specified pin to behave either as an input (with or without an internal weak pull-up or pull-down resistor), or an output.


2 Answers

In this case you just want to access the data register itself.

PORTB and PORTD registers contain the pin data you are looking for. I finally got access to an Arduino to figure it out. You want to use bitRead(PORTD, pin).

Serial.println(bitRead(PORTD,3)); //Reads bit 3 of register PORTD which contains the current state (high/low) of pin 3. 

Reference Bit Read Operation for more information.

like image 191
MikeRags Avatar answered Sep 20 '22 16:09

MikeRags


Your sketch should be

void setup() {     Serial.begin(9600); }  void loop() {     delay(1000);      pinMode(3, OUTPUT);     digitalWrite(3, HIGH);     delay(1000);      // pinMode(3, INPUT); // get rid of this line     Serial.println(digitalRead(3)); } 

That's all. Then it reads the pin's state which in your case is "HIGH". If you set the pinMode to input it will read the input depending on what is connected. If you are writing "HIGH" to an input pin the internal pullup will be activated. It does not matter if you write HIGH before setting it to input mode or after setting it to input mode. Unless of course you are driving a load that is to high for the output pin (e.g. a switch to ground). Then this would probably kill the pin.

If you have written a low and set the pin to low it might float which may lead to any kind of unpredictable behaviour.

like image 36
Udo Klein Avatar answered Sep 19 '22 16:09

Udo Klein