Will digitalRead return the state of an Output pin ?

At the moment, I have a number of variables in my code that keep track of the high / low state of each output pin.

Not wanting to damage the board, can anyone confirm if using digitalRead(pin no) would return the state of an output pin ?

example, if pin 12 is an Output pin.

if(digitalRead(12) == 1){
Serial.println("OUTPUT Pin 12 is HIGH");
}

Hi,
Its fine to try, Arduino is designed to a degree to stop you from unintended consequences. If it doesn't work I have two other ideas.

Duane B

rcarduino.blogspot.com

A read of a port should reflect the pin values regardless of the DDR (data direction) setting.

Exactly what the digitalRead() func does I'm not sure but I think it should work and certainly shouldn't do any harm.

That said keeping a variable to mirror the port value is the normal way to go.


Rob

Hi,

I would even consider turning it upside down and having the pins reflect the variables.

Basically this -

Loop -

  1. Gather inputs
  2. Process inputs to calculate outputs
  3. Set the outputs
  4. Repeat

If some of your inputs happen to be outputs set in 4) its ok to gather them back in 1) its a little bit less efficient, but also a little bit more maintainable as the only state you will ever have for your outputs is the state that they are in rather than your best efforts are tracking their state using variables - we all make mistakes, this approach avoids a lot of potential mistakes.

Just a suggestion

Duane B

rcarduino.blogspot.com

Yes.
I often toggle an output by

digitalWrite(pin, !digitalRead(pin) );

Many Thanks to all for the replies.