Go Down

Topic: switch led on/off (Read 251 times) previous topic - next topic

user4592357

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: https://www.youtube.com/watch?v=SKQzljqd3cQ. by the way, at the end of this video he explains the circuit: https://www.youtube.com/watch?v=8bufqdXwCpY

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:
Code: [Select]
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); 
  }
}

Delta_G

It looks like you might be confused about how the breadboard works.  It's hard to tell though through the tangle of wires.  I don't think the green wire is connected to the button at all and the red wire doesn't appear to be connected to the resistor. 

The little groups of 5 going horizontal on either side are connected.  Adjacent rows going vertical are not. 

I also don't see any current limiting resistor with that LED.  You're going to let the magic smoke out of your Arduino if you keep that up. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

user4592357

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.


Jobi-Wan

Those two legs of the button are connected internally.



Try rotating the button 90 degrees.

Grumpy_Mike

Quote
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.

Quote
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.

Jobi-Wan

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.

Go Up