Go Down

Topic: Second digitalRead() of PWM pin always HIGH? (Read 2592 times) previous topic - next topic

WzDD

Hi all,

I've just got my arduino and am very happy with it. I'm not really a hardware guy so it's nice to have something easy-to-use that I can understand! :-)

I've discovered a problem though. It seems that if you do a digitalRead() from a PWM pin (9, 10, or 11) configured as input, the second and subsequent digitalRead()s will always return HIGH, until the signal on the pin actually changes.

That is:

1  h = digitalRead(10); /* h gets correct value */
2  h = digitalRead(10); /* h is HIGH regardless of value */
3  /* Signal changes from LOW to HIGH or vice versa */
4  h = digitalRead(10); /* h once again gets correct value*/

This wouldn't be a problem except that sometimes I get spurious HIGH values in line 1: ie h gets HIGH regardless.

If I move the input to one of the other pins (not PWM), everything works fine.

Has anyone else experienced this problem?

mellis

When digitalRead() mistakenly returns HIGH, is the pin actually connected to a low voltage?  If it's floating (not-connected), you'll get random readings (e.g. all HIGHs).  If so, please post your code.

I ran the following program, and with pin 10 connected directly to ground, the digitalRead() always returned LOW.

Code: [Select]

void setup()
{
 Serial.begin(9600);
 pinMode(10, INPUT);
}

void loop()
{
 Serial.println(digitalRead(10));
 delay(300);
}

WzDD

Thanks for your answer.

I did some more testing and got the same results as you, so I think perhaps I was just silly and had a bug the first time (I'm interfacing with another device and I think my timing was slightly wrong so I actually was reading wrong values). Or the pin could have been floating temporarily.

Either way it works now. Thanks again for the suggestions!

Go Up