This build turns a potentiometer knob into a volume-less switch for a piezo buzzer: turn the knob past a certain point and the buzzer sounds, turn it back and it goes quiet. It is a good first project for reading analog input and driving a digital output on the ESP8266, and a stepping stone toward tone-based alarms or melodies.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP8266 - Potentiometer 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)
- Potentiometer
- 10k Ohm Trimmer Potentiometer
- Potentiometer Kit
- Potentiometer Module with Knob
- 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
The potentiometer's wiper (middle pin) connects to the ESP8266's analog input, A0. The piezo buzzer's signal pin connects to D8, which is configured as a digital output. You can build this on a breadboard or wire it directly, point to point, without one.
How it works
A potentiometer is a variable resistor with three pins; turning the knob changes the voltage at the middle pin, which the ESP8266 reads as an analog value through analogRead(). The piezo buzzer is a simple output device that makes sound when it receives a signal. The sketch compares the potentiometer's analog reading against a fixed threshold: above it, the buzzer pin is driven HIGH and the buzzer sounds; at or below it, the pin goes LOW and the buzzer stays silent.
Code
In setup(), BUZZER_PIN is set to output mode. The loop() function continuously reads the potentiometer's analog value on A0 and checks it against ANALOG_THRESHOLD, which is set to 1000. If the reading exceeds the threshold, digitalWrite() turns the buzzer on; otherwise it turns it off. A variant of this sketch compares a converted voltage value instead of the raw analog reading, and another variant plays a melody once the threshold is crossed, using a separate pitches.h file you create in the Arduino IDE.
esp8266-potentiometer-piezo-buzzer.ino
/*
* This ESP8266 NodeMCU code was developed by newbiely.com
*
* This ESP8266 NodeMCU code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/esp8266/esp8266-potentiometer-piezo-buzzer
*/
#define POTENTIOMETER_PIN A0 // The ESP8266 pin connected to Potentiometer pin
#define BUZZER_PIN D8 // The ESP8266 pin connected to Buzzer's pin
#define ANALOG_THRESHOLD 1000
void setup() {
pinMode(BUZZER_PIN, OUTPUT); // Configure the ESP8266 pin to the output mode
}
void loop() {
int analog_value = analogRead(POTENTIOMETER_PIN); // read the input on analog pin
if (analog_value > ANALOG_THRESHOLD)
digitalWrite(BUZZER_PIN, HIGH); // turn on Piezo Buzzer
else
digitalWrite(BUZZER_PIN, LOW); // turn off Piezo Buzzer
}
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp8266/esp8266-potentiometer-piezo-buzzer

Top comments (0)