Arduino MicroPython Rocker Switch Module

Flip a rocker switch once, and you hear one click. A microcontroller that watches the pin very closely can see more than that: ON, OFF, ON, all within a few milliseconds, because the metal contacts bounce before they come to rest. On this page, an Arduino Giga R1 WiFi runs MicroPython and reads the DIYables ON/OFF Rocker Switch Module. The first script reports the rocker position. The second script keeps a flip counter, and a simple 30 ms rule makes sure that one click always counts as one flip. Here is the plan:

Arduino Giga R1 WiFi with MicroPython reading a DIYables ON/OFF rocker switch module wired to D2

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×ON/OFF Rocker Switch Module
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 the Rocker Switch Module and Its Pins

The module has two layers. On the top layer sits a KCD1 rocker switch in black plastic, a panel switch that many mains appliances use. Under it is a blue circuit board, close to 24 mm square. Four screw holes let you fix it inside the lid of a project box. You will find an "I" printed at one end of the rocker and an "O" at the other. These are the usual symbols for ON and OFF: with the "I" end down, the switch is ON, and with the "O" end down, it is OFF. Two labels on the blue board repeat this: "ON (H)" and "OFF (L)". So "I" gives a HIGH signal, and "O" gives a LOW one. One DIYables pack holds five modules.

Electricians call this a maintained switch. Makers often say latching switch. Either way, it stays put when you let go, like the light switch on a wall. A push button springs back: release it, and it is OFF again. So this module gives the program a position to check, not a press to catch. The code checks the position and reacts when it moves to the other end.

Rocker switch module pinout: SIG on top, VCC in the middle, GND at the bottom of the 3-pin header
image source: diyables.io

The three pins leave the board sideways, bent at 90 degrees, 2.54 mm apart. Look down on the module from above and turn it so the pins point to the right. Now read the pins from top to bottom: SIG first, VCC in the middle, GND last. The labels are on the underside, and there you read them the other way round (GND, VCC, SIG). On that side, the GND pad is square and the other two are round.

  • SIG pin: the output of the module. It goes to a digital input of the Arduino. We use D2.
  • VCC pin: the supply. The module works from 3.3V to 5V, but on the Giga it must go to 3.3V.
  • GND pin: the ground. Any GND pin of the Giga will do.

What value() returns on D2:

  • Rocker on "I" (ON): the contact closes and connects SIG to VCC, so you read 1.
  • Rocker on "O" (OFF): a 10 kOhm resistor, printed "103", is fitted on the board and keeps SIG at ground level, so you read 0.

The pin never floats, and 1 means ON, so there is nothing to invert. Do not enable Pin.PULL_UP here. An extra resistor is not needed either.

DIYables ON/OFF Rocker Switch Module
Rocker KCD1, SPST, ON/OFF
Behavior maintained (latching)
Symbols I = ON, O = OFF
Pins SIG, VCC, GND (3-pin header, 2.54 mm, right-angle)
Power 3.3V to 5V on VCC
SIG output ON gives HIGH (the VCC level), OFF gives LOW (0V)
Resistor 10 kOhm pull-down, already fitted
Board 24 x 24 mm (approx.), 4 screw holes
Overall size about 24 x 29 x 22 mm (header counted)

※ NOTE THAT:

In the "I" position, SIG is simply connected to VCC and carries the same voltage. The pins of the Arduino Giga R1 WiFi work at 3.3V. With VCC on 5V or VIN, every "I" would push 5V into D2. The pin is not made for that and could be damaged. Take VCC from the 3.3V pin only.

The KCD1 also carries the text "6A 250VAC / 10A 125VAC". It shows the limit of the switch part on its own, for example inside a lamp or a heater. Here, the current through it is tiny: just a logic signal for D2. The module is for logic signals only, so keep mains voltage and motor current away from its header.

Last, a fact that matters for the second script. When the rocker snaps over, the contacts inside hit, jump apart and hit again a few times. This lasts only a few milliseconds, but during that time the pin can read 1, 0, 1, 0. A loop that reads twice per second almost never catches it, and one odd line would not hurt there. A loop that counts changes catches it often.

Wiring Diagram

The wiring is three jumper wires from the module to the Giga. Nothing goes in between: no breadboard, no resistor.

The wiring diagram between Arduino MicroPython rocker switch module

This image is created using Fritzing. Click to enlarge image

Rocker Switch Module Arduino Giga R1 WiFi
SIG D2
VCC 3.3V (never 5V)
GND GND

How To Program the Rocker Switch Module in MicroPython

Both scripts use only the machine and time modules, which come with MicroPython. No library is needed.

  • Make D2 a plain input. The module brings its own pull-down, so leave out the pull argument:
rocker = Pin('D2', Pin.IN)
  • Read the rocker. The result is 1 on "I" and 0 on "O":
sig = rocker.value()
  • For the second script: find out how long the level has stayed the same. time.ticks_diff() gives the right answer even when the millisecond counter rolls over:
steady_ms = time.ticks_diff(time.ticks_ms(), last_change_ms)

Arduino MicroPython Code - Show the Rocker Position

This first script is your wiring test. Every half second, it prints one line: which end of the rocker is down, the raw value, and what that value means.

""" 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-rocker-switch-module """ from machine import Pin import time ROCKER_PIN = 'D2' # The Arduino Giga WiFi pin D2 connected to the SIG pin of the rocker switch module # A 10k pull-down resistor is already on the module, so a plain input is enough. # SIG is 1 when the rocker is pushed to "I" (ON) and 0 when it is pushed to "O" (OFF). rocker = Pin(ROCKER_PIN, Pin.IN) while True: sig = rocker.value() # Read the SIG pin: 1 or 0 if sig == 1: print("Rocker on I | SIG = 1 | ON") else: print("Rocker on O | SIG = 0 | OFF") time.sleep_ms(500) # Two lines per second are enough for a switch that stays where you put it

