I am trying to write a simple program to control one LED with two switches.
If any of the switches is pressed, I want to turn the LED on.
My LED however is always ON even when none of the switches are pushed. Why could this happen?
Thanks!
https://www.tinkercad.com/things/7SRoEXkHRqh-one-led-two-switches-
-
the input pins are directly connected to 5V, as the button has the pins on the sides directly connected (the real button sits between right and left). move the two red wires at the top, which go to the input pins, to the right side of the switches (above where the resistors are connected). If that works, I will write an answerchrisl– chrisl01/05/2021 18:52:51Commented Jan 5, 2021 at 18:52
-
Now, that you changed the circuit, it seems to work. Is that correct?chrisl– chrisl01/06/2021 09:17:56Commented Jan 6, 2021 at 9:17
2 Answers 2
It's hard to tell which pins of the switch do what. Since you have your inputs set up as INPUT (instead of INPUT_PULLUP) you should have a resistor (10k Ω, say) tying those pins to ground. Then the other end of switch should connect to +5V. Don't connect the switches to both +5V and ground.
Use a multimeter on continuity to make sure that the pins of the switch that you are using are open normally, and close when you press the switch. With 4 pins it's easy to get confused as to which switch pin does what.
It's easy to confuse the pins of these single-pole buttons - I still do it. Only two pins are needed for this kind of button. The extra two are there for mechanical stability on the PC board.
Lay the button on one of its sides:
If the two upper pins come out of the top edge, those pins are switched - either open, or shorted by the button. Ditto, for the two bottom pins.
If instead, the two upper pins come out of opposite edges, those two are literally a single piece of wire passing through the button-frame and so are always shorted to each other. These two pins together are open with respect to the bottom two pins when the button is released, or shorted to the bottom two pins when the button is pressed.
Update:
I probably won't remember your info next time I need to use one of these switches, so I'll still have to just test it... :(
Thanks, @Duncan C. I probably made it more complicated than it needed to be! How about:
Each pair of pins is a flat metal strip, bent to pass through the switch body, each end of it pointing down at the PC board; roughly into the shape of the letter 'C'. Looking at one of the pins, there is only one other pin that could be its other end, because you can only bend a flat strip toward one of its faces, not edgewise.
-
That's useful information on how to figure out which pins are switched and which are connected. I just use a multimeter to figure it out. I probably won't remember your info next time I need to use one of these switches, so I'll still have to just test it... :(Duncan C– Duncan C01/06/2021 19:34:23Commented Jan 6, 2021 at 19:34
Explore related questions
See similar questions with these tags.