Arduino MicroPython Slide Switch

Some choices in a project should stay fixed until a person changes them: "demo mode" or "normal mode", sound on or sound off, pattern A or pattern B. The DIYables Mini Slide Switch SS12D10 is made for this job. You slide its handle once, and it stays there, even when the power is off. The Arduino Giga R1 WiFi only has to check where the handle is. On this page, MicroPython and Thonny do the work. Here is what we cover:

https://newbiely.com/images/cover/arduino-slide-switch-micropython.jpg

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 and Pinout

The SS12D10 is a very small switch. Its plastic body has a 5 mm handle on top and three metal legs in one row underneath. It is a latching switch: push the handle to one end, and it stays at that end. A push button does the opposite and jumps back the moment you let go. Datasheets call this type SPDT (single pole, double throw) or 1P2T: one common pin that is joined to one of two other pins.

The idea inside is simple. A small metal bridge moves with the handle. It always touches the middle pin, COM, and it also touches the outer pin on the same side as the handle.

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

Nothing is printed on the switch. Just count the legs: the middle one is COM, and the two outer ones are pin 1 and pin 3.

  • Pin 1 (outer pin): wire it to GND of the Arduino.
  • Pin 2, COM (middle pin): the signal pin. Wire it to D2. It carries the voltage of the outer pin that the handle joins it to.
  • Pin 3 (outer pin): wire it to 3.3V of the Arduino Giga.

The two handle positions:

  • 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.

What MicroPython sees on D2 with this wiring:

  • Handle toward pin 3: COM is at 3.3V, so value() returns 1. We call this ON.
  • Handle toward pin 1: COM is at GND, so value() returns 0. We call this OFF.
DIYables Mini Slide Switch SS12D10
Contact type SPDT (1P2T), 2 positions
Pins 3, in one row
Pin pitch 4.7 mm
Terminal length 6.9 mm
Handle 5 mm
Body about 12.8 x 6.7 x 18.2 mm
Mounting vertical, through-hole
Contact material silver-plated brass
Contact rating AC 125V 2A
Switching life over 100,000 cycles
Operating temperature -25°C to 80°C

The AC 125V 2A rating is the limit of the contacts. Here, the switch only sends a logic signal to D2, so the current is tiny.

Like all metal contacts, the bridge bounces for a few milliseconds when the handle moves. In that short moment, a fast loop can read 0, 1, 0, 1. The script below waits 50 ms and reads again before it trusts a change.

Wiring Diagram

This wiring uses three jumper wires and no breadboard. COM is always tied to GND or to 3.3V, so D2 always has a clear level. That is why no pull-up or pull-down resistor is needed, and the code uses a plain Pin.IN.

The wiring diagram between Arduino MicroPython 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) D2
Pin 3 3.3V (not 5V)

※ NOTE THAT:

  • Use the 3.3V pin for pin 3, never 5V or VIN. The Giga R1 WiFi works with 3.3V logic, and when the handle is toward pin 3, COM passes that voltage straight into D2. Some pins of the Giga cannot take 5V at all, so 3.3V is the safe habit for every input.
  • Keep D2 as an input. Do not make it Pin.OUT in another script. The switch ties the pin directly to 3.3V or GND, so an output that drives the other level would be shorted.

How To Program Slide Switch With MicroPython

No library is needed. The machine and time modules that come with MicroPython are enough.

  • Make D2 an input. Leave out the pull argument, because pin 1 and pin 3 already give both levels:
slide_switch = Pin('D2', Pin.IN)
  • Read the handle. You get 1 when it is toward pin 3 (ON) and 0 when it is toward pin 1 (OFF):
state = slide_switch.value()
  • When a reading is not the same as the last one, wait 50 ms and read once more. Only a change that is still there counts as a real move:
if reading != last_state: time.sleep_ms(50) reading = slide_switch.value()

Arduino MicroPython Code - Slide Switch

