I cant explain this.

Hi all,

I am very new to Arduino. I took some coding classes in high school 10 years ago and haven't touched it since.... BUuuut this doesn't seem to be a coding problem. I've got my Uno wired and programed to have an LED on and shut off when you press a button, but the light just shuts off when ever it feels like it and more often when I move my hand close to it. I've tried different jumpers, LED's, terminals on the arduino (and changed the programming to match), power through USB and 9V. This is baffling me. I cant figure it out. HELP!!! I uploaded a Youtube video of the problem too see attached

int switchstate = 0;
void setup() {
 // put your setup code here, to run once:
pinMode(3, OUTPUT);

pinMode(7, INPUT);

void loop() {
 // put your main code here, to run repeatedly:
switchstate = digitalRead(7);

if (switchstate == LOW)
digitalWrite(3, LOW);
digitalWrite(3, HIGH);


You need a pullup or pulldown resistor on your input pin, to force it to be high or low respectively, when it's not pressed. Easiest way is to use the built-in pullup (there's no builtin pulldown) in code by changing this:

pinMode(7, INPUT);

to this:

pinMode(7, INPUT_PULLUP);