Built-in project · Arduino Uno R3

Piezo melody on an Arduino Uno

The Uno plays the opening of Ode to Joy on a piezo with tone() and noTone(), lighting an LED for every note and printing each pitch on the serial monitor. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Beginner Runs in your browser. Free, and no account needed.

Piezo melody · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Melody: a tune on a piezo, one note at a time, with a lamp on the beat.
//
//   pin 8   a passive piezo, its other leg to GND
//   pin 13  the lamp, through 220 ohm to GND
//
// tone(pin, frequency) starts a square wave on the pin from timer 2 and
// returns at once, so the sketch decides how long a note lasts with its own
// delay() and stops it with noTone(). The gap after each note is what makes
// two notes of the same pitch sound like two notes instead of one long one.
//
// The tune is the opening of Beethoven's "Ode to Joy", which is old enough
// to belong to everybody. Each note is a frequency in hertz and a length in
// beats; change TEMPO_MS to play it faster or slower, or write your own.

const int PIEZO = 8;
const int LAMP = 13;

// The notes the tune needs, in hertz (A4 is 440).
const unsigned int C4 = 262;
const unsigned int D4 = 294;
const unsigned int E4 = 330;
const unsigned int F4 = 349;
const unsigned int G4 = 392;

const unsigned int NOTES[] = {
  E4, E4, F4, G4, G4, F4, E4, D4, C4, C4, D4, E4, E4, D4, D4,
};
// Length in beats, times two, so a half beat is 1 and a beat and a half is 3.
const unsigned char BEATS[] = {
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 1, 4,
};
const int COUNT = sizeof(BEATS);

const unsigned long TEMPO_MS = 200;  // half a beat
const unsigned long GAP_MS = 30;     // the silence between notes

void setup() {
  pinMode(LAMP, OUTPUT);
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: melody on pin 8");
}

void loop() {
  for (int i = 0; i < COUNT; i++) {
    unsigned long length = TEMPO_MS * BEATS[i];
    Serial.print("note ");
    Serial.print(NOTES[i]);
    Serial.println(" Hz");
    digitalWrite(LAMP, HIGH);
    tone(PIEZO, NOTES[i]);
    delay(length - GAP_MS);
    noTone(PIEZO);
    digitalWrite(LAMP, LOW);
    delay(GAP_MS);
  }
  Serial.println("again in two seconds");
  delay(2000);
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; LED, yellow pin C; Piezo speaker pin 2
  • Arduino Uno R3 pin 13; Resistor, 220 Ω pin 1
  • Arduino Uno R3 pin 8; Piezo speaker pin 1
  • Resistor, 220 Ω pin 2; LED, yellow pin A

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.

Free forever. Every board and part. Saves your projects. Works on Chromebooks.