i just started watching tutorials. first one was turning the led on/off. i've done that one and was pretty happy.
now i'm watching the second one in which the led should turn on when the button is pressed and turn off when it's not pressed. here''s the video: Arduino Lesson 4 - If Statements - YouTube. by the way, at the end of this video he explains the circuit: Arduino Lesson 2 - digitalRead & digitalWrite - YouTube
in video he's using a pcb on which he's built a small circuits. i'm using a breadboard and i cannot figure out how to correctly wire things up. with what i've done, the led turns on when i upload the sketch and it doesn't turn off, even when the button is pressed.
i've attached what i currently have. please let me know if something can't be seen in the picture.
and here's the code:
int button = 7;
int led = 8;
void setup() {
pinMode(button, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
if (digitalRead(button) == HIGH) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
}
green wire is connected to one leg of the switch and to 5v.
red wire is connected to switch through the resistor and to gnd.
the blue wire (on the same line) is connected to a leg of switch and pin7.
yellow one is wired to pin8.
Those two legs of the button are connected internally.
Try rotating the button 90 degrees.
yellow one is wired to pin8.
Yes but the other end is to 5V so a bit useless doing that because it will always read high.
green wire is connected to one leg of the switch and to 5v.
Your switch is all wrong. Wire across the corners of the switch, wire the switch directly to the input with the other end to ground. Do not wire it through a resistor to ground. Enable the internal pull up resistors when you do your pinMode statement in the setup function. You only need to connect one input pin to one switch, any more is simply a waste as it will not give you any more information.
Grumpy_Mike:
Yes but the other end is to 5V
Pin 8 is an output to drive the LED.
The other end of the yellow wire is connected to where you would expect 5V, but there is no 5V on the breadboard power rail. Only GND, through the orange wire.
The way the button sits on the breadboard, all 4 corners are connected together.