VU Meter with ESP32

Let's display sound intensity using ESP32 module, a microphone and an OLED display.

In this sketch, the ESP32 module reads the analog microphone output and computes the peak-to-peak value during each sampling window. At the end of each sampling window, the peak-to-peak voltage value is displayed as progress bar on OLED display:



Hardware set-up

Following hardware is required to build the experiment: Implement following connections:
  • Connect GND(ESP32+OLED) together with GND(KY-037)
  • Connect 3V3(ESP32+OLED) together with Vcc(KY-037)
  • Connect GPIO39(ESP32) to microphone analog output(KY-037)

Confused about all acronyms? Here is a brief explanation: GND is the ground line, 3V3 and Vcc are the positive supply line, GPIO39 is the ADC1 channel 3 input (GPIO is generic purpose Input/Output).

The following picture shows the VU Meter breadboard:

VU Meter breadboard

ESP32 software

Before writing software make sure to have installed the software development environment, downloadable from Arduino Software web site. It may be needed to download some library if not already present. Libraries are required also to manage the external devices.

The sketch has to include the required libraries:

#include "SSD1306Wire.h"


The loop() function implements the VU Meter functionality.

void loop() { unsigned long nextWindow = millis() + sampleWindow; // start of next sample window unsigned int peakToPeak = 0; // peak-to-peak value unsigned int signalMax = 0; unsigned int signalMin = 4095; unsigned int n_samples = 0; unsigned long accvalue = 0; // get peak to peak value during sampleWindow while (millis() < nextWindow) { unsigned int sample = analogRead(analogInputPin); n_samples++; accvalue += sample; if (sample > signalMax) signalMax = sample; else if (sample < signalMin) signalMin = sample; delayMicroseconds(100); // pauses for 100 microseconds before next sample } peakToPeak = signalMax - signalMin; // double volts = (peakToPeak * 3.3) / 4096; // convert to volts int progress = peakToPeak / attenuation; if (progress > 100) progress = 100; display.clear(); // draw the progress bar display.drawProgressBar(0, 32, 120, 10, progress); // write the buffer to the display display.display(); }


Download the complete sketch here and enjoy it.

Share Share on Facebook Share on Twitter Bookmark on Reddit Share via mail
Creative Commons Attribution-Share Alike | Terms and conditions | Privacy policy