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?
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:
The fist image comes from this Logic Level tutorial.
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.