breadboard
docs

Example project · Builds & projects

Uno R4 · tone and header interrupts

D5 generates a short 1 kHz tone waveform. Physical wires feed D2’s CHANGE interrupt and D3’s FALLING interrupt; Serial prints their edge counts. Probe D5 in Scope and reset the simulation to capture the burst again.

The Uno R4 · tone and header interrupts circuit as rendered by the simulator

How it works

This bench tests two UNO R4 features together: tone generation and external interrupts. In setup(), tone(5, 1000, 20) starts a 1 kHz square wave on D5 that stops by itself after 20 ms, so about 20 cycles. D5 drives a 1 kΩ load to GND, and two wires connect it to D2 and D3, which the sketch sets as inputs with pull-ups.

attachInterrupt() makes D2 fire an interrupt on every CHANGE, rising or falling, and D3 fire only on FALLING edges. Each interrupt handler adds one to its own counter. loop() never reads the pins. Every 25 ms it turns interrupts off for a moment, copies both counters, turns interrupts on again and prints the copies, so both numbers come from the same instant. The edges the handlers count are the voltage edges on the solved D5 net, the same ones that Scope shows.

What to watch

Serial prints a line irq <D2 count> <D3 count> every 25 ms. Once the 20 ms burst is over, the counts stop at irq 41 21: the CHANGE interrupt has seen about twice as many edges as the FALLING one. Probe D5 in Scope and press Restart to catch the burst: a square wave with a 1 ms period for 20 ms, then a flat line.

What's on the bench

  • Arduino Uno R4
  • Resistor

How it's wired

  • Arduino Uno R4 · d5→Resistor · a
  • Resistor · b→Arduino Uno R4 · gnd2
  • Arduino Uno R4 · d5→Arduino Uno R4 · d2
  • Arduino Uno R4 · d5→Arduino Uno R4 · d3

The code

This Arduino C++ sketch lives in Code; sign in and press Compile & upload to build real firmware for the emulated board.

// Breadboard UNO R4 P5 external-interrupt and tone acceptance fixture.

static volatile uint32_t d2Edges = 0;
static volatile uint32_t d3Edges = 0;

static void onD2() {
  ++d2Edges;
}

static void onD3() {
  ++d3Edges;
}

void setup() {
  Serial.begin(115200);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), onD2, CHANGE);
  attachInterrupt(digitalPinToInterrupt(3), onD3, FALLING);
  tone(5, 1000, 20);
}

void loop() {
  static uint32_t last = 0;
  if (millis() - last >= 25) {
    last = millis();
    noInterrupts();
    uint32_t d2 = d2Edges;
    uint32_t d3 = d3Edges;
    interrupts();
    Serial.print("irq ");
    Serial.print(d2);
    Serial.print(' ');
    Serial.println(d3);
  }
}

Firmware licence

Notice: this firmware uses the LGPL-licensed Arduino core library

arduino-unor4wifi-irq-tone.bin is statically linked with ArduinoCore-renesas 1.6.0, in Breadboard's port of its UNO R4 WiFi variant to Renesas FSP 5.3.0. Parts of that core are licensed under the GNU Lesser General Public License, version 2.1 or (at your option) any later version ("the Library"). Breadboard distributes this firmware under terms that permit you to modify the work for your own use and to reverse engineer it for debugging such modifications.

Try this

  • Change tone(5, 1000, 20) to tone(5, 2000, 20), then sign in and press Compile & upload. Twice as many cycles fit in the burst, so both counts roughly double.
  • Change FALLING to RISING for D3 and upload again. The D3 count stays about the same, because a square wave has one rising edge for each falling edge.
  • Remove the wire from D5 to D3 and press Restart. The D3 count stays at 0, because the pull-up holds the pin high and nothing pulls it low.

Related projects