Raspberry Pi Pico - Switch

In this guide, we will learn how to use the ON/OFF switch with a Raspberry Pi Pico. We will cover:

Raspberry Pi Pico ON/OFF Switch

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Wires
1×ON/OFF Square Switch
1×(Alternative) ON/OFF Round Switch
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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 ON/OFF Switch

A switch marked ON/OFF moves from ON to OFF, or from OFF to ON, when pressed. It remains in the new position after you release it. Press it once more to switch it back.

Pinout

There are two primary kinds of ON/OFF switches: the two-pin switch and the three-pin switch.

In this guide, we use a switch with two pins. You do not need to worry about which pin is which.

ON/OFF Switch Pinout

How It Works

Here are two ways to use an ON/OFF switch. Below, you will find the wiring instructions for the ON/OFF switch and the status readings on the Raspberry Pi Pico for each way:

pin 1 pin 2 Raspberry Pi Pico Input Pin's State
1 GND Raspberry Pi Pico Input Pin (with pull-up) HIGH OFF, LOW ON
2 VCC Raspberry Pi Pico Input Pin (with pull-down) HIGH ON, LOW OFF

We just have to choose one of the two methods. The rest of the guide will use the first method.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

We recommend using a Soldering Iron to securely solder the wires and the ON/OFF switch pin. After that, cover them with Heat Shrink Tube for protection.

Raspberry Pi Pico Code - ON/OFF Switch

An ON/OFF switch, similar to a button, requires debouncing. Debouncing can make the code more complex. Fortunately, the DIYables_MicroPython_Button library helps by providing a debouncing feature and includes an internal pull-up resistor, which makes programming easier.

※ NOTE THAT:

There are two use cases:

  • First: If the switch is ON, do something. If the switch is OFF, do the opposite.
  • Second: If the switch goes from ON to OFF or from OFF to ON, do something.

Let's learn how to program Raspberry Pico one by one.

Raspberry Pi Pico - Read state of ON/OFF Switch

""" 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 (similar to the Arduino code) 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")

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.
  • Connect the ON/OFF switch to the Raspberry Pi Pico according to the provided diagram.
  • 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).
  • On Thonny IDE, navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-Button”, then find the Button library created by DIYables.
  • Click on DIYables-MicroPython-Button, then click Install button to install Button library.
  • 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
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Move the switch to the ON position.
  • Check out the message in the Shell at the bottom of Thonny.
  • Switch to the OFF position.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The switch: OFF The switch: OFF The switch: OFF The switch: OFF The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: OFF The switch: OFF The switch: OFF
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Raspberry Pi Pico - Detect Event from ON/OFF Switch

""" 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 (similar to the Arduino code) switch.set_debounce_time(50) while True: switch.loop() # Update the button state if switch.is_pressed(): print("The switch: OFF -> ON") if switch.is_released(): print("The switch: ON -> OFF")

Detailed Instructions

  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico
  • Move switch between ON/OFF several time.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The switch: OFF -> ON The switch: ON -> OFF The switch: OFF -> ON The switch: ON -> OFF
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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!