Skip to content
systemcoding.dev
Embedded

Read a Button on Raspberry Pi Pico With the Pico SDK

Wire a button to a Raspberry Pi Pico, enable the pull-up with gpio_pull_up and read it with gpio_get in C using Pico SDK 2.0, with CMake and expected output.

6 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Raspberry Pi, Arm and C logos.

A button is the first input every embedded project reads. On a Raspberry Pi Pico it takes one wire, no external resistor and four SDK calls. This post wires a button to GPIO 15 of a Pico (RP2040), reads it with the Pico SDK 2.0 in C, and explains the three things a first-time reader needs to know: why the button reads 0 when pressed, what the internal pull-up does, and why reading it in a loop twice a millisecond can print the same press many times. It includes the CMake file to build it. The functions are from hardware/gpio.h in the SDK, documented in the Raspberry Pi Pico C/C++ SDK manual.

Wiring

A pushbutton has two contacts that connect when pressed. Connect one contact to GPIO 15 (physical pin 20 on the Pico) and the other to a ground pin (physical pin 18 is next to it). Nothing else is needed: the Pico's pins have internal pull-up and pull-down resistors that you enable in software.

Pico pinConnects to
GPIO 15 (pin 20)One side of the button
GND (pin 18)Other side of the button

When the button is open, the pin is connected to nothing. A pin connected to nothing is floating: it picks up noise and reads 0 or 1 at random. The pull-up resistor connects the pin to 3.3 V through a high resistance (the RP2040 datasheet gives about 50 to 80 kilohms), so an unpressed button reads 1. When pressed, the button connects the pin directly to ground, which overpowers the weak pull-up and reads 0. The wiring is therefore active low: pressed is 0.

The four SDK calls

button.cc
#include <stdio.h>
#include "pico/stdlib.h"

#define BUTTON_PIN 15

int main(void)
{
	stdio_init_all();

	gpio_init(BUTTON_PIN);
	gpio_set_dir(BUTTON_PIN, GPIO_IN);
	gpio_pull_up(BUTTON_PIN);

	bool was_pressed = false;

	for (;;) {
		bool pressed = !gpio_get(BUTTON_PIN);       /* active low */

		if (pressed && !was_pressed)
			printf("button pressed\n");
		if (!pressed && was_pressed)
			printf("button released\n");

		was_pressed = pressed;
		sleep_ms(10);
	}
}

What each call does:

  • **gpio_init(pin)** takes the pin out of whatever function it had and makes it a software-controlled GPIO (function SIO, the single-cycle I/O block of the RP2040). It also clears the output and output-enable bits, so the pin starts as an input driving nothing.

  • **gpio_set_dir(pin, GPIO_IN)** sets the direction. GPIO_IN is 0 and GPIO_OUT is 1. After gpio_init the pin is already an input, and writing it states the intent.

  • **gpio_pull_up(pin)** enables the internal pull-up and disables the pull-down. The counterpart is gpio_pull_down(pin), and gpio_disable_pulls(pin) turns both off. gpio_set_pulls(pin, up, down) sets both at once.

  • **gpio_get(pin)** returns the pin level as a bool: true for high, false for low. It reads the pin's input value from the SIO block in a single instruction.

The stdio_init_all() call sets up the default standard output. With the CMake settings below, that is USB serial, so the output appears on a serial monitor on the host.

Detecting edges, not levels

The loop reads the pin every 10 milliseconds and remembers the previous state, so it prints only when the state changes. Without was_pressed, a press lasting half a second would print about 50 lines. The comparison pressed && !was_pressed is an edge: the press began on this iteration. The same idea in other languages is called edge detection, and the 10 ms sleep also gives a crude form of debouncing, since contact bounce lasts a few milliseconds and rarely survives to the next sample. A proper debounce is the subject of a later post in this cluster.

With a press and release, the serial monitor shows:

Text
button pressed
button released

A mechanical button can still print extra lines if the bounce straddles a sample, which shows up as pressed released pressed in rapid succession. The fix is the debounce logic, not a longer sleep.

Building it

A Pico SDK project needs a CMakeLists.txt that imports the SDK and links the libraries. The SDK 2.0 template:

CMakeLists.txtText
cmake_minimum_required(VERSION 3.13)

include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)

project(button C CXX ASM)
set(CMAKE_C_STANDARD 11)

pico_sdk_init()

add_executable(button button.c)

target_link_libraries(button pico_stdlib)

pico_enable_stdio_usb(button 1)
pico_enable_stdio_uart(button 0)

pico_add_extra_outputs(button)

pico_sdk_import.cmake is copied from the SDK's external folder into the project, as the SDK documentation describes. pico_stdlib pulls in the GPIO, timer and stdio libraries. pico_enable_stdio_usb sends printf over USB, and pico_add_extra_outputs produces the .uf2 file for drag-and-drop programming.

Text
mkdir build && cd build
cmake ..
make

Hold the BOOTSEL button while plugging in the Pico, and copy button.uf2 to the drive that appears.

Using the on-board LED to see it

If you do not have a serial monitor ready, drive the on-board LED from the button. On the original Pico the LED is GPIO 25 (PICO_DEFAULT_LED_PIN); on the Pico W it is controlled through the wireless chip and needs a different call. For the original Pico:

c
const uint LED = PICO_DEFAULT_LED_PIN;

gpio_init(LED);
gpio_set_dir(LED, GPIO_OUT);

for (;;)
	gpio_put(LED, !gpio_get(BUTTON_PIN));

gpio_put(pin, value) drives an output. The inverted read makes the LED light while the button is held.

Things that go wrong

The button always reads pressed. The pull-up is missing, so the pin floats low, or the button is wired between GPIO 15 and 3.3 V. With the wiring above and gpio_pull_up, an unpressed pin reads 1.

The output shows nothing. USB stdio takes about a second to enumerate. A message printed in the first moments after boot can be lost. Add sleep_ms(2000) after stdio_init_all() for debugging, or wait for the connection with while (!stdio_usb_connected()) sleep_ms(10);.

Pins GPIO 23 to 25 and 29. On a Pico these are connected to board functions (power control, VBUS detect, the LED, ADC supply sensing), and are not available on the header. GPIO 15 is a safe choice.

Reading at too high a rate. A loop without a sleep reads the pin millions of times per second. It works, but wastes power and amplifies contact bounce into many events.

Pull-down wiring and the other polarity

Some designs wire the button between the pin and 3.3 V and use a pull-down, so pressed reads 1. The SDK call changes to gpio_pull_down(BUTTON_PIN) and the test loses its inversion:

c
gpio_pull_down(BUTTON_PIN);
bool pressed = gpio_get(BUTTON_PIN);        /* active high */

Which polarity to choose is a design decision with a safety angle. If a wire to a button breaks, an active-low input with a pull-up reads released, which is the harmless state for most controls. An active-high input with a pull-down also reads released on a broken wire, so both fail the same way; the difference is that a short to ground makes an active-low input read as permanently pressed, and a short to 3.3 V does the same for active-high. On a board with a ground plane and few exposed 3.3 V traces, short-to-ground is the more likely fault, so many designers prefer to make the permanent-press state the one the firmware can detect and ignore. For a hobby project either is fine.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.