Arduino Nano - Slide Switch

This tutorial instructs you how to use Arduino Nano to find out which way a slide switch is pushed. The part is the DIYables Mini Slide Switch SS12D10: a tiny switch with a sliding handle that stays where you leave it. That makes it a handy mode selector or ON/OFF setting for a small Nano project. In detail, we will learn:

Arduino Nano slide switch SS12D10 - print the ON/OFF handle position on the Serial Monitor

We suggest:

Hardware Preparation

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×USB A to Mini-B USB cable
1×Mini Slide Switch SS12D10
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Slide Switch

The SS12D10 is a small switch with two positions. A short handle sits on top, and three metal pins come out of the bottom in one row. Push the handle to one side and it stays there until you push it back. A push button behaves differently: it jumps back the moment you let go. The switch type is SPDT (single pole, double throw), also written 1P2T. In plain words, one common pin is joined to one of the two other pins, never to both.

How does it work? Inside, a small metal bridge always touches the middle pin, called COM. The bridge slides together with the handle. On one side it joins COM to pin 1, on the other side it joins COM to pin 3.

Slide Switch SS12D10
Contact type SPDT (1P2T), 2 positions
Pins 3, vertical through-hole
Pin pitch 4.7mm
Terminal length 6.9mm
Handle 5mm
Body size about 12.8 x 6.7 x 18.2mm
Contacts silver-plated brass
Contact rating AC 125V 2A
Life more than 100,000 switching cycles
Operating temperature -25°C to 80°C

The 125V 2A value is the limit of the metal contacts. In this tutorial, the switch only passes a tiny signal current into one Nano input pin, far below that limit.

The Slide Switch Pinout

Slide switch SS12D10 pinout - outer pin 1, middle COM pin 2, outer pin 3 and both handle positions

The three pins, from one end of the row to the other:

  • Pin 1 (outer pin): must be connected to GND (0V)
  • Pin 2, COM (middle pin): must be connected to a digital input pin of Arduino Nano. It always carries the voltage of the outer pin that the handle points to
  • Pin 3 (outer pin): must be connected to the 5V pin of Arduino Nano in the 3-wire wiring. In the 2-wire wiring, this pin stays free

The picture also shows what each handle position does:

  • Position 1 (handle pushed to the pin 1 end): COM links to pin 1, and pin 3 is left alone
  • Position 2 (handle pushed to the pin 3 end): COM links to pin 3, and pin 1 is left alone

※ NOTE THAT:

There are no labels printed on the SS12D10. The middle pin is always COM. The two outer pins are the same, so if you swap the wires on pin 1 and pin 3, the switch still works. Only the ON side and the OFF side of the handle trade places.

Wiring Diagram

COM goes to pin D2, pin 1 goes to GND and pin 3 goes to 5V. In both handle positions, D2 is tied firmly to 5V or to GND, so the pin never floats and no pull-up or pull-down resistor is needed.

The wiring diagram between Arduino Nano and slide switch

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Nano Pin
Pin 1 GND
Pin 2 (COM) D2
Pin 3 5V

Want a stronger connection than jumper wires? In this case, use a Screw Terminal Expansion Board for Arduino Nano. The Arduino Nano plugs into it, and each pin gets a screw terminal. The diagram below shows the same wiring with the screw terminal board.

The wiring diagram between Arduino Nano and slide switch Screw Terminal

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • Pin 3 goes to the 5V pin, because 5V is the logic level of the ATmega328P chip on the Nano. The 3V3 pin is not used in this tutorial.
  • Keep D2 set as an input. Never make it an OUTPUT: the switch connects it straight to 5V or to GND, so an output pin that drives the other level would be shorted.

How To Program For Slide Switch

No library is needed. The Nano reads a slide switch like any other digital input.

  • The first step is to specify the Arduino Nano pin that is connected to the COM pin:
#define SLIDE_SWITCH_PIN 2
  • Configure that pin as a plain input in setup() with pinMode(). The switch itself pulls the pin up or down, so INPUT is enough:
pinMode(SLIDE_SWITCH_PIN, INPUT);
  • Get the handle position with digitalRead(). HIGH means the handle points to pin 3 (ON), LOW means it points to pin 1 (OFF):
int handleState = digitalRead(SLIDE_SWITCH_PIN);
  • The metal contacts bounce for a few milliseconds while the handle travels. So when the reading changes, wait 50 milliseconds and read the pin again before you trust the new value.

Arduino Nano Code for Slide Switch

The sketch below prints the handle position once, right after start-up. After that, it stays quiet and adds a line only when you move the handle. Every change is checked a second time after 50 milliseconds, so contact bounce never prints a false line.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-slide-switch */ #define SLIDE_SWITCH_PIN 2 // The Arduino Nano pin wired to the COM (middle) pin of the slide switch #define DEBOUNCE_TIME 50 // How long to wait (in milliseconds) for the contacts to stop bouncing int lastState; // the last confirmed level of the COM pin // Print ON or OFF for a level read from the COM pin void printState(int state) { if (state == HIGH) Serial.println("ON"); // handle toward pin 3: COM is joined to 5V else Serial.println("OFF"); // handle toward pin 1: COM is joined to GND } // The setup function runs once on reset or power-up void setup() { Serial.begin(9600); pinMode(SLIDE_SWITCH_PIN, INPUT); // plain INPUT: the switch always ties the pin to 5V or GND, no pull-up needed lastState = digitalRead(SLIDE_SWITCH_PIN); // the handle can be on either side when the Nano starts Serial.print("Slide switch at start-up: "); printState(lastState); } // The loop function repeats indefinitely 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 once more to confirm if (reading != lastState) { // the change is real, not a bounce lastState = reading; // remember the new position Serial.print("Slide switch changed to "); printState(reading); } } }

