Raspberry Pi Pico - Button Module

A Pico project often needs one simple way for a person to talk to it: a start key for a timer, a "next song" key for a music box, or a fire key for a small game. A button module is the quickest way to add that key. You plug three wires into the Pico, and the board tells you 1 or 0. There is no resistor to pick and no four-leg button to turn the right way.

In this guide, we will learn to set up and use the Button Module with the Raspberry Pi Pico. We will cover the following details:

Raspberry Pi Pico and button module

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×Button Module
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

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 Button Module

Think of the button module as a push button that brings its own resistor. The small board holds a momentary tactile switch with a colored cap and a pull-down resistor. While nobody touches the cap, the resistor holds the OUT pin at 0. While the cap is held down, the switch joins OUT to VCC, so OUT reads 1. When you let go, it goes back to 0. On the Pico, this means value() gives you 1 for "pressed" and 0 for "released", with no upside-down logic.

The DIYables button module is sold as a set of five, one in each color: red, yellow, green, blue and white. Use different colors for different jobs, for example green for start and red for stop.

Button Module
Switch Type momentary tactile push button with cap
Cap Colors red, yellow, green, blue, white
Operating Voltage 3.3V to 5V DC
Output OUT = 0 when released, 1 when pressed
Pull Resistor pull-down, already on the board
Header 3-pin, 2.54mm pitch

Pinout

Read the labels on the module from left to right: VCC, OUT, GND.

  • VCC pin: connect to the 3.3 volts pin of the Pico. When the cap is pressed, this same voltage comes out of the OUT pin.
  • OUT pin: connect to a Pico GPIO pin. It gives 1 (3.3 volts) when pressed and 0 (0 volts) when released.
  • GND pin: connect to GND (0 volts).
Button module pinout
image source: diyables.io

One more thing to know about any push button: the metal contacts inside do not close cleanly. For a few milliseconds they bounce, so a single press can look like several quick presses to a fast chip like the Pico. The second and third examples below show two ways to handle this.

If you have used the bare 4-pin button from our Raspberry Pi Pico - Button tutorial, you will notice two changes. The bare button needs Pin.PULL_UP (or your own resistor), and then it reads 0 when pressed. Its four legs are also easy to place wrong on a breadboard. The module has labeled pins and its own pull-down, so a press reads 1. Do not turn on Pin.PULL_UP for the module, because it would work against the pull-down on the board.

Wiring Diagram

The OUT pin goes to GP0, which is pin 1 at the top-left corner of the Pico, next to the USB connector. The GND wire can go to pin 3, the ground pin just below GP1. Power comes from 3V3(OUT), pin 36.

The wiring diagram between Raspberry Pi and Pico button module

This image is created using Fritzing. Click to enlarge image

Button Module Raspberry Pi Pico
VCC 3V3(OUT) (pin 36)
OUT GP0 (pin 1)
GND GND (pin 3)

※ NOTE THAT:

Use only the 3V3(OUT) pin for VCC. Do not use VBUS (pin 40) or VSYS (pin 39). Those pins carry about 5 volts from USB, and the module would send that 5 volts straight into GP0 when you press the cap. The Pico's GPIO pins are made for 3.3 volts, and 5 volts can damage them.

Raspberry Pi Pico Code - Read the Raw State

The first script is the simplest possible check. It sets GP0 as a plain input, with no pull resistor from the Pico, and prints what it reads four times per second. This is a good way to confirm the wiring before you write any real project code.

""" 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-button-module """ from machine import Pin import time # GP0 is wired to the OUT pin of the button module OUT_PIN = 0 # Plain digital input, no pull resistor from the Pico. # The module already has a pull-down resistor on its board. key = Pin(OUT_PIN, Pin.IN) while True: reading = key.value() # 1 while the cap is held down, 0 when it is free if reading: print("GP0 =", reading, " the button is held down") else: print("GP0 =", reading, " the button is up") time.sleep(0.25) # Four lines per second keeps the Shell readable

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.
  • Place the button module on the breadboard and run the three wires as shown in the diagram. Check twice that VCC goes to 3V3(OUT).
  • 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
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Leave the button alone for a moment, then hold the cap down for about one second, then let it go.
  • Read the lines in the Shell at the bottom of Thonny:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot GP0 = 0 the button is up GP0 = 0 the button is up GP0 = 0 the button is up GP0 = 1 the button is held down GP0 = 1 the button is held down GP0 = 1 the button is held down GP0 = 1 the button is held down GP0 = 0 the button is up GP0 = 0 the button is up
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

The value stays at 0 when the button is idle, without any random jumps. That is the module's pull-down at work. If you see 1 all the time, OUT may be touching VCC. If you see 0 even while pressing, check the VCC wire.

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 - Press and Release Events with sleep_ms()

Printing the level many times per second is fine for a test, but a real project wants one message per action, for example "start the timer" exactly once per press. This script remembers the last trusted state. When GP0 shows a different value, it waits 50 ms for the bounce to end, reads GP0 again, and only reports the change if it is still there. Keep in mind that time.sleep_ms() stops the whole script during those 50 ms, so nothing else can run in that time.

""" 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-button-module """ from machine import Pin import time OUT_PIN = 0 # GP0 reads the OUT pin of the button module BOUNCE_MS = 50 # How long we let the metal contacts settle key = Pin(OUT_PIN, Pin.IN) # The module's own pull-down keeps GP0 at 0 when idle stable_state = key.value() # The last state we trusted while True: if key.value() != stable_state: # Something changed. It may be real, or just contact bounce. # sleep_ms() pauses the whole script for 50 ms here. time.sleep_ms(BOUNCE_MS) new_state = key.value() # Look again after the bounce is over if new_state != stable_state: stable_state = new_state if stable_state == 1: print("The button is pressed") else: print("The button is released")

Detailed Instructions

The wiring does not change:

  • Click the red Stop button in Thonny to end the first script.
  • Replace the code in the editor with the script above.
  • Save it to the Raspberry Pi Pico as main.py.
  • Click the green Run button (or press F5).
  • Give the button three short taps.
  • Each tap now gives exactly two lines in the Shell:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot The button is pressed The button is released The button is pressed The button is released The button is pressed The button is released
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

To compare, the Raspberry Pi Pico - Button - Debounce tutorial shows what happens with no debounce at all: one press can print several "pressed" lines. If you still get a double line now and then, raise BOUNCE_MS to 80 or 100.

Raspberry Pi Pico Code - Non-blocking Debounce with a Library

The sleep method is short and easy, but the Pico is stuck while it waits. That becomes a problem when the same script also has to blink an LED, update a display or read a sensor. The DIYables-MicroPython-Button library solves this. Its loop() function looks at GP0 and returns right away, and it keeps track of the bounce time with the Pico's millisecond clock. You just call loop() on every pass and then ask:

  • is_pressed(): True one time, right after a confirmed press.
  • is_released(): True one time, right after a confirmed release.
  • get_state(): the steady state now, 1 for pressed and 0 for released.

We create the button with Button(0, Pin.PULL_DOWN). The Pin.PULL_DOWN mode tells the library that 1 means pressed, which matches the module. The library also turns on the Pico's internal pull-down, which is harmless here because it pulls the same way as the resistor on the module.

""" 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-button-module """ from machine import Pin from DIYables_MicroPython_Button import Button OUT_PIN = 0 # GP0 reads the OUT pin of the button module # Pin.PULL_DOWN matches the module: 1 = pressed, 0 = released key = Button(OUT_PIN, Pin.PULL_DOWN) key.set_debounce_time(50) # Ignore changes shorter than 50 ms while True: key.loop() # Must run on every pass. It checks the pin and returns at once. if key.is_pressed(): print("The button is pressed (steady state:", key.get_state(), ")") if key.is_released(): print("The button is released (steady state:", key.get_state(), ")")

Detailed Instructions

  • Stop the script that is running in Thonny.
  • 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.
Raspberry Pi Pico Button library
  • Copy the above code into the editor and save it to the Raspberry Pi Pico as main.py.
  • Click the green Run button (or press F5).
  • Press the button once quickly, then press it again and hold it for two seconds before you let go.
  • The Shell shows one line for each action, and it does not matter how long you hold:
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot The button is pressed (steady state: 1 ) The button is released (steady state: 0 ) The button is pressed (steady state: 1 ) The button is released (steady state: 0 )
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Because loop() never waits, you can add other work inside the while True: loop, as long as that work does not sleep for a long time itself. For more ideas, see Raspberry Pi Pico - Button - LED, where a press switches an LED. The button module works in that project too: wire it as shown on this page and use Pin.PULL_DOWN mode with "pressed = 1".

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!