Hi guys! My question is why does a usual button (first picture) have to be connected with a resistor but Slide Switch (second picture) is not? I'll be glad if you help me with it
The slide switch needs a resistor as well. An input that is connected to nothing, like when either switch is in the open position, is floating and its state is indeterminate. The resistor pulls the pin to ground (pull down) or to Vcc (pull up). That puts the previously floating pin to a known state.
Got it. Thanks.
The better way to wire a switch is to wire one side to ground and the other to a pin with the pinMode set to INPUT_PULLUP. That uses the built in internal pullup resistor, no external resistor required. And you do not string wires with Vcc on them around, avoiding a potential short to ground of Vcc.
And this way for the slide switch.
So I can solder a button without a resistor and just change a code into pinMode(2, INPUT); to pinMode(2, INPUT_PULLUP). Sorry If I misunderstood you
Be aware, the slide switch, as you have it pictured, has NO connection as you slide it from side to side. The connection is made near to the actual end of travel.
Paul
I think that you have it right. One side to ground and the other to the pin set to pinMode INPUT_PULLUP with the connections to diagonal terminals of the switch if it is one of those popular 6mm tactile momentary push button switches.
Note that in the fritz in the original post that the way the switch is wired you have the pin 2 shorted to ground so the switch will only ever read LOW.



