I purchased these slide switches recently because I want to use them in my project:

enter image description here

But I don't know what to connect up the three pins to and I can't find any information about them online. Nor can I find sample code. Can someone show me how to wire them up and provide sample code please? I just want to be able to check their state and save it as a boolean variable.

  • As stated by the seller, this is an SPDT switch. – Edgar Bonet May 15 at 10:19
  • Sample code? For how to read a switch? Really? – Duncan C May 15 at 17:56
  • It has only two positions and three pins. You'll figure out how it works in less than a minute if you use a multimeter in the continuity test setting. You dou have one, do you? If you don't, I really recommend adding one to your shopping basket next time. – StarCat May 15 at 18:06
  • for use purely by sensing with an MCU, you can ignore or even cut off one of the outer pins, then it's just a button (logically), for which examples abound. – dandavis May 15 at 18:19

As others have said, this is a SPDT (Single Pole Double Throw) switch. The center pin is common. When the switch is slid one way, it connects the center pin to the outer pin on that side. When you slide the switch the other way, it connects the center pin to the opposite outer pin.

See the link about SPDT switches Edgar provided in his comment: http://www.learningaboutelectronics.com/Articles/What-is-a-single-pole-double-throw-switch-SPDT

That explains these switches pretty clearly.

Connect the center pin of the switch to a digital pin on your Arduino. Connect one of the outer pins to ground.

Set the digital pin on the Arduino to INPUT_PULLUP mode.

Then, when you read the digital state of the pin, if it is LOW, the switch is closed. (Slid to the side of the pin you attached to ground.)

| improve this answer | |

Since it has two possible positions, and three pins, you have a connection between pin 1 and 2 when position 1 is selected, and between pin 1 and 3 when position 2 is selected. As BSF comment below, the middle pin is common. This means it will be connected to the outside pins depending on where the switch is moved towards.

However, you always can verify this, in the following way:

To find out which is pin 1, pin 2 and pin 3, use a multimeter. Set it to continuity mode, or if you don't have that mode, to resistance mode, and put the red/black probes to two random pins. Check if the resistance is 0, in that case you know those two pins will be connected when position 1 is selected. If there is resistance, try other pins with the probes, until you know which are connected.

Than switch the position, and do it again.

Instead of a multimeter you also can 'hardwire' it to e.g. 5V -> pin 1, pin 2 -> resistor of 220 ohm -> LED -> GND And change pin 1 and pin 2 until you found which pins connect for which positions.

To use it for you Arduino, check for a normal simple switch example, but these are two in one, one for each position.

| improve this answer | |
  • 2
    Pin 2, the one in the middle is always the common. There is no other possibility with a slider switch. – SBF May 15 at 10:09
  • @SBF Ok ... my test would have the same conclusion, but good to know it's always the middle position which is common. – Michel Keijzers May 15 at 11:03
  • 1
    The key point of Michel's post is that you can figure out how a switch is wired in a few minutes with a multimeter. This is a very good skill to develop. – Duncan C May 15 at 17:55
  • @DuncanC thanks for the compliment ... upvoted your answer as it confirmed something I thought should work (pullup + LOW active), assuming on MCU's with internal pulldown resistors HIGH active should work equally). – Michel Keijzers May 16 at 10:11

These switches are actually a lot more simple than you would think. You have two positions, but you really only care about one of them, since if it's not in that position it must be in the other.

The central pin is the common, and when slid to one side it connects that central pin to the corresponding outside pin.

So you just use the central pin and one of the outside pins as if it were any other on-off switch or pushbutton. Treat it exactly the same (pullup resistor, which may be internal to the AVR chip, and connect the IO pin to ground through the switch).

If it's "on" (reads LOW) then the switch is in one position. If it's "off" (reads HIGH) then it's in the other position.

The only times you'd want to use both outer pins is if you were wanting to direct a signal to one of two different places, or select between two different incoming signals, or if the switch were a "3 position on-off-on" switch where it can be in either outer position or neither position (centre off), in which case you treat it as two separate switches, but with a common central pin that connects both to ground.

| improve this answer | |
  • So which pin should I connect to ground? And which pin should I connect to Arduino's #2 to get this sketch to work? man.hubwiz.com/docset/Arduino.docset/Contents/Resources/… – Saqib Ali May 15 at 17:50
  • Connect any pin to ground. The pin you connect to #2 depends which pin you connected to ground. If you connected the central pin to ground then use one of the outside pins. If you connected one of the outside pins to ground then connect the central pin to #2. All the switch consists of is three metal posts and a sliding lump of metal that moves across the top of those posts. – Majenko May 15 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.