Nucleo button
A pushbutton from PC13 to GND is the Nucleo’s USER button: pressing it lights the green LED on D13. The whole build, an STM32 Nucleo-F411RE 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 $16.10. On Mokxi they're free.
What you'll learn
- Reading an on-board button
- A pull-up already built into the board
- A pressed button reads LOW
- Using named pins like USER_BTN
- Difficulty: Beginner
- Time: About 10 minutes
- Board: STM32 Nucleo-F411RE
- Parts: 3
How it works
The blue USER button B1 on a Nucleo connects PC13 to ground, and the board has its own 4.7k pull-up on that pin. So PC13 reads HIGH when the button is up and LOW while it is pressed.
In Mokxi the buttons drawn on the board do not take clicks, so you wire a pushbutton from PC13 to GND, which is what B1 does. The sketch checks the pin and lights the green LED while the button is held.
Parts list
3 parts, plus the jumper wires. They retail for about $16.10; on Mokxi they're free.
- 1 × STM32 Nucleo-F411RE
- 1 × Half 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 1a | STM32 Nucleo-F411RE pin PC13 |
| Pushbutton pin 2a | STM32 Nucleo-F411RE pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Button: the blue USER button B1 lights the green LED while it is held.
//
// B1 sits between PC13 and GND, and the board has its own 4.7 k pull-up (R30)
// on that pin, so plain INPUT is enough: PC13 reads HIGH released and LOW
// pressed. USER_BTN is STM32duino's name for it.
//
// The buttons drawn on the board do not take clicks in Mokxi, so to press B1
// here wire a pushbutton from PC13 (on the left morpho header) to GND, which
// is exactly what B1 does on the real board.
void setup() {
pinMode(USER_BTN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
bool pressed = digitalRead(USER_BTN) == LOW;
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(5);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets USER_BTN, which is PC13, as a plain INPUT, since the board already has the pull-up. Sets LED_BUILTIN, the green LED, as an output.
void setup() {
pinMode(USER_BTN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}loop sketch.ino
Reads the button, and pressed is true when it reads LOW. Then it writes the LED HIGH when pressed and LOW otherwise. A 5 ms delay keeps the loop calm.
void loop() {
bool pressed = digitalRead(USER_BTN) == LOW;
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(5);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Clicking the board's blue button does nothing. This project
In Mokxi, wire a pushbutton from PC13 to GND and press that one instead. - The LED is on when the button is not pressed. This project
Pressed reads LOW, so keep the test as== LOW. - 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. - Pins burn out or misbehave with 5 V parts. STM32 Nucleo-F411RE
STM32 pins run at 3.3 V logic. Many are 5 V tolerant, but not all. Check the datasheet or use a level shifter before connecting 5 V outputs. - Some pins do not work as expected. STM32 Nucleo-F411RE
Arduino header pins share functions and solder bridges set some. Check the pin mapping, and keep PA2 and PA3 for the ST-LINK serial port.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.