Reading Actual Pin State on Outputs

It appears to me if you do a digitalRead on a pin configured as an output, that it returns something other than the actual external pin state. Is there a way to read the actual external pin state?

Regards, Ray L.

digitalRead() should return a value reflecting the state of the pin...

It just reads the PINx register and selects the appropriate bit, and per datasheet:

14.2.4 Reading the Pin Value Independent of the setting of Data Direction bit DDxn, the port pin can be read through the PINxn Register bit.

How could the external pin state ever not match the state you set, if the pin is an output, unless you've got an output connected to something lower impedance than the uC pin driver, which would exceed the specs of the chip.

DrAzzy: How could the external pin state ever not match the state you set, if the pin is an output, unless you've got an output connected to something lower impedance than the uC pin driver, which would exceed the specs of the chip.

Things don't always work as planned. This is for system validation and functional testing.

Regards, Ray L.

Well, if it is something like a test socket for the IC, you could connect a small cap to the output, set it to output, write a value, set it to input, read it back, flip the bit and repeat. You could even test the pullups by timing it. But not in a circuit.

RayLivingston: It appears to me if you do a digitalRead on a pin configured as an output, that it returns something other than the actual external pin state. Is there a way to read the actual external pin state?

If you write HIGH to an output pin, and expect to read back LOW, it sounds like you are damaging something.

It appears to me if you do a digitalRead on a pin configured as an output, that it returns something other than the actual external pin state. Is there a way to read the actual external pin state?

If you're referring to the Due, then take a look at pio.c located in C:\Program Files\Arduino\hardware\arduino\sam\system\libsam\source. Functions PIO_Get() or PIO_GetOutputDataStatus() may provide what you need.

 * \brief Returns 1 if one or more PIO of the given Pin instance currently have
 * a high level; otherwise returns 0. This method returns the actual value that
 * is being read on the pin. To return the supposed output value of a pin, use
 * PIO_GetOutputDataStatus() instead.
 *
 * \param pinĀ  Pointer to a Pin instance describing one or more pins.
 *
 * \return 1 if the Pin instance contains at least one PIO that currently has
 * a high level; otherwise 0.

"Independent of the setting of Data Direction bit DDxn, the port pin can be read through the PINxn Register bit." - Atmel datasheet.