Built-in project · Arduino Uno R3

Twinkle, Twinkle, Little Star on a passive buzzer (pin 9), on an Arduino Uno

A passive buzzer on pin 9 plays the first line of Twinkle, Twinkle, Little Star from two arrays, one of notes and one of lengths, with tone(). The whole build, an Arduino Uno and 1 more part, 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 $30. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Notes as frequencies in hertz
  • Storing a tune in arrays
  • Playing sounds with tone and noTone
  • Note lengths in milliseconds
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

A passive buzzer makes a note when pin 9 switches on and off at a certain frequency. The sketch holds a tune as two lists: one with the frequency of each note, and one with how long it lasts. Middle C is 262 Hz.

The loop plays the 14 notes of the first line of the song in order. Each note sounds for its length, then the sketch waits 30 percent longer and stops the tone, so there is a short gap and repeated notes sound separate. After the last note it rests a second and plays it all again.

Parts list

3 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.

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 pinConnects to
Piezo speaker pin 2Arduino Uno R3 pin GND
Piezo speaker pin 1Arduino Uno R3 pin 9

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
// Twinkle, Twinkle, Little Star on a passive buzzer on pin 9.
//
//   pin 9 -> the buzzer's + leg; the other leg -> GND
//
// A tune is two lists: which note, and for how long. Each note is a
// frequency in hertz (middle C is 262), and each length is in milliseconds.
// The for loop plays them in order, with a short gap so repeated notes are
// heard as two.

const int BUZZER = 9;

const int C = 262, D = 294, E = 330, F = 349, G = 392, A = 440;

const int melody[] = {C, C, G, G, A, A, G, F, F, E, E, D, D, C};
const int lengths[] = {400, 400, 400, 400, 400, 400, 800, 400, 400, 400, 400, 400, 400, 800};
const int NOTES = sizeof(melody) / sizeof(melody[0]);

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: Twinkle, Twinkle, Little Star on pin 9");
}

void loop() {
  for (int i = 0; i < NOTES; i++) {
    tone(BUZZER, melody[i], lengths[i]);
    delay(lengths[i] * 13 / 10);
    noTone(BUZZER);
  }
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts serial at 9600 baud and prints a line naming the tune. The buzzer pin needs no pin mode setup because tone handles it.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: Twinkle, Twinkle, Little Star on pin 9");
}

loop sketch.ino

A for loop goes through melody. For each note, tone plays it for lengths[i] milliseconds. Then delay(lengths[i] * 13 / 10) waits a little longer than the note, and noTone stops the buzzer. After the last note, a 1000 ms delay comes before the repeat.

void loop() {
  for (int i = 0; i < NOTES; i++) {
    tone(BUZZER, melody[i], lengths[i]);
    delay(lengths[i] * 13 / 10);
    noTone(BUZZER);
  }
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Silence This project
    Check the buzzer's pin 1 goes to pin 9 and pin 2 goes to GND. Use a passive buzzer.
  • Wrong notes or a different tune This project
    The melody and lengths lists must stay in the same order and the same size.
  • 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.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

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