Detailed Instructions

  • If this is the first time you use Arduino Nano, see how to set up the environment for Arduino Nano in Arduino IDE.
  • Wire the components as shown in the diagram.
  • Connect the Arduino Nano board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • In the board list, choose Arduino Nano, then choose its COM port.
  • Copy the code above into an empty sketch.
  • Click the Upload button to compile the sketch and send it to the Nano.
  • Open the Serial Monitor and select 9600 baud.
  • Set the handle toward pin 1 and press the reset button of the Nano.
  • Now slide the handle toward pin 3, back toward pin 1, and toward pin 3 once more.
  • Every move adds exactly one line:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' on 'COM15')
New Line
9600 baud
Slide switch at start-up: OFF Slide switch changed to ON Slide switch changed to OFF Slide switch changed to ON
Ln 11, Col 1
Arduino Nano on COM15
2

If the text never changes, check that the middle pin of the switch, not an outer pin, goes to D2.

Arduino Nano Code for Slide Switch - INPUT_PULLUP (2 Wires)

Short on wires, or need the 5V pin for something else? Each digital pin of the Nano has a pull-up resistor inside the chip. Turn it on with INPUT_PULLUP, and the pin 3 wire is no longer needed. Only two wires are left: COM to D2 and pin 1 to GND.

The wiring diagram between Arduino Nano and slide switch INPUT_PULLUP

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Nano and other components.

Slide Switch Pin Arduino Nano Pin
Pin 1 GND
Pin 2 (COM) D2
Pin 3 not connected

Pay attention: with this wiring, the logic is upside down.

  • Handle toward pin 1: COM is joined to GND, so D2 reads LOW. The code calls this ON.
  • Handle toward pin 3: COM touches nothing, and the internal pull-up lifts D2 to HIGH. The code calls this OFF.
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-slide-switch */ // 2-wire version: COM (pin 2) -> D2, pin 1 -> GND, pin 3 is not connected. // INPUT_PULLUP turns on the pull-up resistor inside the Nano. It holds D2 at HIGH while COM touches nothing. // This flips the logic of the 3-wire version: // LOW = handle toward pin 1 (COM joined to GND) = ON // HIGH = handle toward pin 3 (COM open, pulled up) = OFF #define SLIDE_SWITCH_PIN 2 // The Arduino Nano pin wired to the COM (middle) pin of the slide switch #define DEBOUNCE_TIME 50 // How long to wait (in milliseconds) for the contacts to stop bouncing int lastState; // the last confirmed level of the COM pin // Print ON or OFF for a level read from the COM pin (inverted logic) void printState(int state) { if (state == LOW) Serial.println("ON"); // handle toward pin 1: COM pulls D2 down to GND else Serial.println("OFF"); // handle toward pin 3: COM is open, the pull-up keeps D2 HIGH } // The setup function runs once on reset or power-up void setup() { Serial.begin(9600); pinMode(SLIDE_SWITCH_PIN, INPUT_PULLUP); // enable the internal pull-up, so D2 never floats lastState = digitalRead(SLIDE_SWITCH_PIN); Serial.print("Slide switch at start-up: "); printState(lastState); } // The loop function repeats indefinitely 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 once more to confirm if (reading != lastState) { // the change is real, not a bounce lastState = reading; // remember the new position Serial.print("Slide switch changed to "); printState(reading); } } }

Detailed Instructions

  • Pull the wire off pin 3 of the switch. Leave the other two wires in place.
  • Paste this sketch into Arduino IDE, in place of the first one.
  • Click Upload. The handle is still toward pin 3 from the last test, so the first line says OFF.
  • Slide the handle toward pin 1, then toward pin 3, then toward pin 1, then toward pin 3 again.
  • The Serial Monitor now shows:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' on 'COM15')
New Line
9600 baud
Slide switch at start-up: OFF Slide switch changed to ON Slide switch changed to OFF Slide switch changed to ON Slide switch changed to OFF
Ln 11, Col 1
Arduino Nano on COM15
2

Additional Knowledge

Slide Switch vs Push Button vs ON/OFF Rocker Switch

Slide Switch (SS12D10) Push Button ON/OFF Rocker Switch
How you use it slide the handle left or right press the cap down tip the rocker to one side
Stays where you leave it yes, until you slide it back no, it springs back when released yes, until you tip it back
Pins 3 (SPDT, one common pin) 4 legs joined in pairs 2 or 3
Where it goes on a PCB, through-hole on a PCB or a breadboard in a cut-out hole of a panel
Size very small small large and easy to reach
Typical job a mode or setting selector inside a device a short command such as start or next the main power switch of a box

It is evident that the slide switch suits a setting that must stay put on a small board. A push button suits a quick command, and an ON/OFF rocker switch suits a power switch on the outside of a case.

Project Ideas

  • A day/night selector that changes how bright a lamp project shines
  • A mute switch that turns the sound of a buzzer alarm on or off
  • A test/run selector that the sketch reads once at start-up to pick its mode
  • A direction selector for a small DC motor or a stepper motor

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!