Arduino - Slide Switch

You have seen slide switches on toys, small speakers and battery boxes: push the handle one way for ON, the other way for OFF. In this tutorial, we are going to learn:

Arduino Uno slide switch SS12D10 - print the ON/OFF position of an SPDT switch on Serial Monitor

About Slide Switch

The SS12D10 is a tiny switch with a sliding handle on top and three metal pins in a row below. Move the handle to one side and it stays there. This is the big difference from a push button. A button jumps back as soon as you let go. A slide switch keeps its position until you move it again. So it fits settings that should stay put, like a power switch, a "sound on/off" option or a "test mode" selector.

Its contact type is SPDT (Single Pole, Double Throw), also written as 1P2T. In plain words: the middle pin is joined to one of the two outer pins, never to both.

SS12D10
Contact Type SPDT (1P2T), 2 positions
Pins 3, 4.7mm pitch
Terminal Length 6.9mm
Handle Length 5mm
Body Size about 12.8 x 6.7 x 18.2mm
Mounting vertical, through-hole
Contacts silver-plated brass
Contact Rating AC 125V 2A
Life more than 100,000 switching cycles
Operating Temperature -25°C to 80°C

Do not worry about the 125V 2A rating. It is the most the contacts can handle. In this project, only a very small signal goes through the switch into pin 7.

Pinout

SS12D10 slide switch pinout - pin 1, the middle COM pin 2 and pin 3, with both handle positions

Nothing is printed next to the pins. Just remember: the middle one is always the common pin.

  • Pin 1: an outer pin. In this tutorial, it needs to be connected to GND (0V)
  • Pin 2 (COM): the middle pin. It needs to be connected to an Arduino digital input pin
  • Pin 3: the other outer pin. In this tutorial, it needs to be connected to 5V

How it works: a tiny metal slider moves with the handle. It links the middle pin with the outer pin at the handle's end.

  • Handle toward pin 1: pin 1 and COM are connected, pin 3 is open
  • Handle toward pin 3: COM and pin 3 are connected, pin 1 is open

With pin 1 on GND and pin 3 on 5V, COM is always tied to one of them. Arduino reads LOW in one position and HIGH in the other.

Wiring Diagram

The middle pin goes to Arduino pin 7, and the two outer pins go to GND and 5V.

Arduino slide switch wiring diagram

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Uno Pin
Pin 1 GND
Pin 2 (COM) Pin 7
Pin 3 5V

Loose wires are a common cause of strange bugs. Try a Screw Shield V2 or a Proto Screw Terminal Block Shield: it sits on the Arduino Uno, and each pin gets its own screw terminal. The diagram below shows the same wiring with the Screw Shield V2.

Arduino slide switch Screw Shield Wiring Diagram

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • No resistor is needed. Through the switch, pin 7 is always tied to 5V or GND, so it cannot pick up random values.
  • Pin 3 goes to 5V because 5V is the logic level of the Arduino Uno. The 3.3V pin is not used in this wiring.
  • Keep pin 7 as an INPUT. Never make it an OUTPUT in your code: the switch ties it straight to 5V or GND, and an output fighting that can damage the pin.

How To Program For Slide Switch

  • Define the Arduino pin that the COM wire goes to:
#define SLIDE_SWITCH_PIN 7
  • Set that pin as a plain input with pinMode(). The switch drives the pin HIGH or LOW by itself, so no pull-up is needed:
