9
\$\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\$
  • \$\begingroup\$ Just connect your triggering pin to ground when not triggered ! \$\endgroup\$ – user99014 Feb 1 '16 at 23:46
17
\$\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\$
  • 2
    \$\begingroup\$ How about mentioning pinMode(pin, INPUT_PULLUP)? It's easy to remember, and really hard to screw up. \$\endgroup\$ – piojo Apr 25 '18 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
    \$\begingroup\$ pinMode(pin, INPUT_PULLUP ); \$\endgroup\$ – jippie Sep 23 '13 at 5:31

protected by Community May 5 '18 at 13:53

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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