Build it step by step
0 of 7 doneCount by hand first
Press the button on D3 and print the count by polling.
Build the 555
Wire the astable clock and check pin 3 with a scope or LED.
Send pulses to both pins
Connect pin 3 to D2 and D4.
Run the sketch
Compare the interrupt and polled counts after a minute.
Remove the delay
See the polled count get closer.
Make the interrupt routine long
Add a delay inside it and see counts go wrong, then remove it.
Debounce the button
Ignore presses within 50 ms of the last.
How do you build an interrupts lab?
Count fast button presses or pulses with an interrupt and compare with polling. You need Arduino Uno R3, Tactile push button 6 mm, Resistor 10 kΩ × 2, TM1637 4-digit display, NE555 Timer IC, Ceramic Capacitor 100 nF, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 45 minutes.
- Level
- Intermediate
- Time
- About 45 min
- Parts
- 8
Parts list
- Development BoardsBuyArduino Uno R3 × 1
- SwitchesBuy
- PassivesBuyResistor 10 kΩ × 2
- DisplaysBuy
- Integrated CircuitsBuyNE555 Timer IC × 1
- PassivesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| 9 V or 5 V supply | 555 pins 8 and 4, 5 V from the Uno | Power for the clock |
| 555 pin 1 | GND | Ground |
| 10 kΩ resistor (R1) | 5V and 555 pin 7 | Timing |
| 10 kΩ resistor (R2) | 555 pin 7 and pin 6 | Timing |
| 555 pins 2 and 6 | Joined | Astable connection |
| 100 nF capacitor | 555 pin 6 and GND | Timing capacitor, about 500 Hz |
| 555 pin 3 | Uno D2 and Uno D4 | Same pulse to the interrupt pin and to the polled pin |
| Button | Uno D3 and GND | Hand-pressed counter |
| TM1637 CLK | Uno D6 | Display clock |
| TM1637 DIO | Uno D7 | Display data |
| TM1637 VCC and GND | Uno 5V and GND | Power |
Code
The 555 makes about 500 pulses per second, and both D2 and D4 see the same pulses. The interrupt counts every one because the hardware calls the routine at each rising edge; the polled count misses pulses whenever the loop is busy with the display or the delay. The variable shared with the interrupt is marked volatile, and it is read with interrupts briefly switched off because it is more than one byte. Keep the interrupt routine short with no delay or printing. The button on D3 is for a hand count with bounce: add a debounce time of about 50 ms in your own routine. Arduino IDE, board "Arduino Uno" (or "Arduino Nano" with processor ATmega328P).
Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Library: TM1637Display (by Avishay Orpaz).
#include <TM1637Display.h>
const byte IRQ_PIN = 2, POLL_PIN = 4;
TM1637Display disp(6, 7);
volatile unsigned long irqCount = 0;
unsigned long pollCount = 0;
bool lastPoll = LOW;
void onPulse() { irqCount++; } // keep interrupt routines tiny
void setup() {
pinMode(IRQ_PIN, INPUT);
pinMode(POLL_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(IRQ_PIN), onPulse, RISING);
disp.setBrightness(5);
Serial.begin(115200);
}
void loop() {
// Polling: look at the pin when the loop gets round to it
bool now = digitalRead(POLL_PIN);
if (now && !lastPoll) pollCount++;
lastPoll = now;
static unsigned long lastShow = 0;
if (millis() - lastShow > 200) { // slow display work makes the polled count miss pulses
lastShow = millis();
noInterrupts(); unsigned long c = irqCount; interrupts(); // read a multi-byte variable safely
disp.showNumberDec(c % 10000);
Serial.print("interrupt "); Serial.print(c);
Serial.print(" polled "); Serial.println(pollCount);
delay(30); // pretend the loop is busy with other work
}
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
interrupt 0 polled 0
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Interrupt count | Close to 500 per second |
| Polled count with the delay | Much lower |
| Polled count without the delay | Closer but still low |
| Button pressed once | A single count after debounce |
If it does not work
Both counts stay zero
Check the 555 output and wires to D2 and D4.
Interrupt count too high
Check there is no bounce or noise on the pin.
Display flickers
Normal while busy; it refreshes every 200 ms.
Counts jump oddly
Read the volatile variable with interrupts off.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the interrupts lab?
Arduino Uno R3, Tactile push button 6 mm, Resistor 10 kΩ × 2, TM1637 4-digit display, NE555 Timer IC, Ceramic Capacitor 100 nF, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the interrupts lab take to build?
About 45 minutes for a intermediate maker, in 7 steps.
How do I know the interrupts lab works?
Interrupt count: Close to 500 per second. Polled count with the delay: Much lower. Polled count without the delay: Closer but still low.
Both counts stay zero: what should I check?
Check the 555 output and wires to D2 and D4.
Interrupt count too high: what should I check?
Check there is no bounce or noise on the pin.
Display flickers: what should I check?
Normal while busy; it refreshes every 200 ms.
Counts jump oddly: what should I check?
Read the volatile variable with interrupts off.
