Built-in project · Arduino Uno R3

Passive buzzer module on pin 3, on an Arduino Uno

A passive buzzer on pin 3 plays two alternating tones by switching the pin on and off by hand, the way the lesson makes sound without 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

  • Passive versus active buzzers
  • Making a square wave by hand
  • Frequency sets pitch
  • Passing values into a function
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

A passive buzzer makes sound only when the voltage across it changes. So the sketch builds the sound wave itself: pin 3 HIGH, a short wait, pin 3 LOW, another short wait, over and over.

The length of each wait sets the pitch. A 1 ms wait each way gives a 500 Hz note, and 2 ms gives 250 Hz. The loop plays a high note for 80 cycles, then a lower note for 100 cycles, and repeats.

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 3

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
// A passive buzzer on pin 3: sound made by hand, one wave at a time.
//
//   module S -> pin 3   + -> 5V   - -> GND
//
// A passive buzzer only moves when the voltage changes, so the sketch makes
// the wave itself: HIGH, wait, LOW, wait, over and over. A 1 ms wait each way
// is a 500 Hz note; 2 ms is 250 Hz.

const int BUZZER = 3;

void note(int halfPeriodMs, int cycles) {
  for (int i = 0; i < cycles; i++) {
    digitalWrite(BUZZER, HIGH);
    delay(halfPeriodMs);
    digitalWrite(BUZZER, LOW);
    delay(halfPeriodMs);
  }
}

void setup() {
  pinMode(BUZZER, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: passive buzzer on pin 3");
}

void loop() {
  note(1, 80);
  note(2, 100);
}

Code walkthrough

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

note sketch.ino

Plays one note. For each cycle it sets the pin HIGH, waits halfPeriodMs, sets it LOW and waits the same time again. A shorter wait means a higher pitch. The cycles value sets how long the note lasts.

void note(int halfPeriodMs, int cycles) {
  for (int i = 0; i < cycles; i++) {
    digitalWrite(BUZZER, HIGH);
    delay(halfPeriodMs);
    digitalWrite(BUZZER, LOW);
    delay(halfPeriodMs);
  }
}

setup sketch.ino

Makes BUZZER (pin 3) an output, starts the serial port at 9600 baud and prints a hello message.

void setup() {
  pinMode(BUZZER, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: passive buzzer on pin 3");
}

loop sketch.ino

Calls note(1, 80) for a 500 Hz tone and note(2, 100) for a 250 Hz tone, then repeats.

void loop() {
  note(1, 80);
  note(2, 100);
}

Troubleshooting

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

  • Only clicks, or a steady tone, with no pitch changes. This project
    A passive buzzer needs the pin to switch on and off. Make sure both digitalWrite calls and both delay calls in note are there.
  • No sound. This project
    Pin 3 goes to the buzzer's pin 1, and the buzzer's pin 2 goes to GND.
  • 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.