Go Down

Topic: digitalRead() Problem with Due (Read 2053 times) previous topic - next topic

controlsfreq

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:

Code: [Select]

#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:
Code: [Select]

digitalWrite(~digitalRead(LED_PIN));

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

UKHeliBob

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

http://arduino.cc/forum/index.php?topic=102716.0
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

controlsfreq

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

UKHeliBob

Why make things difficult for yourself ?  Save the pin state in a variable when you do the digitalWrite().  Job done, no hassle.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

controlsfreq

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

m3741


ldjohn

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

Go Up