Digital In too sensitive

I just bought an Arduino Uno and was experimenting with the DigitalReadSerial sample program. I connected a 2 inch wire to pin 2 and I get an unpredictable result. It was almost sensitive enough to be used as a motion detector as the serial readout would change a lot when I move my hand near the wire, and not so much when I kept my hand away from it.

It seems to me that this is way too sensitive to be functional, as I was expecting to have to connect as much as +5v to make the pin read High. Am I wrong? It there a problem with my Arduino Uno? If not what am I doing wrong?

BTW: The wire was only connected to pin 9, the other end was not connected to any thing.

It seems to me that this is way too sensitive to be functional, as I was expecting to have to connect as much as +5v to make the pin read High. Am I wrong? It there a problem with my Arduino Uno? If not what am I doing wrong?

What you are seeing is the result of reading a ‘floating input pin’, which just returns random noise as you touch it. I/O pins must be wired to valid voltage sources to return valid values. That applies to both digital input pins and analog input pins. Wiring an ‘antenna’ to a input pin does not work for anything but amusement purposes.

Lefty

What you are seeing is the result of reading a 'floating input pin', which just returns random noise as you touch it. I/O pins must be wired to valid voltage sources to return valid values. That applies to both digital input pins and analog input pins. Wiring an 'antenna' to a input pin does not work for anything but amusement purposes.

Lefty

So what you're saying is that the pin needs to be either grounded or connected to +5v or the serial output will be all goofy?

Correct. Or you could with software command, enable the internal pull-up resistor for the pin, that will in effect be wiring +5vdc through a high value resistor so that the pin will read a steady high even with nothing wired to the pin.

Lefty

So what you're saying is that the pin needs to be either grounded or connected to +5v or the serial output will be all goofy?

No, the input needs to be connected to ground or +5V, or the [u]input[/u] will be "all goofy". The serial output will print whatever you tell it to print, sans goofiness.

ITman: It was almost sensitive enough to be used as a motion detector as the serial readout would change a lot when I move my hand near the wire, and not so much when I kept my hand away from it.

This is a video I made to help illustrate what is happening to the pin when it is floating. The oscilloscope is attached to a pin that is floating. Instead of wires I am using a pushbutton. I never actually push the push button.

As AWOL points out, digitalRead reports whatever state the pin is in. The human body does a great job of coupling near-by electrical noise into a floating pin: http://www.youtube.com/watch?feature=player_embedded&v=dBIBFLYCjMM

digitalWrite(9, HIGH);

will the "problem". This enables the built-in pull-up resistor.

Which then looks like this: http://www.youtube.com/watch?feature=player_embedded&v=SAIw7LLVl-U#t=20s