Go Down

Topic: digitalRead not working correctly (Read 1 time) previous topic - next topic

NovaDesu

Hey guys,

After a few months not using arduino I decided to give it a try again, but it seems to be bugged now.

the digital read appears to be random. it changes from high to low whenever it contects with a wire, which is not connected to an energy source. I debugged it with a serialconnection and it indeed showed a random stream of 0 and 1's while only plain wire was connected. I have used different digital inputs but the bug remained.

Anyone knows what might be wrong?

Thanks in advance,

Nova

retrolefty

Before a digital input pin can be read reliably it must have either it's internal pull-up enabled, or an external pull up or pull down resistor wired up. If the digital input is wired to an external signal source that can actively sink or source current then no pull-up or down is required. The point is that a digital input pin not wired to anything is not seeing a valid logic level voltage, neither a high or low and should not be attempted to be read.

That make sense?

Lefty

NovaDesu

Yes, I used a 1M ohm resistor between the button and the input.

this is some of the code I used

Code: [Select]

global scope:
const int rotateInput = 31;
int b11;

setup
pinMode(rotateInput, INPUT);


in the loop
Code: [Select]

b11 = digitalRead(rotateInput);
if(b11 == HIGH){
  dostuff;
}else{
 other stuff;
}


Now when I checked, using a serial connection, what value b11 would get while I didn't press the button, it would be something like: 101010110101010101010101010100110. If I actually pressed the button it would be all 1111's.

I also tried it with the example button code, same story...

What did I do wrong?

retrolefty

#3
Aug 29, 2010, 02:28 am Last Edit: Aug 29, 2010, 02:29 am by retrolefty Reason: 1
Quote
Yes, I used a 1M ohm resistor between the button and the input.


That is [glow]not[/glow] the same as having a pull-up or pull-down installed (not sure what you wired to the otherside of the button switch, +5 or ground?). It sounds like you need a pull-down, so wire a 10k ohm resistor from the input pin to ground.

Lefty

Go Up