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.

Quick summaryCount 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.

LevelTime needed
Intermediate1-2 hours

What you need (tools and setup)

ToolWhat it isWhy you need itCost
This course's live C sandboxA 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) — optionalThe 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) — optionalShows 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

An interrupt-driven press counter with a shared flagC: edit and run

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

  1. Configure a pin-change interrupt on the button pin.
  2. In the ISR, increment a shared press_count variable (declared volatile in real C).
  3. Layer debouncing on top (the interrupt+timer technique from the Interrupts group) to avoid counting bounce as multiple presses.
  4. In the main loop, periodically read and report press_count (using a critical section if reading requires more than one instruction).
  5. Confirm count accuracy against the actual number of physical presses.

Checklist before you call it done

CheckWhy it mattersHow to verify
Count matches the actual number of physical presses exactlyConfirms debounce is correctly filtering bouncePress a known number of times and compare
press_count is properly protected from ISR/main-loop racesPrevents data corruption or lost incrementsReview for a critical section around any multi-step access
No presses are missed even with rapid repeated pressingConfirms the interrupt and debounce logic keep up under loadPress rapidly in succession and verify an accurate count
Tip:Commit the first working version and every later improvement as separate commits, so the diff itself becomes a teaching example.
Debouncing the interrupt-driven count in softwareC: edit and run

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?