Pages: [1]   Go Down
Author Topic: digitalRead not working correctly  (Read 1122 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16497
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

this is some of the code I used

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

setup
pinMode(rotateInput, INPUT);

in the loop
Code:
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?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16497
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: August 28, 2010, 07:29:32 pm by retrolefty » Logged

Pages: [1]   Go Up
Jump to: