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.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
Step by step, with the wiring and the common mistakes: Arduino Tone Melody: Play a Song on a Piezo
What you'll learn
- Making sound with tone() and noTone()
- Notes as frequencies in hertz
- Arrays of notes and lengths played in order
- A short gap between notes
- Changing tempo with one constant
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
A passive piezo on pin 8 makes sound when it is fed a square wave. tone() starts that wave at a frequency you choose, and noTone() stops it. The sketch plays the opening of Ode to Joy, 15 notes.
Each note has a frequency and a length. The sketch starts the tone, waits, then stops it 30 ms before the note is meant to end, so repeated notes sound separate. The LED on pin 13 lights with each note. After the tune, it waits two seconds and plays again.
Parts list
5 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
Kits that can build it
These 10 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| LED, yellow pin C | Arduino Uno R3 pin GND |
| Piezo speaker pin 2 | Arduino Uno R3 pin GND |
| Resistor, 220 Ω pin 1 | Arduino Uno R3 pin 13 |
| Piezo speaker pin 1 | Arduino Uno R3 pin 8 |
| LED, yellow pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes the lamp pin an output, opens the serial port at 115200 and prints a hello line. The piezo needs no setup, because tone() handles the pin.
void setup() {
pinMode(LAMP, OUTPUT);
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: melody on pin 8");
}loop sketch.ino
For each note, it works out the length from TEMPO_MS times the beats, prints the frequency and lights the lamp. It plays NOTES[i] with tone, waits, then calls noTone and turns the lamp off for the 30 ms gap. At the end it prints a message and waits two seconds.
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);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The tune is too fast or slow This project
ChangeTEMPO_MS. A smaller number plays faster. - No sound This project
The piezo goes between pin 8 and ground. A buzzer that is already driven by itself will not work this way. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead. - Sound is very quiet or the pin seems overloaded. Piezo speaker
Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.