DEV Community

Cover image for Raspberry Pi Pico - Switch
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Raspberry Pi Pico - Switch

A toggle switch is different from a push button: flip it once and it stays in that position until you flip it back. This guide wires a two-pin ON/OFF switch to a Raspberry Pi Pico and reads its state in MicroPython, which is handy anytime you want a persistent on/off control instead of a momentary press.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi Pico - Switch

What you need

Wiring

Raspberry Pi Pico - Switch wiring diagram

The switch used here has two pins, and it doesn't matter which pin goes where. Connect it to the Pico following the wiring diagram; the code expects it on GP1. You can build this on a breadboard or wire it directly without one. If you solder the wires to the switch pins, cover the joints with heat shrink tubing for protection.

How it works

An ON/OFF switch moves from one position to the other when you press it, and it stays there until you press it again, unlike a momentary button that springs back. There are two common ways to wire this kind of switch, each giving a different HIGH/LOW reading on the Pico; this guide picks one of them and sticks with it for the wiring diagram and code.

Code

The sketch uses the DIYables_MicroPython_Button library, which adds debouncing and an internal pull-up resistor so you don't have to handle that logic yourself. The code creates a Button on GP1, sets a 50 millisecond debounce time, and in a loop calls switch.loop() to update the state, then switch.get_state() to check it: a HIGH reading prints that the switch is OFF, anything else prints ON.

raspberry-pi-pico-switch.py

"""
This Raspberry Pi Pico MicroPython code was developed by newbiely.com
This Raspberry Pi Pico code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-switch
"""

from DIYables_MicroPython_Button import Button
import time

# Initialize the  on/off switch connected to GPIO pin GP1 of the Raspberry Pi Pico
switch = Button(1)

# Set debounce time to 50 milliseconds
switch.set_debounce_time(50)

while True:
    switch.loop()  # Update the button state

    state = switch.get_state()
    if state == 1:  # GPIO HIGH
        print("The switch: OFF")
    else:
        print("The switch: ON")
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

Like a push button, a toggle switch can produce noisy, rapidly flickering signals right when it changes position, so skipping debouncing can make your code see several false transitions instead of one clean flip. The library's built-in debounce timer and pull-up resistor handle this for you, so if readings still look unstable, double check the wiring matches the diagram and the switch is connected to GP1 as the code expects.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-switch

Top comments (0)