Nano RP2040 button
A pushbutton on D2 of the Nano RP2040 Connect lights the LED on D13, with the RP2040’s own pull-up and the Nano’s pin names. The whole build, an Arduino Nano RP2040 Connect and 1 more part, 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 $34. On Mokxi they're free.
What you'll learn
- Reading a button with INPUT_PULLUP
- Using the Nano's D pin names
- Why a pull-up button reads LOW when pressed
- Turning an input into an output with an expression
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Nano RP2040 Connect
- Parts: 3
How it works
The button connects D2 to ground. The sketch turns on the pin's internal pull-up, so D2 sits at 3.3 V and reads HIGH until you press the button, when it reads LOW.
The loop checks D2 every 5 milliseconds. If it is LOW, it sets the LED on D13 HIGH, and otherwise LOW. So the LED is on exactly while you hold the button.
Parts list
3 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Nano RP2040 Connect
- 1 × Full-size breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab 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 |
|---|---|
| Pushbutton pin 2b | Arduino Nano RP2040 Connect pin GND |
| Pushbutton pin 1a | Arduino Nano RP2040 Connect pin D2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Button: a pushbutton on D2 lights the LED on D13.
//
// INPUT_PULLUP switches on the pad's pull-up, so D2 sits at 3.3 V until the
// button pulls it to GND and reads LOW while it is held. D2 is GPIO 25.
const int BUTTON = D2;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, digitalRead(BUTTON) == LOW ? HIGH : LOW);
delay(5);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once. pinMode(BUTTON, INPUT_PULLUP) makes D2 an input with its pull-up on, so you need no resistor. pinMode(LED_BUILTIN, OUTPUT) makes the LED pin an output.
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}loop sketch.ino
Reads D2 with digitalRead and compares it to LOW. The ? HIGH : LOW part picks HIGH for the LED if the button is pressed and LOW if not. Then delay(5) waits briefly.
void loop() {
digitalWrite(LED_BUILTIN, digitalRead(BUTTON) == LOW ? HIGH : LOW);
delay(5);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED never lights This project
Check that one side of the button goes to D2 and the other to GND. - 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. - A pin is fed 5 V. The RP2040 is a 3.3 V part, and the pins are not 5 V tolerant. Arduino Nano RP2040 Connect
Keep every input at 3.3 V or less, with a level shifter or a divider for 5 V parts. Power the board by USB or VIN, and join the grounds. - The board is not found, or the wrong board is picked. The Arduino Nano RP2040 Connect needs its own board package. Arduino Nano RP2040 Connect
Install the Arduino Mbed OS Nano boards package and select the Nano RP2040 Connect. Use D13 for the LED. Double-tap reset to enter the bootloader.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.