Arduino Giga R1 WiFi Slide Switch

In this guide, we will learn how to use the DIYables Mini Slide Switch SS12D10 with the Arduino Giga R1 WiFi. The Giga often runs big projects: a WiFi display, a robot arm, or a data logger with many sensors. Such a project often needs one setting that must stay the same until you change it by hand. A "maintenance mode" that keeps a motor stopped while you work on the machine is one example. A night setting that turns a screen dark is another. A slide switch gives the Giga this kind of setting for very little money. In detail, we will learn:

Arduino Giga R1 WiFi with SS12D10 mini slide switch as a latching ON/OFF input

Please do not confuse the slide switch with these parts, which have their own Giga tutorials:

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Mini Slide Switch SS12D10
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega/Giga
1×Recommended: Breadboard Shield for Arduino Mega/Giga
1×Recommended: Enclosure for Arduino Giga
1×Recommended: Power Splitter for Arduino Giga

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 a 5mm handle on top and three legs in one row underneath. Push the handle to one end and it stays there. Push it back and it stays at the other end. This is called a latching switch. A push button works the other way: it jumps back the moment you lift your finger. So a button is good for "do something now", and a slide switch is good for "keep this setting".

In part lists, you will see it called SPDT (single pole, double throw) or 1P2T. Both names mean the same thing: one common pin that can connect to one of two sides.

Mini Slide Switch SS12D10
Contact type SPDT (1P2T), 2 positions
Pins 3 through-hole pins, 4.7mm pitch
Pin length 6.9mm
Handle length 5mm
Body size 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 value is the most the contacts can carry. In this guide, the switch only carries a tiny signal current into one input pin of the Giga, which is far below that limit.

When the handle moves, the metal contacts bounce for a few milliseconds before they settle. The Giga is fast enough to see each bounce as a separate change. Our code waits 50 ms and checks again, so one move gives one line on the screen.

Pinout

SS12D10 slide switch pinout - pin 1, middle COM pin 2, pin 3 and what each handle position joins

The SS12D10 has no pin labels. Hold it with the legs pointing down: the leg in the center is always COM.

  • Pin 1 (outer pin): connect to a GND pin of the Giga (0V).
  • Pin 2, COM (middle pin): connect to digital pin 7 (D7) of the Giga. It carries the voltage of the outer pin that the handle has picked.
  • Pin 3 (outer pin at the other end): connect to the 3.3V pin of the Giga.

A thin metal bridge moves with the handle. One end of it always touches COM. The other end touches the outer pin on the handle's side:

  • Handle toward pin 1: pins 1 and 2 are connected, pin 3 is open. D7 sees GND and reads LOW. We call this OFF.
  • Handle toward pin 3: pins 2 and 3 are connected, pin 1 is open. D7 sees 3.3V and reads HIGH. We call this ON.

Wiring Diagram

Three jumper wires go from the switch to the Giga, with no breadboard. COM is always tied to GND or to 3.3V through the switch, so there's no need to add a pull-up or pull-down resistor. This makes wiring or soldering easier.

The wiring diagram between Arduino Giga R1 WiFi slide switch

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Giga R1 WiFi Pin
Pin 1 GND
Pin 2 (COM) D7
Pin 3 3.3V

※ NOTE THAT:

  • Pin 3 of the switch goes to the 3.3V pin, never to 5V or VIN. In the ON position, COM passes the voltage of pin 3 straight to D7, and the GPIO pins of the Giga work at 3.3V only. A 5V level can damage the pin.
  • Keep D7 as an input. Never make it an OUTPUT: the switch ties it directly to 3.3V or GND, so an output pin would be shorted.

D7 is only our choice. The Giga has dozens of digital pins, so if D7 is busy in your project, pick another one and change SLIDE_SWITCH_PIN in the code. For a project that stays built for a long time, solder the wires to the switch legs, so they cannot slip off.

How To Program For Slide Switch

