digitalRead() Problem with Due

Hello all,
I just got a new Due board and was trying out some simple programs and ran into an odd issue. I'm somewhat new to Arduino, but I've done a lot of work with PICs and general C programming, so maybe I'm just approaching this the wrong way.

I am trying to read the state of a digital pin (specifically the LED pin) when it's in OUTPUT mode, however, it always returns zero. See the code below for an example:

#define LED_PIN 13

unsigned long led_counter = 0;

void setup()
{
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(19200); 
  while (!Serial);
}

void loop()
{
  
  if(led_counter%2 == 0)
  {
    digitalWrite(LED_PIN,HIGH);
  }
  else
  {
    digitalWrite(LED_PIN,LOW);
  }
  Serial.print(digitalRead(LED_PIN));
  
  delay(1000);
  led_counter++;
}

The serial monitor just shows a string of "0"s, when it should return alternating "0" and "1". So no matter what the actual output of the pin is digitalRead() seems to return LOW. The LED is actually blinking, so I know the pin state is changing.

After googling around it seems doing a digitalRead() of an output pin should theoretically work. However, I didn't find any cases using a Due, so I wondered if maybe that was the issue. Maybe the different I/O mapping in the ARM core has something to do with it?

Any thoughts or suggestions would be much appreciated! Thanks guys!
Liam

P.S. Just to preempt the question: "Why would you want to read an output value you just set?". The I/O register is already storing the value of the pin, so I should just be able to do something like this:

digitalWrite(~digitalRead(LED_PIN));

Also, it just makes sense that digitalRead() should return the value of the pin regardless of the tristate.

This question has been asked recently (twice) but not specifically about the Due

I took a look at that thread and it doesn't seem like they came to a conclusion on whether or not digitalRead() works for a pin in OUTPUT mode. The thread is a year old, so I'm not sure it's worth starting it up again (especially because that situation is a bit different).

As I said, after googling around it seems that this works on other boards and so should theoretically work on the Due; however, I'm not seeing that. Maybe I'm doing something wrong or maybe the Due has a different I/O mapping which is not compatible. If anyone has gotten this to work and can give me some example code I would really appreciate it.

Thanks again!
Liam

Why make things difficult for yourself ? Save the pin state in a variable when you do the digitalWrite(). Job done, no hassle.

I know there are work-arounds, but I'm just trying to understand why this doesn't work. Also, if it really is a bug it should be fixed (or at least noted in the documentation), not glossed over.
Liam

This is a bug. See post Arduino Due and IDE 1.5.3 digitalRead() BUG - Suggestions for the Arduino Project - Arduino Forum for a workaround specific to the Due.

Hey there, i have a DUE running with compiler 1.6.4, and it has the same issues. its 2015... :smiley: