How can I use a toggle switch with Arduino ?

Hey !

I have a toogle switch like this one

But how can I wire it with Arduino ?
Here is a part of my code with which i'll be using this toggle switch

int switch_pin = 2;  
pinMode(switch_pin, INPUT);

So should I wire my toggle on PWM 2 or Analog 2 ?

Thanks !

Connect one side of the switch to ground, connect the other to the pin,
change the code to:

int switch_pin = 2;  
pinMode(switch_pin, INPUT_PULLUP);

Then calling digitalRead (switch_pin) will return HIGH if the switch is open-circuit
and LOW if its short-circuit.

Next task will be learning to solder and performing debouncing in software I think.

Can't you just connect it to a digital input as described in the button example (Files -> Examples -> Digital -> Button).

By the way, PWM is an output thing.

MarkT:
Connect one side of the switch to ground, connect the other to the pin
Next task will be learning to solder and performing debouncing in software I think.

I already included software debouncing, but the thing is that the switch has 3 pins, what is the center pin for then ?

Its a SPDT (single pole, double throw). You just need to use the centre pin and one of
the others if you want SPST.

MarkT:
Its a SPDT (single pole, double throw). You just need to use the centre pin and one of
the others if you want SPST.

Yes that was what I meant, I want to use it as an SPST but the center pin = Gnd and one of the others = pin 2 ?
Or the other way around ? :smiley:

It's a mechanical switch - it makes no difference. If you have a multimeter with a diode test function, or if you build a little led circuit using the Arduino's 5v and gnd pins, then you can figure all this out for yourself. You wont break anything.

bwat:
It's a mechanical switch - it makes no difference. If you have a multimeter with a diode test function, or if you build a little led circuit using the Arduino's 5v and gnd pins, then you can figure all this out for yourself. You wont break anything.

Okey cool thanks for the help I was afraid for my Arduino :*

SilenT612:
Okey cool thanks for the help I was afraid for my Arduino :*

I understand. Yesterday I accidentally shorted some pins on my LCD when I got clumsy with an oscilloscope probe. Thankfully it only seemed to reset my Uno- nothing broken.

If you always have a spare 9v battery around, then you can quickly build little test circuits to figure out how a component works without risking your Arduino or anything else expensive.

Still doesn't seem to work :frowning:

I get no signal

What happens when you connect this circuit and toggle the switch?

+5v (Arduino) -> Switch -> LED -> 330 Ohm resistor -> Gnd (Arduno)

Using the code above in Reply 1 from MarkT, connect the center pin to ground and connect one of the other two pins to pin 2 of the Arduino. Done.

Which of the outer pins on the switch that you connect to, depends on which one of the toggle switch positions you wish to be closed. Flip it one way, the center pin is connected to the outer pin on the opposite side.

Here is an illustration of why toggle switches generally seem to work backwards as compared to slide switches:

polymorph:
Using the code above in Reply 1 from MarkT, connect the center pin to ground and connect one of the other two pins to pin 2 of the Arduino. Done.

Which of the outer pins on the switch that you connect to, depends on which one of the toggle switch positions you wish to be closed. Flip it one way, the center pin is connected to the outer pin on the opposite side.

Here is an illustration of why toggle switches generally seem to work backwards as compared to slide switches:

Okey cool I'll try that one out later !

Thanks ! :sweat_smile: