Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I am trying to read the value of an output pin. online forums say that digitalRead(pinNum); should work, but that is not the case here. digitalRead always returns 0 (LOW). This is the code:

int pin = 22; // or 13, or 3 ...
void setup()
{
  Serial.begin(9600);
  pinMode(pin,OUTPUT);
}
void loop()
{
  digitalWrite(pin,HIGH);
  delay(100);  
  Serial.println(digitalRead(pin));
  delay(100);
  digitalWrite(pin,LOW);      
  delay(100);
  Serial.println(digitalRead(pin));
  delay(100);
}

printed values:

0
0
0
...
share|improve this question

1 Answer 1

up vote 3 down vote accepted

That method only works on AVR based systems. It exploits a "feature" whereby the IO pin, when in OUTPUT mode, is also in INPUT mode at the same time, and reading the pin reads the value that the pin is being driven to by the digitalWrite function.

The same is not true of the Due, which is an ARM based system. The IO pins function completely differently and the same "feature" cannot be used.

You will have to remember the state of the pin yourself.

share|improve this answer
1  
I cannot upvote yet, but thank you! –  Makan Tayebi Jul 20 at 14:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.