Arduino MKR WiFi 1010 - Slide Switch

In this tutorial, we are going to learn:

Arduino MKR WiFi 1010 slide switch SS12D10 - detect the ON/OFF handle position on pin D2

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×Mini Slide Switch SS12D10
1×Jumper Wires

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 very small switch: a plastic body, a 5mm handle sticking out of the top, and three legs in one row underneath. You move the handle left or right with a fingertip, and it stays where you leave it. A push button does the opposite. It springs back the moment you let go. This "stay where you put it" behavior is called latching. It makes the slide switch a good fit for choices you set once and then forget, for example "WiFi on / WiFi off" on a battery-powered MKR project, or "send data every minute / every hour".

On the datasheet, the contact type is SPDT, short for Single Pole, Double Throw. Some sellers write 1P2T. It means there is one common pin, and it can be joined to either of the two other pins, but never to both at once.

SS12D10
Switch Type SPDT (1P2T) slide switch, 2 positions
Pins 3 in one row, 4.7mm apart, 6.9mm long
Handle 5mm
Body about 12.8 x 6.7 x 18.2mm
Mounting vertical, through-hole
Contact Material silver-plated brass
Contact Rating AC 125V 2A
Switching Life more than 100,000 cycles
Operating Temperature -25°C to 80°C

The AC 125V 2A line is the limit for switching a real load with the contacts. In this project, the switch only passes a tiny signal into pin D2 of the MKR, so we stay far below it.

Pinout

SS12D10 slide switch pinout - outer pin 1, COM pin 2 in the middle, outer pin 3, and the two handle positions

The switch has no labels on it. Hold it with the legs pointing down: the middle leg is COM, and the legs at the two ends are pin 1 and pin 3.

  • Pin 1 (end leg): needs to be connected to a GND pin of Arduino MKR WiFi 1010 (0V)
  • Pin 2, COM (middle leg): needs to be connected to a digital pin of Arduino MKR WiFi 1010. This tutorial uses D2
  • Pin 3 (other end leg): needs to be connected to the VCC pin of Arduino MKR WiFi 1010, which gives 3.3V

Inside, a small metal bridge slides along with the handle. It always touches COM, and it also reaches the outer pin on the handle's side. With pin 1 on GND and pin 3 on 3.3V, this is what pin D2 sees:

Handle PositionCOM Is Joined ToPin D2 ReadsThe Sketch Prints
Toward pin 1pin 1 (GND)LOWOFF
Toward pin 3pin 3 (3.3V)HIGHON

Compared with the push button in the Arduino MKR WiFi 1010 - Button tutorial, there is one more difference: the button gives a short pulse while your finger is on it, but the slide switch gives a level that lasts for hours. The code does not have to remember anything. It can simply read D2 whenever it needs the setting.

Wiring Diagram

Use three jumper wires and no resistor: pin 1 to GND, the middle pin (COM) to D2, and pin 3 to the VCC pin of the MKR, which is its 3.3V output (the diagram tags this wire 3V3).

The wiring diagram between Arduino MKR WiFi 1010 slide switch

This image is created using Fritzing. Click to enlarge image

Slide Switch PinArduino MKR WiFi 1010 Pin
Pin 1GND
Pin 2 (COM)D2
Pin 3VCC (3.3V)

※ NOTE THAT:

  • 3.3V only: pin 3 goes to VCC, never to the 5V or VIN pin. When the handle is at the pin 3 end, COM passes that voltage straight into D2. The SAMD21 chip of the MKR WiFi 1010 accepts at most 3.3V on its pins, so 5V can damage it.
  • No pull-up or pull-down resistor is needed. Through the switch, D2 is always tied to GND or to 3.3V, so it never floats. Plain INPUT mode is enough.
  • Leave D2 as an INPUT. If the code ever made it an OUTPUT, the switch would connect that output straight to VCC or GND, and the pin could be damaged.

The board and the switch both get their power from the Micro USB cable.

