digitalRead not working correctly

Hey guys,

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,

PixelMaster

There's nothing wrong, your pin is floating.

whenever it contects with a wire, which is not connected to an energy source.

That's what we call an antenna.

Yes this happens, it is called a floating input and it seems to surprise beginners.
You must connect a digital input to something and not leave it unconnected. Look at pull up resistors.

AWOL:
There's nothing wrong, your pin is floating.

Whats that?

It isn't part of a complete circuit.

PixelMaster:
Hey guys,

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,

PixelMaster

As several have stated you are reading a 'floating' input. To put it simple, I would ask you what would be your expections on reading a input pin that has nothing wired to it (or even a wire which is not wired to anything but the input pin)? If you said it should read LOW that would be in error. The correct answer is that a input pin that is floating is not a legally defined input state and will randomly read either high or low as circuit noise determines. The input pin must be electrically be wired to a voltage source that is either 0vdc (low) or +5vdc (high) to operate correctly.

Lefty

Anyone knows what might be wrong?

Nothing is wrong, read this:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

So if I connect the the pin to a ground it will work? (just making sure I have that correct)

PixelMaster:
So if I connect the the pin to a ground it will work? (just making sure I have that correct)

http://www.cmiyc.com/tutorials/arduino-pull-ups/

Videos of what your pin is doing, along with an explanation of how to use the internal pull-ups to keep the pin from floating.

Thanks for the Help :slight_smile: I fixed it