Pages: [1]   Go Down
Author Topic: Problem with digitalRead()  (Read 463 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everybody,

I'm trying to make very simple exercise with my Arduino UNO. Basically, I'm trying to read the pulsation on button but I've run into an error I do not understand.

I'm trying to explain...

My Design is like this:

3.5 v output ------ cable ------ button ------ cable -------- PIN 7

And my code is like this:

Code:
void setup()
{
pinMode(7, INPUT);
}

void loop()
{
delay(100);
int x = digitalRead(7);
Serial.print(x);

}

If I don't push the button, I will receive "0", however, if I push the button I will receive "1".... yes?

But... without touching anything... the "Monitor serial" always shows me alternatively 0 and 1... 6 or 7 times each... like this example:

Code:
x: 1
x: 1
x: 0
x: 0
x: 0
x: 0
x: 0
x: 0
x: 1
x: 1
x: 1
x: 1
x: 1
x: 1
x: 1
x: 0
x: 0
x: 0
x: 0
x: 0
x: 0
x: 0
x: 1
x: 1
...

If I touch the button, the "x" value, evidenty, force change to "1"... but the other times... What happens???

I do not understand why this problem happens.

Thanks in advance for your comments.

Moderator edit: topic subject corrected
« Last Edit: February 10, 2013, 03:00:12 pm by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25820
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You've got a floating input.

Use a pull up or pull down resistor.
« Last Edit: February 10, 2013, 03:00:36 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 64
Posts: 2488
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Add a resistor (about 10K) from pin 7 to Arduino +5 volts.

edit: as AWOL said
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your answers, but I think I'm not explaining very well..

Imagine my board without anything... in this case, digitalRead() always return me "0", but when I connect one piece of cable (only the cable) to PIN 7 (remember, is INPUT) my Arduino returns me alternatively "0" and "1" (even without closing the circuit)

In this way...

if PIN 7 still do not receive voltage.. Why do I need the "resistance"?

if PIN 7 will be returns 1 or 0 (representing HIGH or LOW), Why do I need float variable?

I'm confused..

Thanks a lot!!!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25820
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you think that having nothing connected to a pin should give a consistent result?
The resistor gives a consistent high or low, depending on which rail it is connected to.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi AWOL

I connected resistor to PIN 7 but the problems don't disappeared..
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33387
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Thanks for your answers, but I think I'm not explaining very well..
Yes you are perfectly clear, as are the answers you got.
The connection of a cable makes it more likely to pick up noise. If you have a UNO then that works off 5V and so trying to switch with 3V5 is marginal at best.
Don't wire it up like that.

Where else is that resistor going?

Read this for why you are seeing what you do:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html
« Last Edit: February 10, 2013, 04:05:46 pm by Grumpy_Mike » Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11718
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Add a resistor (about 10K) from pin 7 to Arduino +5 volts.

edit: as AWOL said

Since the button is connected to +3.5V that won't work - use a pull-down to ground.  3.5V will consistently
read as HIGH when the button is pressed.   You need the resistor to pull the pin back down to 0V when the
button is released.

Unconnected inputs on CMOS chips are like radio antennas, they pick up any nearby signal very easily
since they are almost perfect voltage sensors with infinite resistance (well thousands of megohms or more).

Why 3.5V?  Perhaps you mean 3.3?
Logged

[ I won't respond to messages, use the forum please ]

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4079
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi AWOL

I connected resistor to PIN 7 but the problems don't disappeared..
What is the other end of the resistor connected to ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much to all, I resolve my issue with information of this link http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4079
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For the benefit of others that might have the same problem what did you do to resolve your issue ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: