Raspberry Pi Pico - Rocker Switch Module
Some choices in a project should stay set until you change them on purpose. A rocker switch is made for exactly that job. It is the black key that tips to one side or the other on extension leads, desk fans and the back of many PC cases, marked "I" at one end and "O" at the other end. The DIYables ON/OFF Rocker Switch Module places such a switch on a tiny board with three named header pins, which lets the Raspberry Pi Pico read it on a single GPIO pin. On this page, the rocker first shows its position in the Thonny Shell. Then it becomes the start and pause key of a small temperature logger: on "I", the Pico writes the temperature of its own chip every 2 seconds, and on "O", it takes a break.
This guide sets up the rocker switch module on a Raspberry Pi Pico, one step at a time. Here is what we will cover:
- How to tell the "I" end from the "O" end, and where each of the three pins (SIG, VCC, GND) goes.
- How to join the module to GP22 and 3V3(OUT) of the Pico with three jumper wires.
- How to show the switch position, ON or OFF, in the Thonny Shell with MicroPython.
- How to start, pause and resume a log of the Pico's chip temperature with the switch, using a 30 ms debounce that never stops the loop.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of ON/OFF Rocker Switch Module
The module is built from two parts. On top sits a black KCD1 rocker switch. Under it is a little blue circuit board of roughly 24 by 24 mm. Four mounting holes let you screw the board behind a cut-out, for example in the lid of a plastic case. The "I" end of the rocker stands for ON, and the "O" end stands for OFF. Beside the switch, the board repeats this as text: "ON (H)" and "OFF (L)". So SIG is high while the switch is ON and low while it is OFF. One pack holds five modules.
Switches like this one are called maintained, or latching. Press one half down, it clicks, and the rocker keeps that position for hours or for years, until a finger presses the other half. A push button, like the one on the button module, jumps back up as soon as you lift your finger. This changes the way we write the code. For a push button, a script waits for a press. For the rocker, a script asks "where is the switch right now?" and reacts when the answer changes from ON to OFF, or from OFF to ON.
Pinout
The three header pins sit 2.54 mm apart and are bent 90 degrees, so the wires join the board sideways. Hold the module so that you look down on the rocker, with the pins pointing right. The top pin is SIG, the middle one is VCC and the bottom one is GND. The names themselves sit on the underside, near the pins. Turn the board over and they read GND, VCC, SIG, and the square solder pad marks GND.
- SIG pin: connect to a free GPIO pin of the Pico (we use GP22). It gives 3.3 volts (a 1) while "I" is pressed down, and 0 volts (a 0) while "O" is pressed down.
- VCC pin: connect to 3V3(OUT), the 3.3 volts output of the Pico. The module itself works from 3.3 volts to 5 volts, but on the Pico, give it 3.3 volts only.
- GND pin: connect to any GND pin of the Pico (0 volts).

| ON/OFF Rocker Switch Module | |
|---|---|
| Switch | KCD1 rocker, SPST, ON/OFF |
| Action | maintained (latching) |
| Rocker Marks | I = ON, O = OFF |
| Supply Voltage | 3.3V to 5V DC |
| SIG Output | same voltage as VCC when ON, 0V when OFF |
| Pull-down Resistor | 10 kOhm, on the board (marked 103) |
| Header | 3 pins, 2.54mm pitch, right-angle |
| Board Size | about 24 x 24mm, 4 mounting holes |
| Size with Pins | about 24 x 29 x 22mm |
You will not find Pin.PULL_DOWN or Pin.PULL_UP in the scripts below. The board already has a 10 kOhm pull-down resistor, the tiny part marked "103". It ties SIG to 0 volts while the switch is open, so GP22 reads a clean 0 instead of random values, and Pin.IN alone is all we need. Leave Pin.PULL_UP off as well. The Pico's own resistor would then pull GP22 up while the resistor on the module pulls it down.
The body of the switch shows "6A 250VAC / 10A 125VAC". These numbers tell you what the bare switch can handle inside a mains device. On this module, the switch does a much smaller job: it passes a 3.3 volt signal to one GPIO pin. The header pins are for a logic signal only, so never wire mains or a motor to them.
When the rocker snaps over, its metal contacts touch, jump apart and touch again for a few milliseconds. A loop on the Pico checks GP22 much faster than that, so it may see one flip as several quick flips. The first script does not care, because it looks at the pin only a few times per second. The second script reacts to each change, so it accepts a new level only after the level has stayed the same for 30 ms.
Maybe you already know the bare 2-pin switch from the Raspberry Pi Pico - Switch tutorial. That switch has no resistor of its own, so its script uses a pull-up resistor inside the Pico, and the reading is inverted: a 0 stands for ON and a 1 for OFF. The module turns this the right way around. ON is 1, OFF is 0, and every pin has a printed name, so the wiring is hard to get wrong. If you need a key that returns by itself after each press instead, see Raspberry Pi Pico - Button Module.
Wiring Diagram
In the drawing, the Pico is plugged into a breadboard. The rocker switch module stays beside the breadboard, and three jumper wires link its header with the breadboard holes beside the Pico pins. All three wires go to the same long side of the Pico: the side where VBUS (pin 40) sits next to the USB connector. Counted from the USB end of that side, GND (pin 38) is the 3rd pin, 3V3(OUT) (pin 36) is the 5th pin and GP22 (pin 29) is the 12th pin. Because every wire comes from one side, no wires cross.

This image is created using Fritzing. Click to enlarge image
| Rocker Switch Module | Raspberry Pi Pico |
|---|---|
| SIG | GP22 (pin 29) |
| VCC | 3V3(OUT) (pin 36) |
| GND | GND (pin 38) |
※ NOTE THAT:
VCC belongs on 3V3(OUT), pin 36. VSYS (pin 39) and VBUS (pin 40) are only a few holes away, and those two pins hold about 5 volts straight from USB. SIG always gives out the same voltage as VCC. So with VCC on one of them, GP22 would see 5 volts each time you press "I". GP22 can take no more than 3.3 volts, so the extra voltage can damage it.
Raspberry Pi Pico Code - Show ON or OFF
Before the switch takes on a task, check the wiring with a tiny script. It sets GP22 as an input and prints the switch state once every 400 ms. Every line tells you three things: ON or OFF, the rocker mark that is pressed down, and the raw value of GP22.
Detailed Instructions
Please follow these instructions step by step:
- Ensure that Thonny IDE is installed on your computer.
- Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
- If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
- Plug the Pico into the breadboard. Then connect the module with three jumper wires, as the table shows: SIG to GP22, VCC to 3V3(OUT), GND to pin 38.
- Connect the Raspberry Pi Pico to your computer using a USB cable.
- Launch the Thonny IDE on your computer.
- On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
- In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
- Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Raspberry Pi Pico by:
- Click the Save button, or use Ctrl+S keys.
- In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
- Save the file as main.py
- Press the "O" side of the rocker down, then click the green Run button (or press F5) to run the script.
- Wait two seconds and press the "I" side. Wait two seconds more and press "O" again.
- Look at the Shell at the bottom of Thonny:
The lines keep coming even when nobody touches the switch. That is normal: a latching switch keeps its state, so the script reports the same state again and again. If the Shell says OFF in both rocker positions, check the VCC wire on 3V3(OUT). If it says ON in both positions, look for a SIG wire that touches the VCC wire or sits in the same breadboard row as 3V3(OUT).
If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset.
Raspberry Pi Pico Code - Temperature Logger Switch
Time to put the switch to work. The RP2040 chip on the Pico has a small temperature sensor built in. MicroPython reads it as channel 4 of the ADC, with machine.ADC(4), so no extra part is needed. This script turns the switch into the start and pause key of a temperature log:
- Rocker on "I": every 2 seconds, the Pico reads the sensor and prints one numbered line, for example [3] 25.2 °C.
- Rocker flipped to "O": the Shell shows "Logging paused" one time, and then the Pico stays quiet.
- Rocker back on "I": the Shell shows "Logging resumed", and the numbers go on from where they stopped.
To get degrees from the sensor, read_u16() first gives a number from 0 to 65535. The script turns it into volts with raw * 3.3 / 65535. Then the formula from the RP2040 datasheet, 27 - (volts - 0.706) / 0.001721, turns the volts into °C.
The debounce in this script never pauses the loop. Each time GP22 shows a new level, the script stores that moment in a variable, using time.ticks_ms(). It only believes the new level after GP22 has kept it for 30 ms. The 2-second timer works the same way, with a "next reading is due at" time. So the Pico can watch the switch and the clock at the same time, and a flip is seen right away, not only every 2 seconds.
※ NOTE THAT:
This sensor measures the temperature of the RP2040 chip, not the air in your room. The chip warms itself a little while it works, so the numbers are often a few degrees above room temperature. The readings also move in steps of about half a degree. To log the room, use an outside sensor such as the DS18B20 from Raspberry Pi Pico - Temperature Sensor, and keep the same switch logic.
Detailed Instructions
The wiring stays the same:
- End the first script: click the red Stop button in Thonny.
- Paste the new script into the editor in place of the old one, and save it to the Pico as main.py.
- Put the rocker on "O", then press F5 (or the green Run button) to start it. The Shell says that the logger is ready.
- Press "I". The log starts, and a new line comes every 2 seconds.
- After four lines, press "O" and wait a few seconds. No new lines come.
- Press "I" again, let three more lines come, then press "O".
Each flip gives exactly one "paused" or "resumed" line, even though the contacts bounce. The values jump between a few numbers, like 24.7, 25.2 and 25.6. That is normal for this sensor: one step of the ADC is worth almost half a degree. If you start the script while "I" is pressed down, the log begins at once, with no flip needed. For a slower log, change LOG_EVERY_MS to 10000, and the Pico writes one line every 10 seconds.