I/O input reading random

Hi,
I have strange behavior of digital input. Using serial monitor and digitalread on any digital input of my arduino uno rev3, it happens this:

  • if no wire is connected into the digital input then read is 0 (perfect)
  • if a wire is connected with one side but the other side is not then I read random 0 and 1 .....
  • if a wire is connected with one side and the other side is connected in a breadboard with any others connections then again random 0 and 1.....
    So basically it reads random 0 and 1 when there is a wire in the I/O pin of arduino. How can I expect to read the correct digital value if I connect the wire into a circuit for my experiments? Pls tell me where I wrong

did you specify pinMode(pin, INPUT);

Yes,
program is very simple, I just started to use Arduino and I'm still testing basical functions

void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
}

void loop() {
int sensorValue = digitalRead(2);
Serial.println(sensorValue, DEC);
}

:frowning:

Well I know the answer and have explained it many times to begineers. However this time I would like first to pose a question to you. What value, HIGH or LOW, would you expect a digital input pin to read with nothing connected to it and why do you think that? If after you think about the question and respond I will explain the situation and how to deal with it.

Lefty

Well,
if wire is not connected and it's free then is possible to see interferences captured so it could read random value.... what i dont aspect is reading of random value if wire is plugged into a breadbord, the electrical path in it is surrounded by plastic so it shoudnt capture anything :frowning:

I get it wrong?

AndreBologna:
Well,
if wire is not connected and it's free then is possible to see interferences captured so it could read random value.... what i dont aspect is reading of random value if wire is plugged into a breadbord, the electrical path in it is surrounded by plastic so it shoudnt capture anything :frowning:

I get it wrong?

The fact that you have a wire connected to a breadboad doesn't change that there is no valid electrical voltage being applied to the input pin just noise. An input pin is a voltage sensing device that needs a voltage applied to it via an external circuit, the value of the voltage will determine if the pin will read it as a HIGH or a LOW logic level. So with no complete circuit attached the pin is said to be in a "floating input pin condition" and will just respond to random noise as to if it returns a HIGH or LOW when you perform a digitalRead() command. So you have to always have a valid voltage applied to a input pin for the pin to return valid values, high or low, when reading the pin. This can be done several ways. There is a way to enable an internal pull-up resistor for the pin using software command that will cause the pin to always read HIGH even with nothing connected to the pin. You can also wire external resistor (say 10k ohms) to the pin either to ground or to +5vdc voltage so that it will always return a LOW or HIGH respectivly.

So in passing, the input of a digital input pin is where the electrical world meets the software world and in the electrical world one has to insure proper circuitry wiring and proper applied voltages. Also when using digital output pins one has the responsiblity that the external electrical circuitry does not try and draw more current from the output pin then the pin can safely supply without damage.

So does that help? If you have specific questions about this I will gladly try and explain further.

Lefty

"if wire is plugged into a breadbord, the electrical path in it is surrounded by plastic"
Mechanically that may be true, electrically the wire is still connected to the microcontoller at one end, and nothing to the other end.
You want to make sure the wire has valid voltage on it. One way is to use the internal pullup resistors that retrolefty brings up. There are no internal pulldown resistors.

void setup() {  
Serial.begin(9600);  
pinMode(2, INPUT);
digitalWrite (2, HIGH); // connects internal pullup resistor - digitalRead of 2 returns HIGH, or 1, unless pin is pulled low, then it returns LOW, or 0.

// if using IDE 1.0.1, this does the same
pinMode(2, INPUT_PULLUP);

So basically on the wire must have applied always an electrical signal in order to have a proper read of digital input, is that right?

@CrossRoads what i meant here "if wire is plugged into a breadbord, the electrical path in it is surrounded by plastic" was that one end connected to arduino, the other end plugged in a breadboard with no electrical circuits on it

Yes, electrical signal is needed for a proper reading.

I understood the 2nd part - there is no external part supplying a voltage - so you rely on the internal pullup to supply that voltage.

Well ty to both of you, very helpful on my newbie problems :slight_smile:
Since I'm new I have already another problem :smiley: so if you want to check and help me again I would appreciate ..... here's the link http://arduino.cc/forum/index.php/topic,125164.0.html

Ty

ps @CrossRoads I have tried to pullup and it works very good