Arduino Nano 33 IoT - Slide Switch

This tutorial instructs you how to use Arduino Nano 33 IoT to read the position of a slide switch. The part is the DIYables Mini Slide Switch SS12D10. Its small handle stays where you push it, so it is a good way to give a connected gadget a setting: for example, a privacy switch that stops a sensor node from sending data to the cloud, or a "setup / run" switch that the sketch checks before it joins WiFi. In detail, we will learn:

Arduino Nano 33 IoT slide switch SS12D10 - a 3.3V SPDT switch read as ON or OFF

We suggest:

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Mini Slide Switch SS12D10
1×Jumper Wires
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 and Its Pinout

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

The SS12D10 is a tiny two-position switch. A 5 mm handle sticks out of the top, and three pins come out of the bottom in a straight row. Slide the handle to one end and it stays there: the switch latches. A push button is different, because it jumps back as soon as you let go. The switch type is SPDT (single pole, double throw), also written 1P2T. That means one common pin, which is joined to one of the two other pins.

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

The AC 125V 2A rating tells you how much the contacts can carry. Here they carry almost nothing: only the tiny current that flows into one input pin of the Arduino Nano 33 IoT.

How does it work? Under the handle, a small metal bridge always presses on the middle pin (pin 2, COM). When you move the handle, the bridge moves with it and also touches the outer pin on that side:

Handle position Pins joined inside Pin left open D2 level (3-wire wiring)
Toward pin 1 pin 1 and COM pin 3 LOW (OFF)
Toward pin 3 COM and pin 3 pin 1 HIGH (ON)

No labels are printed on the switch, so count the pins from one end of the row:

  • Pin 1 (outer pin): must be connected to a GND pin (0V) of the board
  • Pin 2, COM (middle pin): must be connected to D2, the input pin that the code below reads
  • Pin 3 (other outer pin): must be connected to 3.3V when you use three wires. With the 2-wire INPUT_PULLUP wiring, leave it empty

※ NOTE THAT:

Which outer pin is "pin 1"? It does not matter much. Both outer pins work the same way. If GND ends up on the other outer pin, the sketch still runs; it just reports ON at the opposite end of the handle. What really matters is that D2 goes to the middle pin.

Wiring Diagram

Three jumper wires are enough: the middle pin (COM) to D2, pin 1 to GND, and pin 3 to the 3.3V pin. No resistor is needed, because COM always connects D2 to either GND or 3.3V, so the pin never floats.

The wiring diagram between Arduino Nano and 33 IoT slide switch

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Nano 33 IoT Pin
Pin 1 GND
Pin 2 (COM) D2
Pin 3 3.3V

If you plan to keep this circuit, think about screw terminals. To do that, mount the Arduino Nano 33 IoT on a Screw Terminal Expansion Board for Arduino Nano. The wiring stays the same. Only the wires now go to the screw terminals, as shown below.

The wiring diagram between Arduino Nano and 33 IoT slide switch Screw Terminal

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • Use the 3.3V pin for pin 3. Do not use VUSB (the 5V pin) or VIN. When the handle points to pin 3, COM passes that voltage straight into D2. The SAMD21 chip on the Arduino Nano 33 IoT works at 3.3V, and its pins are not 5V tolerant, so 5V on D2 can damage the board.
  • Keep D2 as an INPUT. Never set it to OUTPUT: the switch ties it straight to 3.3V or GND, so an output pin could be shorted.

How To Program For Slide Switch

No library is needed. The Arduino Nano 33 IoT reads the slide switch with the built-in digitalRead() function, just like a button.

  • The first step is to name the Arduino Nano 33 IoT pin that the COM pin is connected to:
#define SLIDE_SWITCH_PIN 2
  • Inside setup(), call pinMode() with INPUT. A pull-up is not needed, because the switch itself drives D2 to 3.3V or to GND:
pinMode(SLIDE_SWITCH_PIN, INPUT);
  • Read the level with digitalRead(). HIGH means the handle is toward pin 3 (ON), LOW means it is toward pin 1 (OFF):
