user84978: I am simulating a circuit with arduino, which determine if there is current at input, then turn the led on at the output. The problem is the light is always on even when the button is open at the input.

This is my code:
void setup() {
  pinMode(7, OUTPUT);
  pinMode(8, INPUT);
  digitalWrite(7, LOW);
}

void loop() {
  int on = digitalRead(8);
  digitalWrite(7, on);
}

Why does this happen and how do I solve it?
Any help would be appreciated.

user4143: Microcontroller inputs don't detect the presence of current. They compare a voltage to a threshold.
In reality the input pin is closest to a capacitor. You press your button and it charges that capacitor to above the threshold voltage for "HIGH". Then you release the button but that capacitor remains charged and still reading "HIGH". You need something to reduce the charge on that capacitor to below the "LOW" threshold voltage.
This is most normally done with a resistor between the GPIO pin and GND which will "bleed off" that charge and give you a LOW reading when the button is released.
This is a "pull down" resistor (you can also reverse it and have a "pull up" resistor and the button connected to GND) and is an essential part of any input that doesn't itself generate both the HIGH and LOW voltages required (any switch or button).
Also 10V on a GPIO pin will destroy your Arduino.

84978: <p>I am simulating a circuit with arduino, which determine if there is current at input, then turn the led on at the output. The problem is the light is always on even when the button is open at the input.</p>
<p><a href="https://i.stack.imgur.com/B6n2x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B6n2x.png" alt="enter image description here" /></a></p>
<p>This is my code:</p>
<pre><code>void setup() {
  pinMode(7, OUTPUT);
  pinMode(8, INPUT);
  digitalWrite(7, LOW);
}

void loop() {
  int on = digitalRead(8);
  digitalWrite(7, on);
}
</code></pre>
<p>Why does this happen and how do I solve it?</p>
<p>Any help would be appreciated.</p>
 4143: <p>Microcontroller inputs don't detect the presence of current. They compare a voltage to a threshold.</p>
<p>In reality the input pin is closest to a capacitor. You press your button and it charges that capacitor to above the threshold voltage for &quot;HIGH&quot;. Then you release the button but that capacitor remains charged and still reading &quot;HIGH&quot;. You need something to reduce the charge on that capacitor to below the &quot;LOW&quot; threshold voltage.</p>
<p>This is most normally done with a resistor between the GPIO pin and GND which will &quot;bleed off&quot; that charge and give you a LOW reading when the button is released.</p>
<p>This is a &quot;pull down&quot; resistor (you can also reverse it and have a &quot;pull up&quot; resistor and the button connected to GND) and is an essential part of any input that doesn't itself generate both the HIGH and LOW voltages required (any switch or button).</p>
<p>Also 10V on a GPIO pin <strong>will</strong> destroy your Arduino.</p>
