Go Down

### Topic: Problem with digitalRead() (Read 1 time)previous topic - next topic

#### sergioclick

##### Feb 10, 2013, 08:54 pmLast Edit: Feb 10, 2013, 09:00 pm by AWOL Reason: 1
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: [Select]
`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: [Select]
`x: 1x: 1x: 0x: 0x: 0x: 0x: 0x: 0x: 1x: 1x: 1x: 1x: 1x: 1x: 1x: 0x: 0x: 0x: 0x: 0x: 0x: 0x: 1x: 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.

Moderator edit: topic subject corrected

#### AWOL

#1
##### Feb 10, 2013, 08:56 pmLast Edit: Feb 10, 2013, 09:00 pm by AWOL Reason: 1

You've got a floating input.

Use a pull up or pull down resistor.
"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.
I speak for myself, not Arduino.

#### larryd

#2
##### Feb 10, 2013, 08:58 pm
Add a resistor (about 10K) from pin 7 to Arduino +5 volts.

edit: as AWOL said
No technical PMs.
The last thing you did is where you should start looking.

#### sergioclick

#3
##### Feb 10, 2013, 09:33 pm
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!!!

#### AWOL

#4
##### Feb 10, 2013, 09:36 pm
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.
"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.
I speak for myself, not Arduino.

#### sergioclick

#5
##### Feb 10, 2013, 10:00 pm
Hi AWOL

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

#### Grumpy_Mike

#6
##### Feb 10, 2013, 10:02 pmLast Edit: Feb 10, 2013, 10:05 pm by Grumpy_Mike Reason: 1
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

#### MarkT

#7
##### Feb 10, 2013, 10:10 pm

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?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### UKHeliBob

#8
##### Feb 10, 2013, 10:16 pm

Hi AWOL

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

What is the other end of the resistor connected to ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### sergioclick

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

#### UKHeliBob

#10
##### Feb 11, 2013, 08:30 am
For the benefit of others that might have the same problem what did you do to resolve your issue ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up