12
\$\begingroup\$

I have set up my Arduino UNO with 4 input wires, they connect to the port 10, 11, 12, 13, they are connected to my breadboard with nothing on it but the wire.

My code would simply be:

Serial.println(digitalRead(13));
Serial.println(digitalRead(12));
Serial.println(digitalRead(11));
Serial.println(digitalRead(10));

The thing is, after I briefly connect a pin to 5V or 3V3, the reading would start going nuts like: 1 0 0 1 0 1 1 1 0 1 0 1 1 0 Without any sense! Without having any wire connected directly to my VCC (5V or 3V3) the reading is constantly changing, when it should be zero!

Is my Arduino dead? How can I fix this?

\$\endgroup\$
1
  • \$\begingroup\$ Just connect your triggering pin to ground when not triggered ! \$\endgroup\$
    – user99014
    Commented Feb 1, 2016 at 23:46

2 Answers 2

21
\$\begingroup\$

What you have is called a Floating pin. Digital Input pins are very sensitive to change, and unless positively driven to one state or another (High or Low), will pick up stray capacitance from nearby sources, like breadboards, human fingers, or even the air. Any wire connected to it will act like a little antenna and cause the input state to change. And I mean any wire, the trace on the board, the wire to the breadboard, the breadboard pin, even the metal pin of the IC itself.

This is refereed to in the Arduino reference page:

If the pin isn't connected to anything, digitalRead() can return either HIGH or LOW (and this can change randomly).

If you look at the Arduino Digital Pin Tutorial:

This also means however, that input pins with nothing connected to them, or with wires connected to them that are not connected to other circuits, will report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin.

Often it is useful to steer an input pin to a known state if no input is present. This can be done by adding a pullup resistor (to +5V), or a pulldown resistor (resistor to ground) on the input, with 10K being a common value. There are also convenient 20K pullup resistors built into the Atmega chip that can be accessed from software. These built-in pullup resistors are accessed in the following manner.

pinMode(pin, INPUT);           // set pin to input
digitalWrite(pin, HIGH);       // turn on pullup resistors

These weak pull resistors force the input pin state into a known state, and are easily overwritten by stronger input voltages, like a direct connection to ground or +5v.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ How about mentioning pinMode(pin, INPUT_PULLUP)? It's easy to remember, and really hard to screw up. \$\endgroup\$
    – piojo
    Commented Apr 25, 2018 at 14:44
6
\$\begingroup\$

This is normal. Unless an input is connected to something, it will show HIGH or LOW or constantly changing. This is described by the digitalRead reference page on the Arduino web site.

The input pin behaves with some small capacitance, so briefly connecting to 3V3 or 5V will charge the capacitance. You may find a similar effect using a bare finger.

You can avoid the problem by adding a large value resistor between the pin and GND.

Ask yourself why you expect zero?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ pinMode(pin, INPUT_PULLUP ); \$\endgroup\$
    – jippie
    Commented Sep 23, 2013 at 5:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.