int reading = digitalRead(SLIDE_SWITCH_PIN);
  • The contacts bounce for a few milliseconds while the handle travels. So when the level is not the same as the last confirmed one, wait 50 milliseconds, read again, and accept the change only if it is still there.

Arduino Nano 33 IoT Code for Slide Switch

The sketch shows where the handle is, then adds one line for each real move. It starts with switch_state set to -1, a value that digitalRead() never returns. So the very first reading counts as a change, and the start position is printed by the same code. Because the Arduino Nano 33 IoT talks to the PC through its own native USB port, setup() also waits up to 3 seconds for the Serial Monitor. Without a PC, the sketch simply goes on.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-slide-switch */ #define SLIDE_SWITCH_PIN 2 // The Arduino Nano 33 IoT pin D2 connected to the COM (middle) pin of the slide switch #define BOUNCE_TIME 50 // how long the contacts may bounce after the handle moves (milliseconds) int switch_state = -1; // last confirmed level of D2, -1 = nothing confirmed yet, so the start position is printed too void setup() { Serial.begin(9600); // The Nano 33 IoT uses its own native USB port. Give the Serial Monitor // up to 3 seconds to connect, so the first line is not lost. unsigned long start_time = millis(); while (!Serial && millis() - start_time < 3000) { delay(10); } // plain INPUT: COM is always joined to GND (pin 1) or to 3.3V (pin 3), so D2 never floats pinMode(SLIDE_SWITCH_PIN, INPUT); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); // HIGH = handle toward pin 3, LOW = handle toward pin 1 if (reading != switch_state) { // a new level - it can be a real move or just contact bounce delay(BOUNCE_TIME); // let the contacts settle reading = digitalRead(SLIDE_SWITCH_PIN); // read the pin again after the wait if (reading != switch_state) { // the new level is still there, so the move is real switch_state = reading; if (reading == HIGH) Serial.println("The slide switch is ON (handle toward pin 3, D2 = HIGH)"); else Serial.println("The slide switch is OFF (handle toward pin 1, D2 = LOW)"); } } }

Detailed Instructions

New to the Arduino Nano 33 IoT? Go through the Getting Started with Arduino Nano 33 IoT tutorial first, then come back here.

  • Wire the components as shown in the diagram. Check that pin 3 is on 3.3V, not on VUSB.
  • Connect the Arduino Nano 33 IoT board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and its COM port.
  • Paste the code above into a new sketch.
  • Push the handle toward pin 3.
  • Click the Upload button to compile the sketch and send it to the board.
  • Open the Serial Monitor and choose 9600 baud.
  • Slide the handle five times: toward pin 1, pin 3, pin 1, pin 3 and pin 1 again. Wait a moment at each end.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano 33 IoT
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano 33 IoT' on 'COM15')
New Line
9600 baud
The slide switch is ON (handle toward pin 3, D2 = HIGH) The slide switch is OFF (handle toward pin 1, D2 = LOW) The slide switch is ON (handle toward pin 3, D2 = HIGH) The slide switch is OFF (handle toward pin 1, D2 = LOW) The slide switch is ON (handle toward pin 3, D2 = HIGH) The slide switch is OFF (handle toward pin 1, D2 = LOW)
Ln 11, Col 1
Arduino Nano 33 IoT on COM15
2

You get six lines: one for the start position and one for each of the five moves. If the first line is missing, the Serial Monitor connected a bit too late. That is not a problem; the next move prints a new line. If the level never changes, check that the D2 wire sits on the middle pin of the switch, not on an outer one.

Arduino Nano 33 IoT Code for Slide Switch - Two Wires and INPUT_PULLUP

Is the 3.3V pin already busy powering a sensor? Or do you want one wire less inside a small case? The SAMD21 chip has a pull-up resistor built into each input pin, and INPUT_PULLUP turns it on. Then pin 3 needs no wire. Just remove the pin 3 wire: COM stays on D2, and pin 1 stays on GND.

The wiring diagram between Arduino Nano and 33 IoT slide switch INPUT_PULLUP

This image is created using Fritzing. Click to enlarge image

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