No library is needed. You read a slide switch with digitalRead(), just like a button.

  • Choose the Giga pin that is wired to COM:
const int SLIDE_SWITCH_PIN = 7;
  • Set it as a normal input in setup(). Pin 1 and pin 3 already give LOW and HIGH, so the internal pull-up is not needed:
pinMode(SLIDE_SWITCH_PIN, INPUT);
  • Read where the handle is. HIGH means toward pin 3 (ON), LOW means toward pin 1 (OFF):
int handleLevel = digitalRead(SLIDE_SWITCH_PIN);
  • Filter the bounce: when a reading is different from the saved one, wait 50 ms, read again, and keep the new value only if it is still different.

Arduino Giga R1 WiFi Code - Slide Switch

The sketch prints the handle position once, right after it starts. Then it watches D7 and prints a numbered line each time the handle really moves. The number helps you check that one move gives exactly one line.

/* * This Arduino Giga R1 WiFi code was developed by newbiely.com * * This Arduino Giga R1 WiFi code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-giga/arduino-giga-r1-wifi-slide-switch */ const int SLIDE_SWITCH_PIN = 7; // Giga R1 WiFi digital pin 7 (D7), wired to COM (the middle pin) of the slide switch const unsigned long BOUNCE_TIME = 50; // milliseconds to wait while the contacts settle after a move int knownState; // the last position we trust: HIGH or LOW int changeCount = 0; // how many real moves of the handle we have seen void showState(int state) { if (state == HIGH) // handle toward pin 3: COM is joined to 3.3V Serial.println("The slide switch is ON"); else // handle toward pin 1: COM is joined to GND Serial.println("The slide switch is OFF"); } void setup() { Serial.begin(9600); pinMode(SLIDE_SWITCH_PIN, INPUT); // plain INPUT: pin 1 (GND) and pin 3 (3.3V) always give a clear level knownState = digitalRead(SLIDE_SWITCH_PIN); // the handle position when the sketch starts Serial.print("[startup] "); showState(knownState); } void loop() { int newState = digitalRead(SLIDE_SWITCH_PIN); if (newState != knownState) { // The level changed. It can be a real move, or just contact bounce. delay(BOUNCE_TIME); // let the contacts settle newState = digitalRead(SLIDE_SWITCH_PIN); // check once more after the wait if (newState != knownState) { // still different, so the handle really moved changeCount++; knownState = newState; // save the new position Serial.print("[change "); Serial.print(changeCount); Serial.print("] "); showState(knownState); } } }

Detailed Instructions

Follow these instructions step by step:

  • Is this your first sketch on the Giga? Install its board package with the Arduino Giga R1 WiFi getting started guide first.
  • Wire the components according to the provided diagram: pin 1 to GND, COM to D7, pin 3 to 3.3V.
  • Connect the Giga R1 WiFi to a USB port of your computer with the USB Type-C cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Giga R1 WiFi board (e.g., Arduino Giga R1 WiFi) and COM port.
  • Copy the code above into a new sketch and click Upload.
  • Open the Serial Monitor and set the speed to 9600 baud.
  • Slide the handle from end to end six times and watch the lines appear.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Giga R1
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Giga R1' on 'COM15')
New Line
9600 baud
[startup] The slide switch is OFF [change 1] The slide switch is ON [change 2] The slide switch is OFF [change 3] The slide switch is ON [change 4] The slide switch is OFF [change 5] The slide switch is ON [change 6] The slide switch is OFF
Ln 11, Col 1
Arduino Giga R1 on COM15
2

Six moves give six "change" lines, and the counter never jumps by two, so the bounce is filtered. If you leave the handle alone, the screen stays quiet. The sketch prints only when something changes.

The "[startup]" line comes only once, at the very start of the sketch. The Giga talks to your computer through its own USB port, and that port reconnects after each upload. If the Serial Monitor was not ready yet, this first line is lost. That is not a problem: the next "change" line tells you where the handle is. If you see ON while the handle points to pin 1, check that pin 1 goes to GND and pin 3 to 3.3V, not the other way round.