pinMode(SLIDE_SWITCH_PIN, INPUT);
int position = digitalRead(SLIDE_SWITCH_PIN); if (position == HIGH) { // handle toward pin 3: ON } else { // handle toward pin 1: OFF }
  • Handle the bounce. When the handle moves, the metal contacts bounce for a few milliseconds. So when the reading changes, wait 50 ms and read once more before you trust it.

Arduino Code - Slide Switch

The sketch prints the switch position once when Arduino starts. After that, it stays quiet and prints a new line only when you move the handle.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-slide-switch */ #define SLIDE_SWITCH_PIN 7 // Arduino pin connected to the middle pin (COM) of the slide switch #define DEBOUNCE_TIME 50 // time (ms) to wait until the contacts stop bouncing int lastState; // the last position that was printed void printPosition(int state) { if (state == HIGH) // handle toward pin 3: COM is joined to 5V Serial.println("Slide switch: ON"); else // handle toward pin 1: COM is joined to GND Serial.println("Slide switch: OFF"); } void setup() { Serial.begin(9600); pinMode(SLIDE_SWITCH_PIN, INPUT); // plain INPUT: the switch always ties the pin to 5V or GND lastState = digitalRead(SLIDE_SWITCH_PIN); // where is the handle right now? printPosition(lastState); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != lastState) { // the handle seems to have moved delay(DEBOUNCE_TIME); // let the contacts settle reading = digitalRead(SLIDE_SWITCH_PIN); // read again if (reading != lastState) { // still different, so the move is real lastState = reading; // remember the new position printPosition(lastState); } } }

Quick Steps

  • Connect Arduino to PC via USB cable.
  • Wire the slide switch to Arduino as shown in the wiring diagram.
  • Open Arduino IDE, select the right board and port.
  • Copy the above code and paste it to Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor and set the baud rate to 9600.
  • Slide the handle to the pin 3 side, then back to the pin 1 side. Do it one more time.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Slide switch: OFF Slide switch: ON Slide switch: OFF Slide switch: ON Slide switch: OFF
Ln 11, Col 1
Arduino Uno on COM15
2

The first line shows where the handle was when the sketch started. Every line after it is one move of the handle. Want ON and OFF the other way round? Swap the GND and 5V wires on pin 1 and pin 3, or swap the two messages in the code.

If the output never changes, check that the pin 7 wire is on the middle pin of the switch, not on an outer one.

Arduino Code - 2-Wire Slide Switch with INPUT_PULLUP

Want fewer wires? Each digital pin of the Arduino Uno has a built-in pull-up resistor. Turn it on with INPUT_PULLUP, and pin 3 needs no wire at all. This helps when the switch is far from the board, or when the 5V pin is already busy.

Arduino slide switch INPUT_PULLUP wiring diagram

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Uno Pin
Pin 1 GND
Pin 2 (COM) Pin 7
Pin 3 not connected

Note that ON and OFF now swap places:

  • Handle toward pin 1: COM is joined to GND, so pin 7 reads LOW. The code calls this ON.
  • Handle toward pin 3: COM is connected to nothing. The pull-up resistor pulls pin 7 up, so it reads HIGH. The code calls this OFF.
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-slide-switch */ #define SLIDE_SWITCH_PIN 7 // Arduino pin connected to the middle pin (COM) of the slide switch #define DEBOUNCE_TIME 50 // time (ms) to wait until the contacts stop bouncing int lastState; // the last position that was printed // With INPUT_PULLUP the logic is flipped: // - handle toward pin 1: COM is joined to GND, the pin reads LOW -> ON // - handle toward pin 3: COM is open, the internal pull-up resistor // pulls the pin up, so it reads HIGH -> OFF void printPosition(int state) { if (state == LOW) Serial.println("Slide switch: ON"); else Serial.println("Slide switch: OFF"); } void setup() { Serial.begin(9600); pinMode(SLIDE_SWITCH_PIN, INPUT_PULLUP); // turn on the internal pull-up, so pin 3 needs no wire lastState = digitalRead(SLIDE_SWITCH_PIN); // where is the handle right now? printPosition(lastState); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != lastState) { // the handle seems to have moved delay(DEBOUNCE_TIME); // let the contacts settle reading = digitalRead(SLIDE_SWITCH_PIN); // read again if (reading != lastState) { // still different, so the move is real lastState = reading; // remember the new position printPosition(lastState); } } }

Quick Steps

  • Keep the wiring from the first example, and just remove the wire on pin 3.
  • Copy the above code and paste it to Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor (baud rate 9600).
  • Move the handle to the pin 3 side, then back to the pin 1 side.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Slide switch: ON Slide switch: OFF Slide switch: ON
Ln 11, Col 1
Arduino Uno on COM15
2

Need a bigger switch for a panel? See Arduino - Switch. For a row of tiny switches in one package, see Arduino - DIP Switch. For a key that returns by itself after each press, see Arduino - Button or Arduino - Button Module.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES