Arduino, How to know the state of an output pin?

When your program set state on an OUTPUT pin, it can be sometimes useful to know the state of this OUTPUT for some reasons (sending the output state over internet, etc…)


So here is the trick to know the output state of a pin:

//exemple for the state of the pin 3

But why?

Actually the micro-controller keeps in some register the value set for output pin.
PORTB and PORTD are registers for digital pin from 8 to 13 and digital pin from 0 to 7.

A list of interesting register

  • PORTD maps to Arduino digital pins 0 to 7
DDRD – The Port D Data Direction Register – read/write
PORTD – The Port D Data Register – read/write
PIND – The Port D Input Pins Register – read only
  • PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable
DDRB – The Port B Data Direction Register – read/write
PORTB – The Port B Data Register – read/write
PINB – The Port B Input Pins Register – read only
  • PORTC maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on the Arduino Mini
DDRC – The Port C Data Direction Register – read/write
PORTC – The Port C Data Register – read/write
PINC – The Port C Input Pins Register – read only


Leave a Comment.