Projects / Interrupts lab

Interrupts lab

Intermediate45 minGuided buildReviewed

Build it step by step

0 of 7 done
  1. UNO

    Count by hand first

    Press the button on D3 and print the count by polling.

  2. UNO

    Build the 555

    Wire the astable clock and check pin 3 with a scope or LED.

  3. UNO

    Send pulses to both pins

    Connect pin 3 to D2 and D4.

  4. UNO

    Run the sketch

    Compare the interrupt and polled counts after a minute.

  5. UNO

    Remove the delay

    See the polled count get closer.

  6. UNO

    Make the interrupt routine long

    Add a delay inside it and see counts go wrong, then remove it.

  7. UNO

    Debounce the button

    Ignore presses within 50 ms of the last.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
9 V or 5 V supply555 pins 8 and 4, 5 V from the UnoPower for the clock
555 pin 1GNDGround
10 kΩ resistor (R1)5V and 555 pin 7Timing
10 kΩ resistor (R2)555 pin 7 and pin 6Timing
555 pins 2 and 6JoinedAstable connection
100 nF capacitor555 pin 6 and GNDTiming capacitor, about 500 Hz
555 pin 3Uno D2 and Uno D4Same pulse to the interrupt pin and to the polled pin
ButtonUno D3 and GNDHand-pressed counter
TM1637 CLKUno D6Display clock
TM1637 DIOUno D7Display data
TM1637 VCC and GNDUno 5V and GNDPower

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.

CheckExpected
Interrupt countClose to 500 per second
Polled count with the delayMuch lower
Polled count without the delayCloser but still low
Button pressed onceA 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 →

Ask Lark AI

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.