Problem with digitalRead()

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:

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:

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

You've got a floating input.

Use a pull up or pull down resistor.

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

edit: as AWOL said

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!!!

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.

Hi AWOL

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

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

LarryD:
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?

sergioclick:
Hi AWOL

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

What is the other end of the resistor connected to ?

Thank you very much to all, I resolve my issue with information of this link Inputs

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