All tutorials

2 · Inputs & sensors

Hardware interrupts

Expert25 min

Goal

Count pulses without ever missing one thanks to attachInterrupt().

Why it matters

An interrupt suspends the loop instantly: essential for a tachometer or a flow meter.

Steps

  1. 01Wire the pulse source to pin 2 (INT0).
  2. 02Declare volatile unsigned long counter = 0;
  3. 03attachInterrupt(digitalPinToInterrupt(2), isr, FALLING);
  4. 04Print the counter inside loop() (never inside the ISR).

This tutorial runs inside the Circuitly simulator: your wiring and your code are checked automatically at every step.

Next tutorials