STM32 Blue Pill Button Interrupt (EXTI): Wiring, Code and Simulator
To build this STM32 Blue Pill Button Interrupt (EXTI) project you need an STM32 Blue Pill (STM32F103C8) and a push button, a 220 Ω resistor and a green LED. attachInterrupt() on PB12 uses the STM32’s EXTI lines: each press runs an interrupt handler that toggles an LED on PB13 and counts. Make the 5 connections in the wiring table, upload the Arduino C++ code, and press the button a few times. The LED changes on every press, and the loop only prints when the count moves.
Open this circuit in the free STM32 Blue Pill (STM32F103C8) simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | STM32 Blue Pill (STM32F103C8) | the microcontroller board |
| 1 | push button | Connects 1 to 2 while held. Pair with INPUT_PULLUP and wire the other side to GND. |
| 1 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | green LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
Wiring
| # | From | To |
|---|---|---|
| 1 | STM32 Blue Pill (STM32F103C8) PB12 | push button 1.l |
| 2 | STM32 Blue Pill (STM32F103C8) GND | push button 2.r |
| 3 | STM32 Blue Pill (STM32F103C8) PB13 | 220 Ω resistor leg 1 |
| 4 | 220 Ω resistor leg 2 | green LED anode (A, long leg) |
| 5 | STM32 Blue Pill (STM32F103C8) GND | green LED cathode (C, short leg) |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the STM32 Blue Pill (STM32F103C8). No install or account is needed.
- Add the parts. Add a push button, a 220 Ω resistor and a green LED from the parts palette.
- Wire 1. Connect STM32 Blue Pill (STM32F103C8) PB12 to push button 1.l.
- Wire 2. Connect STM32 Blue Pill (STM32F103C8) GND to push button 2.r.
- Wire 3. Connect STM32 Blue Pill (STM32F103C8) PB13 to 220 Ω resistor leg 1.
- Wire 4. Connect 220 Ω resistor leg 2 to green LED anode (A, long leg).
- Wire 5. Connect STM32 Blue Pill (STM32F103C8) GND to green LED cathode (C, short leg).
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Press the button a few times. The LED changes on every press, and the loop only prints when the count moves.
Arduino C++ code
// A push button on PB12 (to GND, internal pull-up) raises an EXTI interrupt
// on each press; the handler toggles the LED on PB13 and counts presses.
volatile int presses = 0;
volatile bool lit = false;
void pressed() {
presses++;
lit = !lit;
digitalWrite(PB13, lit ? HIGH : LOW);
}
void setup() {
Serial.begin(115200);
pinMode(PB13, OUTPUT);
pinMode(PB12, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PB12), pressed, FALLING);
Serial.println("Press the button");
}
int shown = 0;
void loop() {
if (presses != shown) {
shown = presses;
Serial.print("Presses: ");
Serial.println(shown);
}
}
What to try
Press the button a few times. The LED changes on every press, and the loop only prints when the count moves.
Frequently asked questions
How do I wire the Button Interrupt (EXTI) project on an STM32 Blue Pill (STM32F103C8)?
Make 5 connections: STM32 Blue Pill (STM32F103C8) PB12 to push button 1.l; STM32 Blue Pill (STM32F103C8) GND to push button 2.r; STM32 Blue Pill (STM32F103C8) PB13 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to green LED anode (A, long leg); STM32 Blue Pill (STM32F103C8) GND to green LED cathode (C, short leg).
What parts do I need for this STM32 Blue Pill project?
An STM32 Blue Pill (STM32F103C8) and a push button, a 220 Ω resistor and a green LED.
Can I simulate this STM32 Blue Pill project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is built with the STM32duino core and runs on an STM32F103 emulated instruction by instruction: its ARM Cortex-M3 core, NVIC and peripherals (GPIO, EXTI, USART, timers, ADC and I²C).
What does the code do?
attachInterrupt() on PB12 uses the STM32’s EXTI lines: each press runs an interrupt handler that toggles an LED on PB13 and counts.
More STM32 Blue Pill (STM32F103C8) projects
- STM32 Blue Pill HC-05 Bluetooth on USART2: Wiring, Code and Simulator
- STM32 Blue Pill LED Blink and Serial: Wiring, Code and Simulator
- STM32 Blue Pill ADC Potentiometer and PWM LED: Wiring, Code and Simulator
- STM32 Blue Pill I2C BMP280 with Wire: Wiring, Code and Simulator
- STM32 Blue Pill Serial UART Echo: Wiring, Code and Simulator
- STM32 Blue Pill Servo Motor Sweep: Wiring, Code and Simulator