Pico arcade button
A 30 mm arcade button on GP15 lights a lamp on GP14 and the Pico's own LED while it is held. 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 $10.65. On Mokxi they're free.
What you'll learn
- Reading a button with
INPUT_PULLUP - Why a pressed button reads LOW
- Driving two outputs from one input
- The Pico's built-in LED
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico
- Parts: 5
How it works
The arcade button joins GP15 to ground when pressed. The sketch turns on the pin's internal pull-up, so GP15 reads HIGH when the button is up and LOW when it is pressed. That means no outside resistor is needed.
The sketch checks the button over and over. While it is pressed, it turns on both the Pico's own LED and the green LED on GP14, which sits behind a 220 Ω resistor. Let go, and both go off. The Pico's LED is under the board, so the second LED gives you something to see on the breadboard.
Parts list
5 parts, plus the jumper wires. They retail for about $10.65; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × Full-size breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Arcade buttonIn Claw Machine Kit, Fight Stick Kit, Pinball Table Kit.
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 |
|---|---|
| LED, green pin C | Raspberry Pi Pico pin GND |
| Arcade button pin 1 | Raspberry Pi Pico pin GND |
| Resistor, 220 Ω pin 1 | Raspberry Pi Pico pin GP14 |
| Arcade button pin 2 | Raspberry Pi Pico pin GP15 |
| LED, green 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) as an input with the internal pull-up. It makes both 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. LOW means pressed, so pressed becomes HIGH. It writes that same value to both LEDs. There is no wait in the loop, so the LEDs follow the button right away.
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 always on, or never changes This project
Check the button joins GP15 to ground, not to 3.3 V. - Only the on-board LED lights This project
Check the green LED's direction: anode (A) toward the resistor, cathode (C) to ground. - 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. - The button reads pressed all the time, or never changes, when wired to a pin. Arcade button
Use the COM and NO terminals of the microswitch. Wire COM to ground, NO to the pin, and use INPUT_PULLUP. NC is the opposite and reads pressed when released. - One press counts as several presses in your code. Arcade button
Microswitch contacts bounce for a few milliseconds. Ignore changes for 10 to 20 ms after the first one, or react only to the edge and wait before checking again.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.