Guide no. 38
Two wires and one command: Arduino plays “Frère Jacques”. Why you need a passive buzzer, how to write the notes and how to turn it into an alarm.
By Ada Intelisano · informaticait.it technical team
- The passive buzzer goes between a pin and GND: two wires.
- Every note is a number of hertz: A is 440.
toneplays a note, an array holds the melody.- One note at a time; on the UNO it interferes with PWM on pins 3 and 11.
A buzzer turns the signal from a pin into a sound. With a single command, tone, Arduino plays the note you want: by the end of this guide it will play “Frère Jacques”.
1Get the parts ready
- a passive buzzer (piezo): the one with no electronics inside
- Arduino UNO, breadboard, two jumper wires
2Build the circuit
The buzzer has two legs: the one marked + goes to pin 8 (blue wire), the other to the − rail, which is connected to GND. Two wires in all.
3Notes are numbers
Every note is a vibration with a certain number of oscillations per second (hertz). These are the ones we use:
| C 262 | D 294 | E 330 | F 349 | G 392 | A 440 |
tone(8, 440, 400) plays the 440 Hz note (the A) on pin 8 for 400 thousandths of a second. noTone(8) silences the buzzer.4Write and upload the melody
// The buzzer plays “Frère Jacques”. Passive (piezo) buzzer between pin 8 and GND. #include <Arduino.h> const int BUZZER = 8; // with the ESP32 write 25 // the notes in hertz (the same as in the pitches.h file from the Arduino examples) const int NOTE_C4 = 262, NOTE_D4 = 294, NOTE_E4 = 330, NOTE_F4 = 349, NOTE_G4 = 392, NOTE_A4 = 440; const int MELODY[] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_C4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_C4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_E4, NOTE_F4, NOTE_G4}; const int DURATION[] = {400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 800, 400, 400, 800}; const int NUM_NOTES = sizeof(MELODY) / sizeof(MELODY[0]); void setup() { for (int i = 0; i < NUM_NOTES; i++) { tone(BUZZER, MELODY[i], DURATION[i]); // play the note for the given time delay(DURATION[i] * 1.2); // wait for it to end, plus a short pause } noTone(BUZZER); } void loop() { // the melody plays only once: press RESET to hear it again }
MELODY[] holds the notes in order, DURATION[] how long each one lasts; MELODY[0] is the first.- No sound: is the wire on pin 8? Is the buzzer passive? With an active one you'd hear a single tone.
- It plays very quietly: that's normal, small piezos aren't loud. Hold it close to your ear or rest it on a box.
- It always makes the same sound, whatever the note: it's an active buzzer, you need a passive one.
5Challenge yourself
- Change the durations: all 200 for a fast version.
- Add the pushbutton from guide no. 35: the melody starts when you press it (move the code from
setuptoloop). - Make an alarm: two alternating notes, 880 and 440, until you press the button.
6Two things to know
- Arduino plays one note at a time, and it can't go below 31 Hz.
- On the UNO, while
toneis playing, PWM on pins 3 and 11 doesn't work properly: for fading LEDs use other pins (guide no. 37).
7And with the ESP32?
Same circuit with the buzzer on GPIO 25; in the sketch write const int BUZZER = 25;. The melody is the same.
- a passive buzzer plays the note you give it, an active one always makes the same sound;
tone(pin, frequency, duration)plays,noTonesilences;- an array keeps many values in order under a single name.
Useful products
Other guides
- Resistor colour code and tolerances
- N-channel and P-channel MOSFETs
- Bipolar transistor (BJT) or MOSFET?
- Relays: how to drive them and how much current they draw
- THT and SMD/SMT components
- XIAO board pinouts
- LEDs: forward voltage and series resistor
- The multimeter: measuring voltage and current
- Wemos D1 mini: first steps
- MOSFET module with Arduino and ESP32
- Relay module with Arduino and ESP32
- 3D printing filaments: PLA, PETG and fibre-filled versions
- Analog and digital: the differences
- ADS1115: 16-bit converter with Arduino and ESP32
- From bits to real values: converting sensor readings
- DS3231 RTC clock with Arduino and ESP32
- Linear or switching regulator: which one to choose
- Step-down, step-up and buck-boost: choosing the right module
- Voltage divider: reading voltages higher than 5 V or 3.3 V
- Pull-up, pull-down and bounce: wiring a button properly
- 3.3 V and 5 V logic levels: getting an ESP32 and an Arduino to talk
- The I²C bus: addresses, pull-ups and the scanner
- Lithium batteries: voltages, charging and protection
- ESP32 power consumption and deep sleep: how long the battery lasts
- Temperature sensors compared
- ESP32: which pins to use and which to avoid
- Getting started with the ESP32 in Arduino IDE 2 and PlatformIO
- SSD1306 OLED display and 16×2 I²C LCD with Arduino and ESP32
- PIR motion sensor and HC-SR04 ultrasonic sensor
- Date and time from the Internet with the ESP32: NTP, Italian time zone and daylight saving time
- MicroSD data logger: saving measurements to CSV
- Solar-powered ESP32: panel, charger and the numbers for Italy
- Web server on the ESP32: control a relay from your phone
- Your first LED: wire it up and make it blink
- The button: let Arduino decide
- The potentiometer: turn a knob and read its value
- Soft light: adjusting the brightness of an LED
- The traffic light: three LEDs in sequence
- The thermometer: reading the temperature with the TMP36
- The servo motor: moving an arm with Arduino
- The night light: the photoresistor and your first decision
- The relay: switching on a light bulb with Arduino
- The LCD display: making Arduino write
- The electronic dice: your first complete project
- The RGB LED: every color from three lights
- The joystick: two-axis control with a shield
- The LED ring: twelve colors from a single pin
- The DC motor: driving it with a MOSFET
- Soil moisture: the plant that asks for water
- The stepper motor: turn exactly as far as you want
- The parking sensor: faster and faster beeps
- The 7-segment display: big, bright numbers
- Automatic watering: the plant that waters itself
- The keypad: a lock with a secret code
- Networked weather station: values and chart on your phone
- A Telegram bot on the ESP32: your home from your phone
- MQTT with the ESP32: the language of connected devices
- Updating the ESP32 over Wi-Fi (OTA)
- ESP-NOW: two ESP32 boards talking without a router
- WebSocket with the ESP32: the LED that follows the slider
- Readings on a Google sheet, with deep sleep
- WiFiManager: choose the Wi-Fi from your phone
- Bluetooth LE with the ESP32: the phone without a network
- The camera in your browser with the ESP32-S3
- A portable Bluetooth speaker you build yourself
- Bluetooth desktop speakers, 2 × 20 W
- Stereo MP3 player with two buttons
- The shelf that plays music: a Visaton exciter
- How to solder well
- How much does it draw? Current and power with the INA219
- Load-cell scale with the HX711
- CO2 traffic light: when to open the windows
- RFID card door opener
- GPS: track logger on microSD
- LoRa: how far does it go? A range test with two boards
- Battery weather sign with an e-paper display
- Web radio with an ESP32 and an I2S amplifier
- Getting started with the Raspberry Pi Pico 2 in MicroPython
- Your first amplifier: the LM386 and a speaker
- Talking Arduino: the DFR0760 speech synthesis module
- The spectrum of your music on an LED matrix
- Drawing with 64 LEDs: the 8×8 matrix with the MAX7219
- A real voltage: the 12-bit MCP4725 DAC
- The heartbeat on Arduino: an ECG with the AD8232