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

QtyPartWhat it does
1STM32 Blue Pill (STM32F103C8)the microcontroller board
1push buttonConnects 1 to 2 while held. Pair with INPUT_PULLUP and wire the other side to GND.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1green LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1STM32 Blue Pill (STM32F103C8) PB12push button 1.l
2STM32 Blue Pill (STM32F103C8) GNDpush button 2.r
3STM32 Blue Pill (STM32F103C8) PB13220 Ω resistor leg 1
4220 Ω resistor leg 2green LED anode (A, long leg)
5STM32 Blue Pill (STM32F103C8) GNDgreen LED cathode (C, short leg)

Step by step

  1. 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.
  2. Add the parts. Add a push button, a 220 Ω resistor and a green LED from the parts palette.
  3. Wire 1. Connect STM32 Blue Pill (STM32F103C8) PB12 to push button 1.l.
  4. Wire 2. Connect STM32 Blue Pill (STM32F103C8) GND to push button 2.r.
  5. Wire 3. Connect STM32 Blue Pill (STM32F103C8) PB13 to 220 Ω resistor leg 1.
  6. Wire 4. Connect 220 Ω resistor leg 2 to green LED anode (A, long leg).
  7. Wire 5. Connect STM32 Blue Pill (STM32F103C8) GND to green LED cathode (C, short leg).
  8. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  9. 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