Be careful: with this wiring, the meaning of HIGH and LOW is reversed.

  • Handle toward pin 1: COM connects D2 to GND. D2 reads LOW, and the sketch prints ON.
  • Handle toward pin 3: COM touches only the free pin 3. The internal pull-up lifts D2 to HIGH, and the sketch prints OFF.
/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-slide-switch */ // 2-wire version: COM (pin 2) -> D2, pin 1 -> GND, pin 3 is left free. // INPUT_PULLUP turns on the pull-up resistor inside the SAMD21 chip. It lifts D2 to 3.3V // whenever COM is not joined to GND, so the logic is the other way round: // LOW = handle toward pin 1 (COM joined to GND) = ON // HIGH = handle toward pin 3 (COM open, D2 pulled up) = OFF #define SLIDE_SWITCH_PIN 2 // The Arduino Nano 33 IoT pin D2 connected to the COM (middle) pin of the slide switch #define BOUNCE_TIME 50 // how long the contacts may bounce after the handle moves (milliseconds) int switch_state = -1; // last confirmed level of D2, -1 = nothing confirmed yet, so the start position is printed too void setup() { Serial.begin(9600); // The Nano 33 IoT uses its own native USB port. Give the Serial Monitor // up to 3 seconds to connect, so the first line is not lost. unsigned long start_time = millis(); while (!Serial && millis() - start_time < 3000) { delay(10); } // INPUT_PULLUP: without the pin 3 wire, the internal pull-up keeps D2 from floating pinMode(SLIDE_SWITCH_PIN, INPUT_PULLUP); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); // LOW = handle toward pin 1, HIGH = handle toward pin 3 if (reading != switch_state) { // a new level - it can be a real move or just contact bounce delay(BOUNCE_TIME); // let the contacts settle reading = digitalRead(SLIDE_SWITCH_PIN); // read the pin again after the wait if (reading != switch_state) { // the new level is still there, so the move is real switch_state = reading; if (reading == LOW) // inverted: LOW means ON in this wiring Serial.println("The slide switch is ON (handle toward pin 1, D2 = LOW)"); else Serial.println("The slide switch is OFF (handle toward pin 3, D2 = HIGH)"); } } }

Detailed Instructions

  • Take the wire off pin 3 of the switch. Leave COM on D2 and pin 1 on GND.
  • Upload the code above in place of the first sketch. The handle is still toward pin 1 from the last test.
  • Slide the handle toward pin 3, then back toward pin 1.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano 33 IoT
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano 33 IoT' on 'COM15')
New Line
9600 baud
The slide switch is ON (handle toward pin 1, D2 = LOW) The slide switch is OFF (handle toward pin 3, D2 = HIGH) The slide switch is ON (handle toward pin 1, D2 = LOW)
Ln 11, Col 1
Arduino Nano 33 IoT on COM15
2

The handle toward pin 1 now prints ON. In the 3-wire sketch, the same position printed OFF.

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do NOT use internal or external pull-down resistors for these pins

Additional Knowledge

3 Wires or 2 Wires?

3-Wire Wiring 2-Wire Wiring
Wires on the switch pin 1, COM and pin 3 pin 1 and COM
pinMode() INPUT INPUT_PULLUP
What makes D2 HIGH pin 3, joined to 3.3V the pull-up resistor inside the SAMD21
Handle toward pin 3 HIGH = ON HIGH = OFF
Handle toward pin 1 LOW = OFF LOW = ON
Needs the 3.3V pin yes no

It is evident that both ways read the switch equally well. The 3-wire wiring keeps the natural logic (HIGH = ON). The 2-wire wiring saves one wire and keeps the 3.3V pin free; you only have to swap ON and OFF in the code.

Project Ideas

A slide switch suits the jobs of a connected board well:

  • A privacy switch: while it is OFF, the Arduino Nano 33 IoT keeps measuring but stops sending data to the cloud.
  • A WiFi selector that the sketch reads once in setup(): one position joins the home network, the other joins the tutorial network.
  • A display switch: show the readings on a local LCD, or keep the screen dark to save power.

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!