This script prints one line as soon as it starts, so you know where the handle is. After that, the Shell stays quiet until the handle goes to the other end. Each line also shows the raw value of D2, so you can see which number goes with ON and which with OFF.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-slide-switch """ from machine import Pin import time SLIDE_SWITCH_PIN = 'D2' # The Arduino Giga WiFi pin D2 connected to COM (pin 2) of the slide switch SETTLE_MS = 50 # Time for the contacts to stop bouncing after the handle moves (milliseconds) # Pin 1 of the switch is on GND and pin 3 is on 3.3V. # COM is always joined to one of them, so D2 never floats. A plain input is enough. slide_switch = Pin(SLIDE_SWITCH_PIN, Pin.IN) def print_state(label, value): # 1 = handle toward pin 3 (3.3V) = ON # 0 = handle toward pin 1 (GND) = OFF if value == 1: print(label, "| D2 =", value, "-> The slide switch is ON") else: print(label, "| D2 =", value, "-> The slide switch is OFF") last_state = slide_switch.value() # Where is the handle right now? print_state("At start", last_state) while True: reading = slide_switch.value() if reading != last_state: # The handle just moved, and the contacts may still be bouncing time.sleep_ms(SETTLE_MS) # Wait until they settle reading = slide_switch.value() # Read again after the wait if reading != last_state: # Still different, so the handle really moved last_state = reading print_state("Changed ", last_state)

Detailed Instructions

Here's how to run this MicroPython script on the Arduino Giga with Thonny IDE:

  • Install Thonny IDE on your computer if you do not have it yet.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • New to Arduino with MicroPython? Start with the Getting Started with Arduino and MicroPython tutorial.
  • Wire the switch as in the table: pin 1 to GND, COM to D2, pin 3 to 3.3V.
  • Connect the Arduino Giga to your computer with the USB Type-C cable.
  • In Thonny IDE, open Tools Options.
  • On the Interpreter tab, pick MicroPython (generic) from the dropdown menu.
  • Choose the COM port of your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Paste the code above into Thonny's editor.
  • Save the script on the Arduino:
    • Click the Save button or press Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5).
  • Push the handle to the other end and back, a few times. Stop for about one second at each end, and watch the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot At start | D2 = 0 -> The slide switch is OFF Changed | D2 = 1 -> The slide switch is ON Changed | D2 = 0 -> The slide switch is OFF Changed | D2 = 1 -> The slide switch is ON Changed | D2 = 0 -> The slide switch is OFF Changed | D2 = 1 -> The slide switch is ON
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Five moves give five "Changed" lines. The bounce never shows up as an extra line. If the Shell never shows 1, check the wire from pin 3 to 3.3V. If it never shows 0, check the GND wire on pin 1. If new lines appear while nobody touches the handle, a wire is loose and D2 is floating.

Arduino MicroPython Code - Slide Switch with Pin.PULL_UP (2 Wires)

Inside a small case, every wire counts. Each input pin of the Giga has a pull-up resistor inside the chip, and Pin.PULL_UP turns it on. That resistor can take the place of the 3.3V wire on pin 3. Two wires are then enough: pin 1 to GND and COM to D2.

The wiring diagram between Arduino MicroPython slide switch 2-wire Pin.PULL_UP

This image is created using Fritzing. Click to enlarge image

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

The numbers now mean the opposite:

  • Handle toward pin 1: COM joins D2 to GND. value() returns 0, and the script prints ON.
  • Handle toward pin 3: COM touches only the free pin 3. The pull-up lifts D2 to 1, and the script prints OFF.
""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-slide-switch """ from machine import Pin import time SLIDE_SWITCH_PIN = 'D2' # The Arduino Giga WiFi pin D2 connected to COM (pin 2) of the slide switch SETTLE_MS = 50 # Time for the contacts to stop bouncing after the handle moves (milliseconds) # Only 2 wires: pin 1 on GND, COM on D2, pin 3 not connected. # Pin.PULL_UP turns on the resistor inside the Giga. It holds D2 at 1 # while COM touches nothing, so the pin never floats. slide_switch = Pin(SLIDE_SWITCH_PIN, Pin.IN, Pin.PULL_UP) def print_state(label, value): # The logic is flipped compared with the 3-wire version: # 0 = handle toward pin 1, COM joined to GND = ON # 1 = handle toward pin 3, COM open, pulled up to 1 = OFF if value == 0: print(label, "| D2 =", value, "-> The slide switch is ON") else: print(label, "| D2 =", value, "-> The slide switch is OFF") last_state = slide_switch.value() # Where is the handle right now? print_state("At start", last_state) while True: reading = slide_switch.value() if reading != last_state: # The handle just moved, and the contacts may still be bouncing time.sleep_ms(SETTLE_MS) # Wait until they settle reading = slide_switch.value() # Read again after the wait if reading != last_state: # Still different, so the handle really moved last_state = reading print_state("Changed ", last_state)

Detailed Instructions

  • Just remove the pin 3 wire. The GND wire and the D2 wire stay where they are.
  • Paste the new code into Thonny and save it to the Arduino as main.py, over the old file.
  • Slide the handle toward pin 1, then click Run (F5).
  • Move the handle three more times.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot At start | D2 = 0 -> The slide switch is ON Changed | D2 = 1 -> The slide switch is OFF Changed | D2 = 0 -> The slide switch is ON Changed | D2 = 1 -> The slide switch is OFF
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Look at the first line. In the 3-wire test, 0 meant OFF. Here, 0 goes with ON, because the handle on the pin 1 side now pulls D2 down to GND. Both versions work well. Three wires give the natural "1 = ON" logic, and two wires keep the build smaller.

Which Switch Fits Your Project?

The slide switch is not the only way to give the Arduino Giga an input. A short guide:

  • Pick the slide switch for a small setting that must stay as it is, like a mode or an option on a circuit board.
  • Pick a bigger ON/OFF switch for a panel or the lid of a box, where a finger must reach it easily. See Arduino MicroPython Switch.
  • Pick a limit switch when a moving part, not a person, should press it. See Arduino MicroPython Limit Switch.
  • Pick a button when the input is a short action, like "start now", and must spring back. See Arduino MicroPython Button Module.

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!