A piezo buzzer is the simplest way to get sound out of a project: no amp, no speaker, just a part that clicks and buzzes when you feed it a signal. This tutorial wires one up to an ESP8266 and plays a little melody using the tone() and noTone() functions, the same ones you'd use on an Arduino.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP8266 - Piezo Buzzer
What you need
- ESP8266 NodeMCU ESP-12F
- ESP8266 D1 Mini NodeMCU ESP-12F
- ESP8266 NodeMCU ESP-12E (Uno-form)
- Micro USB Cable
- ESP8266 NodeMCU ESP-12F (SMD-form)
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- 3-24V Active Piezo Buzzer
- Active Piezo Buzzer Module
- Passive Piezo Buzzer Module
- Breadboard
- Jumper Wires
- Screw Terminal Expansion Board for ESP8266
- DIYables Sensor Kit (18 sensors/displays)
Wiring
A piezo buzzer has two pins. The negative (-) pin goes to GND on the ESP8266. The positive (+) pin connects to a control pin on the board, either directly or indirectly through a relay if you're driving a higher voltage buzzer. In the example sketch the signal pin used is D8. Buzzers are rated from 3V up to 24V; the 3-5V active ones can be driven straight from an ESP8266 pin, while higher voltage ones need a relay in between to switch the higher voltage safely.
How it works
An active buzzer like this one produces sound when it gets a signal, and the pitch depends on the frequency of that signal. Feeding it directly from a microcontroller pin gives a standard beep, good for things like a keypad click or a simple alert. If you instead switch a high-voltage source through a relay, the buzzer produces a much louder sound, which suits warning signals rather than quiet indicators.
Code
The sketch includes a pitches.h file that defines note frequencies like NOTE_C4, then stores a tune as two arrays: one with the notes (melody[]) and one with their durations (noteDurations[]). In setup(), it loops through the notes, calculates how long each should play based on its duration value, calls tone() on the buzzer pin to play it, waits a bit longer than the note itself so notes don't blur together, then calls noTone() to stop it before moving to the next note. loop() is left empty since the melody only needs to play once.
esp8266-piezo-buzzer.ino
#include "pitches.h" const int BUZZZER_PIN = D8; // The ESP8266 pin connected to piezo buzzer // notes in the melody: int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 }; // note durations: 4 = quarter note, 8 = eighth note, etc.: int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 }; void setup() { // iterate over the notes of the melody: for (int thisNote = 0; thisNote < 8; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(BUZZZER_PIN, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // The note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(BUZZZER_PIN); } } void loop() { // no need to repeat the melody. } Things that go wrong
If you hear nothing, double check the buzzer's negative pin is on GND and the signal pin matches the BUZZZER_PIN constant in the code. Make sure you've created the pitches.h file in the same sketch folder, since the code won't compile without it. If you're using a high-voltage buzzer directly on an ESP8266 pin instead of through a relay, you risk damaging the board, so match the buzzer's voltage rating to how you're driving it.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp8266/esp8266-piezo-buzzer