Detailed Instructions

Follow these steps to run the script on the Arduino Giga with Thonny IDE:

  • Have Thonny IDE installed on your computer.
  • Have MicroPython firmware installed on the Arduino Giga R1 WiFi.
  • Never used MicroPython on this board? The Getting Started with Arduino and MicroPython tutorial covers both.
  • Wire the module: D2 takes SIG, 3.3V takes VCC, and a GND pin takes GND.
  • Use the USB Type-C cable to link the Giga to your computer.
  • In Thonny IDE, click Tools Options.
  • Open the Interpreter tab and choose MicroPython (generic) in the dropdown menu.
  • Select the port of the Giga (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the script above into Thonny's editor.
  • Store the script on the board:
    • Press Ctrl+S or click the Save button.
    • When Thonny asks where to save, choose MicroPython device and enter main.py as the name.
  • Start it with the green Run button (or F5).
  • Leave the rocker on "O" at first. Then push it to "I", wait about two seconds, and push it back to "O". Watch the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Rocker on O | SIG = 0 | OFF Rocker on O | SIG = 0 | OFF Rocker on O | SIG = 0 | OFF Rocker on I | SIG = 1 | ON Rocker on I | SIG = 1 | ON Rocker on I | SIG = 1 | ON Rocker on I | SIG = 1 | ON Rocker on O | SIG = 0 | OFF Rocker on O | SIG = 0 | OFF
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

You let go of the rocker, but the Shell keeps printing ON. That is the latching action at work: the switch holds its position, so the board reads the same state again and again. Always OFF, whatever the rocker does? Look at the SIG wire on D2 and the VCC wire on 3.3V. Always ON? Maybe the SIG wire went into the 3.3V pin by mistake.

Arduino MicroPython Code - Count Each Real Flip With a 30 ms Debounce

Printing the state twice per second is fine for a quick test. But many projects want to act once, at the moment of the flip: count it, log it, start or stop something. For that, the loop must run fast, and a fast loop sees the bounce.

The script below filters the bounce out with a small timer. Each time the raw level on D2 changes, the script writes down the time with time.ticks_ms(). A new level wins only when 30 ms pass with no further change. Bounces are over within a few milliseconds, so they keep restarting the timer and never get counted. When a flip passes the test, the script prints "Switch ON" or "Switch OFF" once, with the flip count and the time since the start.

""" 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-rocker-switch-module """ from machine import Pin import time ROCKER_PIN = 'D2' # The Arduino Giga WiFi pin D2 connected to the SIG pin of the rocker switch module STABLE_MS = 30 # A new level must stay the same for 30 ms before we accept it rocker = Pin(ROCKER_PIN, Pin.IN) # No pull needed: the module has its own pull-down resistor start_ms = time.ticks_ms() # The time when the script started accepted = rocker.value() # The level we trust (1 = ON, 0 = OFF) last_raw = accepted # The raw level from the previous pass of the loop last_change_ms = start_ms # The time when the raw level changed the last time flip_count = 0 # Real flips counted so far if accepted == 1: print("At start: Switch ON") else: print("At start: Switch OFF") while True: raw = rocker.value() now = time.ticks_ms() if raw != last_raw: # The level just moved. It can be a real flip or only a bounce, # so start the 30 ms timer again. last_raw = raw last_change_ms = now # Accept the level only when it is new AND it has not moved for STABLE_MS if raw != accepted and time.ticks_diff(now, last_change_ms) >= STABLE_MS: accepted = raw flip_count += 1 run_time = time.ticks_diff(now, start_ms) # Milliseconds since the script started if accepted == 1: print("Switch ON | flip count:", flip_count, "| at", run_time, "ms") else: print("Switch OFF | flip count:", flip_count, "| at", run_time, "ms") time.sleep_ms(1) # Check the pin about once per millisecond

Detailed Instructions

  • Do not change the wiring.
  • Replace the old code in Thonny's editor with this script.
  • Save it to the Giga as main.py again. It overwrites the first script.
  • Press F5 to run it.
  • Flip the rocker five times, some slowly and some quickly.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot At start: Switch OFF Switch ON | flip count: 1 | at 2871 ms Switch OFF | flip count: 2 | at 5406 ms Switch ON | flip count: 3 | at 6152 ms Switch OFF | flip count: 4 | at 6718 ms Switch ON | flip count: 5 | at 9934 ms
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Five flips, five lines, and the count goes up by exactly one each time. Each line also comes about 30 ms after the real flip, but that is far too short for you to notice.

Want to see why the check is there? Set STABLE_MS to 0, save, and flip the rocker a few times. Now and then, one flip may add two or three to the count, because some bounces are counted as flips. Put 30 back when you are done.

This method does not block. The time.sleep_ms(50) trick on the Arduino MicroPython Button Module page stops the whole loop while it waits. Here, the loop keeps running during the 30 ms, so there is room for more tasks in the same loop later. The Arduino MicroPython Button Debounce tutorial uses the same timer idea for push buttons.

Rocker Switch Module or a Bare ON/OFF Switch?

The Arduino MicroPython Switch tutorial uses a bare ON/OFF switch with two legs. Both switches keep their position, but they are wired and read in a different way:

  • The bare switch comes without a resistor, so the code turns on the Giga's internal pull-up. ON reads as 0 there, which is the reverse of what you might expect. With the module, 1 simply means ON.
  • The bare switch has two unlabeled legs, and the wires are often soldered on. The module has three named header pins for jumper wires.
  • Both need debouncing when the code counts changes. The 30 ms timer on this page works for both.

If your input should spring back after each press, like a "next" or "start" key, use the Arduino MicroPython Button Module instead.

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!