Skip to content

Arduino Interrupt-Based Pulse Measurement Missing Edges

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Arduino Interrupt. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Using interrupts to measure pulse width misses some edges.

Quick Fix

Wrong

volatile unsigned long start, width;
void isr() {
  start = micros();  // Missing edge detection!
}
ISR only captures one edge โ€” cannot measure pulse width.
volatile unsigned long riseTime = 0;
volatile unsigned long width = 0;
volatile byte state = 0;

void pulseISR() {
  if (digitalRead(2) == HIGH) {
    riseTime = micros();  // Rising edge
  } else {
    width = micros() - riseTime;  // Falling edge
    state = 1;
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  attachInterrupt(digitalPinToInterrupt(2), pulseISR, CHANGE);
}

void loop() {
  if (state) {
    state = 0;
    Serial.print("Pulse width: ");
    Serial.print(width);
    Serial.println(" us");
  }
}
Pulse width: 1500 us  (measured by interrupt edge capture).

Prevention

Use CHANGE mode to capture both edges. In the ISR, check the current pin state to determine rising or falling. Use micros() for timestamps (volatile unsigned long). Process the measurement in loop(), not ISR. On AVR, keep ISRs under 10 ยตs.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### Why use CHANGE instead of RISING/FALLING?

CHANGE triggers on both edges, allowing pulse width calculation from one ISR. Two separate ISRs (RISING + FALLING) also work but need shared variables.

What is the minimum measurable pulse with interrupts?

~10 ยตs on AVR (ISR latency). For shorter pulses, use the Input Capture Unit (ICP1) on Timer1.

Does micros() work correctly in an ISR?

micros() uses Timer0 and works in ISRs. However, if your ISR runs longer than the Timer0 overflow period, micros() values may be stale.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro