DEV Community

Cover image for ESP8266 - Potentiometer Piezo Buzzer
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP8266 - Potentiometer Piezo Buzzer

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

Wiring

ESP8266 - Potentiometer Piezo Buzzer wiring diagram

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
}
Enter fullscreen mode Exit fullscreen mode

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)