Home / Learn Embedded C / Project: Interrupt-Driven Button Counter
Project: Interrupt-Driven Button Counter
Count button presses using a pin-change interrupt instead of polling.
How to think about this
Definition
A button-press counter driven entirely by a pin-change interrupt (rather than polling), with proper debouncing and safe ISR/main-loop data sharing.
Why you need this
Real projects are where embedded C patterns become habits instead of theory.
How it works
Work through the steps in order; do not skip the checklist.
Where you'll see this
- Portfolio project
- Interview preparation
- Real hardware practice
What you will build
A button-press counter driven entirely by a pin-change interrupt (rather than polling), with proper debouncing and safe ISR/main-loop data sharing.
| Level | Time needed |
|---|---|
| Intermediate | 1-2 hours |
What you need (tools and setup)
| Tool | What it is | Why you need it | Cost |
|---|---|---|---|
| This course's live C sandbox | A small in-browser C interpreter with simulated GPIO/UART/ADC/PWM. | Every project in this course can be tried here first with zero setup, no board required. | free, built in |
| Code editor (VS Code or similar) | The program you write firmware source files in. | You need somewhere to write and organise the real .c/.h files for the project. | free |
| A real development board (Arduino/AVR, STM32 Nucleo, or ESP32 dev board) — optional | The actual microcontroller hardware. | Lets you move from the simulated board to a real one once the logic works here. | low cost, optional |
| A serial terminal (PuTTY, screen, minicom, or the Arduino IDE's monitor) — optional | Shows UART text your firmware prints, from a real board. | Mirrors the UART line you already see in this course's virtual board. | free |
Step 1: see it working in the sandbox
In real C, press_count must be declared volatile since it is modified by an ISR and read by main-loop code.
Step by step
- Configure a pin-change interrupt on the button pin.
- In the ISR, increment a shared press_count variable (declared volatile in real C).
- Layer debouncing on top (the interrupt+timer technique from the Interrupts group) to avoid counting bounce as multiple presses.
- In the main loop, periodically read and report press_count (using a critical section if reading requires more than one instruction).
- Confirm count accuracy against the actual number of physical presses.
Checklist before you call it done
| Check | Why it matters | How to verify |
|---|---|---|
| Count matches the actual number of physical presses exactly | Confirms debounce is correctly filtering bounce | Press a known number of times and compare |
| press_count is properly protected from ISR/main-loop races | Prevents data corruption or lost increments | Review for a critical section around any multi-step access |
| No presses are missed even with rapid repeated pressing | Confirms the interrupt and debounce logic keep up under load | Press rapidly in succession and verify an accurate count |
Even an interrupt-driven counter still needs a timing-based debounce filter, since the bounce itself generates extra edge interrupts.
Test what you just read
1. Why must press_count be declared volatile in the real (non-sandbox) version of this project?
A variable shared between an ISR and main-loop code needs volatile so the compiler never wrongly assumes it is unchanged and serves a stale cached value.