Skip to content

Instantly share code, notes, and snippets.

@colrichie
Created August 7, 2025 08:59
Show Gist options
  • Save colrichie/59ad9ce9b2f54b600f58eae2c39e49c7 to your computer and use it in GitHub Desktop.
Save colrichie/59ad9ce9b2f54b600f58eae2c39e49c7 to your computer and use it in GitHub Desktop.
A Test Sketch to Evaluate delayMecroseconds() on UNO Rev3 and R4
//////////////////////////////////////////////////////////////////////
//
// A Test Sketch to Evaluate delayMecroseconds() on UNO Rev3 and R4
//
// Try this sketch running on both Arduino UNO Rev3 and UNO R4 by the
// following steps.
//
// 1) Connect the GPIO #12 pin on your Arduino Rev3/R4 to a buzzer
// and amplify it to listen to the signal from the GPIO pin.
// (If you have "EK JAPAN SU-1204", just attach it to the Arduino
// UNO series.)
// 2) Connect the Arduino to your PC.
// 3) Run the Arduino IDE and make it recognize the Arduino.
// 4) Write the following sketch into the IDE and run it on the
// Arduino.
// 5) Listen to the sound from the buzzer. The sketch plays "Do-Re-Me"
// continuously. However, only when running it on UNO R4, you can
// hear a higher sound of "Do-Re-Me" and a lower sound alternately,
// whereas the same tone of "Do-Re-Me" is played on UNO Rev3 (and
// other boards, like Raspberry Pi Pico).
//
// I AM THINKING THAT THE "delayMecroseconds()" ON UNO R4 OVERSLEEPS
// A LITTLE MORE THAN THE SPECIFIED DURATION.
//
//
// To make this sketch, I adopted some codes written on the text book
// of "EK JAPAN SU-1204."
//
// Written by @colrichie on 2025-08-07
//
//////////////////////////////////////////////////////////////////////
void setup() {
pinMode(12,OUTPUT);
}
void loop() {
// Buzzing with delayMecroseconds()
for (int i=0; i<528; i++) { // Do
digitalWrite(12,HIGH);
delayMecroseconds(473);
digitalWrite(12,LOW);
delayMecroseconds(473);
}
for (int i=0; i<588; i++) { // Re
digitalWrite(12,HIGH);
delayMecroseconds(425);
digitalWrite(12,LOW);
delayMecroseconds(425);
}
for (int i=0; i<660; i++) { // Me
digitalWrite(12,HIGH);
delayMecroseconds(379);
digitalWrite(12,LOW);
delayMecroseconds(379);
}
// Buzzing with tone()
tone(12,1056); delay(500); // Do
tone(12,1176); delay(500); // Re
tone(12,1320); delay(500); // Me
noTone(12);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment