Reading a Button with an Interrupt and Responding Instantly — A button press triggers an interrupt; the board responds instantly without disturbing the main loop and changes the LED state.

Circuit Projects

Reading a Button with an Interrupt and Responding Instantly

Reading a Button with an Interrupt and Responding Instantly — A button press triggers an interrupt; the board responds instantly without disturbing the main loop and changes the LED state.

A button press triggers an interrupt; the board responds instantly without disturbing the main loop and changes the LED state.

November 22, 20265 min read

Instead of constantly polling a button, it is more efficient for the hardware to notify the board when an event happens. In this circuit the button connects to a digital pin that supports interrupts. A pull-up resistor holds the pin HIGH while idle; pressing the button pulls the pin to ground and creates the interrupt. The board pauses its current work, enters a short response routine, changes the LED state and then continues where it left off. A second LED shows that the main loop keeps running. The circuit teaches event-driven programming and instant response.

Circuit diagram for reading a button with an interrupt: control board, components and coloured jumper wires on a breadboard
Reading a button with an interrupt — original circuit diagram; the components and connections match the circuit in the simulator.

Bu projeyi gerçekten kurmak istersen parçaları hobidevre.com'dan temin edebilirsin →

Build it for real

To build this project for real, get the parts from hobidevre.com → hobidevre.com →

More build guides: hobidevre.com/blog

Create a free account — save your progress and get more circuits →

No installation

You can open this circuit in the browser simulator, trace the connections and change values to see what happens.

Open in simulator

Parts you need

Build the circuit with the components below. Each component’s role is listed in the parts list.

  • Control board × 1 — Handles the interrupt event and drives the LEDs.
  • Button × 1 — Produces the event that triggers the interrupt.
  • 10kΩ resistor × 1 — Holds the pin HIGH while idle.
  • 220Ω resistor × 2 — Limits the current of the LEDs.
  • Amber LED × 1 — Changes state when an interrupt occurs.
  • Green LED × 1 — Shows that the main loop is running.
  • +5V power rail × 1 — Supplies voltage to the pull-up resistor.

Build steps

Placement on the breadboard: the control board is on the left; the button and 10kΩ pull-up resistor are at the top centre (rows 4-7), with the interrupt line going to board pin 2. The amber LED is at the lower left and the green LED at the lower right; each LED has its own 220Ω resistor. All ground pins are on the bottom rail.

  1. Place the control board and connect its GND pin to the bottom rail. Tip: Pin 2 is suitable for the interrupt.
  2. Add the button at the top centre and connect one end to ground. Tip: The other end goes to the interrupt line.
  3. Place the 10kΩ pull-up resistor between +5V and the button’s signal pin. Tip: Without this resistor the pin floats.
  4. Connect the button’s signal pin to board pin 2. Tip: At the moment of the press the pin is pulled to ground.
  5. Connect the amber and green LEDs with separate 220Ω resistors to pins 3 and 4. Tip: Each LED’s cathode goes to ground.
  6. Press the button in the simulation and watch the amber LED change instantly. Tip: The green LED keeps blinking even during the interrupt.

How it works

An interrupt lets the processor pause the running program and jump to a special routine. When the button is pressed the pin level drops from HIGH to LOW and the hardware flags this as an interrupt request. The processor stops its current task, enters the interrupt routine, changes the LED state and returns; the main loop continues where it left off. The pull-up resistor keeps the pin at a defined HIGH level when the button is not pressed; otherwise the pin would pick up stray fluctuations. Mechanical buttons bounce for a few milliseconds when pressed; a short software delay or a hardware filter removes this bounce. This structure shows an event-driven response model instead of continuous polling.

Expected result

On every button press the amber LED switches instantly; the green LED keeps blinking regularly.

Tips and common mistakes

  • Without a pull-up resistor the LED may change on its own.
  • Keep the interrupt routine as short as possible.
  • Button bounce produces several interrupts; add a short delay.
  • Use only pins that support interrupts as the interrupt pin.

Try it yourself

  • Change the amber LED state when the button is pressed.
  • Observe that the green LED runs without interruption during an interrupt.
  • Change the debounce delay and compare the behaviour.

What you learn in this circuit

  • Compares interrupts with continuous polling.
  • Explains how a pull-up resistor stabilises the pin state.
  • Builds the habit of writing a short interrupt routine.
  • Justifies the effect of button bounce.

Finding the parts

If you want to build this project in real life, you can get the parts from hobidevre.com.

Looking for more project ideas? Browse the guides on the hobidevre.com blog.

You may also like

Try the simulators

You need an account to use the simulators. Create one to save your work.

Keep learning

Reinforce what you read on this page with our own resources.

Deepen this topic

You can also see the same topic in short definitions and full explanation pages.