How To Program For Slide Switch

  • Define the pin of the COM wire: const int SLIDE_SWITCH_PIN = 2;
  • Set it as a plain input in setup(): pinMode(SLIDE_SWITCH_PIN, INPUT);. The internal pull-up stays off, because the switch already gives D2 a clear level in both positions.
  • Read the handle: int state = digitalRead(SLIDE_SWITCH_PIN); gives HIGH at the pin 3 end (ON) and LOW at the pin 1 end (OFF).
  • Filter the bounce: the contacts chatter for a few milliseconds after each move. When a reading is not the same as the saved one, delay(50), read D2 again, and accept the change only if it is still there.
  • Wait for the Serial Monitor: add while (!Serial && millis() < 3000); right after Serial.begin(9600);. The MKR talks to the PC over its own (native) USB port, and this line gives the Serial Monitor up to 3 seconds to connect, so the first message is not lost.

Arduino Code - Read the Slide Switch Position

When the MKR starts, the sketch tells you where the handle is. After that, it watches D2 and writes one line each time the handle really moves to the other end.

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-slide-switch */ const int SLIDE_SWITCH_PIN = 2; // Arduino MKR WiFi 1010 pin D2 connected to the middle pin (COM) of the slide switch const int DEBOUNCE_DELAY = 50; // time (in ms) for the contacts to stop bouncing after the handle moves int last_state; // the last confirmed level of D2 void print_state(int state) { // pin 3 of the switch is on VCC (3.3V), pin 1 is on GND if (state == HIGH) Serial.println("D2 is HIGH, the slide switch is ON"); // handle toward pin 3 else Serial.println("D2 is LOW, the slide switch is OFF"); // handle toward pin 1 } void setup() { Serial.begin(9600); while (!Serial && millis() < 3000); // native USB: give the Serial Monitor up to 3 seconds to connect pinMode(SLIDE_SWITCH_PIN, INPUT); // plain INPUT: the switch always ties D2 to 3.3V or to GND last_state = digitalRead(SLIDE_SWITCH_PIN); // where is the handle at power-up? Serial.print("Power-up check: "); print_state(last_state); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != last_state) { // D2 looks different from before delay(DEBOUNCE_DELAY); // wait until the contacts stop bouncing reading = digitalRead(SLIDE_SWITCH_PIN); // read D2 one more time if (reading != last_state) { // still different, so the handle really moved last_state = reading; // remember the new position Serial.print("New position: "); print_state(last_state); } } }

Detailed Instructions

  • Connect Arduino MKR WiFi 1010 to PC via Micro USB cable.
  • Wire the slide switch as shown in the wiring diagram. Before you go on, check once more that pin 3 sits on VCC, not on 5V.
  • Navigate to the Arduino IDE, then select the Arduino MKR WiFi 1010 board and its port. If the board is new to you, see Getting Started with Arduino MKR WiFi 1010 first.
  • Copy the above code and paste it into Arduino IDE.
  • Leave the handle at the pin 3 end, then click the Upload button to send the code to Arduino MKR WiFi 1010.
How to upload Arduino MKR WiFi 1010 code on Arduino IDE
  • Open the Serial Monitor right away and set the baud rate to 9600.
  • Push the handle to the pin 1 end, back to the pin 3 end, and to the pin 1 end again.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino MKR WiFi 1010
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino MKR WiFi 1010' on 'COM15')
New Line
9600 baud
Power-up check: D2 is HIGH, the slide switch is ON New position: D2 is LOW, the slide switch is OFF New position: D2 is HIGH, the slide switch is ON New position: D2 is LOW, the slide switch is OFF
Ln 11, Col 1
Arduino MKR WiFi 1010 on COM15
2

Three moves give three "New position" lines, even though the contacts bounced each time. If the "Power-up check" line is missing, the Serial Monitor connected after the 3 seconds were over. That is not a problem: the next move of the handle prints as usual.

Something looks wrong?

  • ON and OFF are the other way round: the GND and VCC wires are swapped on pin 1 and pin 3. Swap them back, or keep it and treat that side as ON.
  • Nothing is printed when you move the handle: the D2 wire is probably on an outer leg. Move it to the middle leg.

