Pico button
A button on GP15 with the internal pull-up lights the LED on GP25. The whole build, a Raspberry Pi Pico and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $4.25. On Mokxi they're free.
What you'll learn
- Internal pull-up resistors with INPUT_PULLUP
- A pressed button reads LOW
- Reading one pin and driving another
- Using a breadboard LED beside the on-board LED
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico
- Parts: 4
How it works
The button connects GP15 to ground. The sketch turns on the Pico's internal pull-up, so the pin reads HIGH when the button is up and LOW when it is pressed. No outside resistor is needed.
The sketch keeps checking the button. When it is pressed, it lights the red LED on GP14 through a 220 ohm resistor, and the Pico's on-board LED too.
Parts list
4 parts, plus the jumper wires. They retail for about $4.25; on Mokxi they're free.
Kits that can build it
These 4 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Pushbutton pin 2a | Raspberry Pi Pico pin GND |
| LED, red pin C | Raspberry Pi Pico pin GND |
| Resistor, 220 Ω pin 1 | Raspberry Pi Pico pin GP14 |
| Pushbutton pin 1a | Raspberry Pi Pico pin GP15 |
| LED, red pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Button: read a pin, drive a pin.
//
// Wire a pushbutton from GP15 to any GND pin. INPUT_PULLUP switches on the
// pad's 55 k pull-up, so the pin reads HIGH when the button is up and LOW when
// it is pressed: no external resistor, and no floating input.
//
// GP25, the on-board LED, is inside the module, so the sketch lights GP14 as
// well for an LED on the breadboard.
const int BUTTON = 15;
const int LED = 14;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int pressed = digitalRead(BUTTON) == LOW ? HIGH : LOW;
digitalWrite(LED_BUILTIN, pressed);
digitalWrite(LED, pressed);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets BUTTON (GP15) to INPUT_PULLUP, and makes LED_BUILTIN and LED (GP14) outputs.
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED, OUTPUT);
}loop sketch.ino
Reads the button and sets pressed to HIGH when the pin reads LOW, and LOW otherwise. It writes that value to both LEDs, so they light together while the button is held.
void loop() {
int pressed = digitalRead(BUTTON) == LOW ? HIGH : LOW;
digitalWrite(LED_BUILTIN, pressed);
digitalWrite(LED, pressed);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED is dark even when pressed. This project
Check the LED direction. The anode (A) connects to the resistor from GP14, and the cathode (C) to GND. - The button seems to do nothing. This project
One side goes to GP15 and the other to any GND pin. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.