Arduino Giga R1 WiFi Code - Slide Switch on Two Wires (INPUT_PULLUP)

Want fewer wires, or does a sensor already use the 3.3V pin? Then let the Giga do the job of pin 3 by itself. Every GPIO pin of the STM32H747 chip has a pull-up resistor inside, and INPUT_PULLUP turns it on. Just remove the pin 3 wire. COM stays on D7, pin 1 stays on GND, and pin 3 is left free.

The wiring diagram between Arduino Giga R1 WiFi slide switch INPUT_PULLUP

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Giga R1 WiFi Pin
Pin 1 GND
Pin 2 (COM) D7
Pin 3 not connected

Be careful: HIGH and LOW now mean the opposite of before.

  • Handle toward pin 1: COM is joined to GND, so D7 reads LOW. The sketch prints ON.
  • Handle toward pin 3: COM touches a free pin, so nothing drives D7. The internal pull-up holds it HIGH, and the sketch prints OFF.
/* * This Arduino Giga R1 WiFi code was developed by newbiely.com * * This Arduino Giga R1 WiFi code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-giga/arduino-giga-r1-wifi-slide-switch */ const int SLIDE_SWITCH_PIN = 7; // Giga R1 WiFi digital pin 7 (D7), wired to COM (the middle pin) of the slide switch const unsigned long BOUNCE_TIME = 50; // milliseconds to wait while the contacts settle after a move int knownState; // the last position we trust: HIGH or LOW int changeCount = 0; // how many real moves of the handle we have seen void showState(int state) { // Only two wires are used (pin 3 is free), so the logic is upside down: // handle toward pin 1 -> COM is joined to GND -> LOW -> ON // handle toward pin 3 -> COM is open, the pull-up holds the pin -> HIGH -> OFF if (state == LOW) Serial.println("The slide switch is ON"); else Serial.println("The slide switch is OFF"); } void setup() { Serial.begin(9600); // INPUT_PULLUP turns on the pull-up resistor inside the STM32H747 chip. // It keeps pin 7 HIGH while COM is not joined to anything. pinMode(SLIDE_SWITCH_PIN, INPUT_PULLUP); knownState = digitalRead(SLIDE_SWITCH_PIN); // the handle position when the sketch starts Serial.print("[startup] "); showState(knownState); } void loop() { int newState = digitalRead(SLIDE_SWITCH_PIN); if (newState != knownState) { // The level changed. It can be a real move, or just contact bounce. delay(BOUNCE_TIME); // let the contacts settle newState = digitalRead(SLIDE_SWITCH_PIN); // check once more after the wait if (newState != knownState) { // still different, so the handle really moved changeCount++; knownState = newState; // save the new position Serial.print("[change "); Serial.print(changeCount); Serial.print("] "); showState(knownState); } } }

Detailed Instructions

  • Unplug the wire between pin 3 of the switch and the 3.3V pin. Leave the handle toward pin 1.
  • Upload this sketch in the same way as the first one.
  • Keep the Serial Monitor at 9600 baud, then slide the handle three times: toward pin 3, back toward pin 1, and toward pin 3 again.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Giga R1
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Giga R1' on 'COM15')
New Line
9600 baud
[startup] The slide switch is ON [change 1] The slide switch is OFF [change 2] The slide switch is ON [change 3] The slide switch is OFF
Ln 11, Col 1
Arduino Giga R1 on COM15
2

Compare these lines with the first test. With the handle toward pin 1, the 3-wire sketch printed OFF, but this sketch prints ON. With the handle toward pin 3, it is the other way round. Both are right, because the wiring is different.

Which version should you use? The 3-wire one reads the natural way, with HIGH as ON. The 2-wire one needs fewer wires, and it keeps the 3.3V pin free for other parts of a big Giga project.

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!