How to find out output pin state?

Hi,

is there any way to check output pin state, e.g., to find out is it in HIGH or LOW state? I mean something like:

int outPin = 11;
pinMode(outPin, OUTPUT);
digitalWrite(outPin, state);
...
// don't have access to original state value, find out if the
// outPin is LOW or HIGH
state = ...; // ????

Try this...

#include <wprogram.h>
#include <wiring_private.h>
#include <pins_arduino.h>

int digitalReadOutputPin(uint8_t pin)
{
  uint8_t bit = digitalPinToBitMask(pin);
  uint8_t port = digitalPinToPort(pin);
  if (port == NOT_A_PIN) 
    return LOW;

  return (*portOutputRegister(port) & bit) ? HIGH : LOW;
}

void loop()
{
  ...
  int outPin = 11;
  int state = ??;
  pinMode(outPin, OUTPUT);
  digitalWrite(outPin, state);
  
  state = digitalReadOutputPin(outPin);
  ...
}

Mikal

Thanks! I'll try that.

Just reading the output pin works for me.

int state;

pinMode(13, OUTPUT);
digitalWrite(13, LOW);

....

state = digitalRead(13);

in fact I use the following frequently to toggle a pin:

digitalWrite(pinnum, !digitalRead(pinnum));

4 Likes