I'm doing some tests and I notice a bad things with digitals pins. I have a wire that sends 1 signals. If i connect it to a reading pin, I read 1 - and it's ok. But if i disconnect it, for 5-6 seconds Arduino continues to read 1 instead of 0! How can I stabilize it more quickly?

  • Where is the pin connected to when you disconnect the wire? Probably nothing. You shouldn't have pins floating! – Paul Apr 25 '16 at 11:11

Digital inputs should either be low or high. In this image acceptable input voltages on the left represent common TTL voltages based on a 0 to 5 volt range: enter image description here

It is bad practice to leave a digital input floating. A floating or disconnected input implies the voltage is unknown. I suspect in your case a small amount of input capacitance held a charge for several seconds. Once leaked off the input appeared to change from 1 to 0. Even though you know what might be happening, you should not depend on it as it is unreliable behavior.

If connecting a SPST switch use a pull up (or pull down) resistor to pull the input pin to the opposite voltage the switch is pulling. In this image the switch is connected to ground so the resistor is connected to positive voltage as a pull up:

enter image description here

The fist image comes from this Logic Level tutorial.

  • For Arduino (Atmega328), at 5V an input of above 2.7V will read as 1 and under 2.2V will read as 0. (i.stack.imgur.com/Dqnbp.png) This doesn't really match the values of your source, but my source is the ATmega328 datasheet. (To avoid confusion on the voltages) – Paul Apr 25 '16 at 13:56
  • 1
    ATMega328P inputs are Schmitt Trigger. There is no such thing as an "unknown" voltage level - only a "last valid" voltage level. The voltage has to drop below VIL for it to start registering LOW and then rise above VIH to start registering HIGH. Anything in between retains the previous setting. – Majenko Apr 25 '16 at 15:26

St2000 's reply above gives the background, which is valid for any digital input, not just the Arduino pins.

However, in the specific case of the Arduino, one more thing should be noted: You can skip the use of an external pullup resistor by configuring the pin to use the internal pullup resistor:

pinMode(pin, INPUT_PULLUP) ;

This assumes, of course, that it is easy to modify the part of your circuit that is connected to your input pin to have the following two states:

- ground
- high impedance (floating)

If, for example, it is a switch you are reading, just connect one pin of the switch to your arduino pin and the other to GND.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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