Arduino Code - Slide Switch with Only Two Wires (INPUT_PULLUP)

Is the VCC pin already busy with a sensor, or do you want fewer wires going to a switch on the lid of a box? Each digital pin of the MKR WiFi 1010 has a pull-up resistor inside the SAMD21 chip, and INPUT_PULLUP turns it on. Then pin 3 of the switch needs no wire.

The wiring diagram between Arduino MKR WiFi 1010 slide switch INPUT_PULLUP

This image is created using Fritzing. Click to enlarge image

Slide Switch PinArduino MKR WiFi 1010 Pin
Pin 1GND
Pin 2 (COM)D2
Pin 3not connected

With this wiring, LOW now means ON and HIGH means OFF. Here is why:

  • Handle toward pin 1: COM ties D2 to GND. D2 reads LOW, and the sketch prints ON.
  • Handle toward pin 3: COM touches only the empty pin 3, so nothing drives D2. The pull-up holds it at 3.3V. D2 reads HIGH, and the sketch prints OFF.
/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-slide-switch */ const int SLIDE_SWITCH_PIN = 2; // Arduino MKR WiFi 1010 pin D2 connected to the middle pin (COM) of the slide switch const int DEBOUNCE_DELAY = 50; // time (in ms) for the contacts to stop bouncing after the handle moves int last_state; // the last confirmed level of D2 // Only two wires here: pin 1 on GND, COM on D2, pin 3 not connected. // So the logic is the other way round: // - handle toward pin 1: COM ties D2 to GND, D2 reads LOW -> ON // - handle toward pin 3: COM is open, the internal pull-up resistor // pulls D2 up to 3.3V, so D2 reads HIGH -> OFF void print_state(int state) { if (state == LOW) Serial.println("D2 is LOW, the slide switch is ON"); // handle toward pin 1 else Serial.println("D2 is HIGH, the slide switch is OFF"); // handle toward pin 3 } void setup() { Serial.begin(9600); while (!Serial && millis() < 3000); // native USB: give the Serial Monitor up to 3 seconds to connect pinMode(SLIDE_SWITCH_PIN, INPUT_PULLUP); // turn on the internal pull-up, so pin 3 needs no wire last_state = digitalRead(SLIDE_SWITCH_PIN); // where is the handle at power-up? Serial.print("Power-up check: "); print_state(last_state); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != last_state) { // D2 looks different from before delay(DEBOUNCE_DELAY); // wait until the contacts stop bouncing reading = digitalRead(SLIDE_SWITCH_PIN); // read D2 one more time if (reading != last_state) { // still different, so the handle really moved last_state = reading; // remember the new position Serial.print("New position: "); print_state(last_state); } } }

Detailed Instructions

  • Just remove the pin 3 wire, between the switch and VCC. Keep the GND and D2 wires where they are.
  • Put the handle at the pin 1 end.
  • Copy the code above into Arduino IDE, in place of the first sketch, and click the Upload button.
  • Open the Serial Monitor at 9600 baud, then slide the handle to pin 3 and back to pin 1, two times.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino MKR WiFi 1010
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino MKR WiFi 1010' on 'COM15')
New Line
9600 baud
Power-up check: D2 is LOW, the slide switch is ON New position: D2 is HIGH, the slide switch is OFF New position: D2 is LOW, the slide switch is ON New position: D2 is HIGH, the slide switch is OFF New position: D2 is LOW, the slide switch is ON
Ln 11, Col 1
Arduino MKR WiFi 1010 on COM15
2

Look at the first line: the handle is at the pin 1 end, and that is ON now. In the 3-wire version, the same end meant OFF. Which one should you use? The 3-wire version reads the natural way (HIGH = ON). The 2-wire version saves a wire and keeps the VCC pin free. For a key that springs back after each press, see the Arduino MKR WiFi 1010 - Button and Arduino MKR WiFi 1010 - Button - Debounce tutorials.

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!