ESP32-Based Sound Level Logger for Classrooms

Objective

To create a sound level logger using an ESP32 and a microphone sensor that monitors noise levels in a classroom. The system records decibel readings over time and can display data on an OLED screen or log it to a cloud platform for later analysis.

Introduction

Maintaining a proper noise level in classrooms is crucial for effective teaching and learning. In this project, we build a noise monitoring system using an ESP32 and a sound sensor module. The ESP32 reads analog values from the sound sensor, calculates an approximate sound level in decibels, and logs the readings over time. The data can be stored locally, displayed, or transmitted over Wi-Fi to a server or IoT platform for further insights.

  • Connect the sound sensor to the ESP32 analog input.
  • Periodically read analog values and calculate the approximate sound level.
  • Display the current noise level on the Serial Monitor or an OLED display.
  • Log the timestamp and sound level to an SD card or upload to a cloud platform like ThingSpeak or Firebase.
  • Analyze the data to understand noise patterns in the classroom.

Formula: Sound Level (dB) ≈ 20 × log10(Vrms / Vref) Note: This is an approximation, as basic microphones provide relative amplitude values.

Components Required

  • ESP32 Development Board
  • Sound Sensor Module (e.g., KY-038 or LM393 Microphone)
  • OLED Display (Optional)
  • SD Card Module (Optional for local logging)
  • Breadboard and Jumper Wires
  • USB Cable

Circuit Diagram

  • Sound Sensor OUT →: GPIO 34 (Analog Input)
  • OLED SDA →: GPIO 21
  • OLED SCL →: GPIO 22
  • VCC & GND →: 3.3V and GND (Common for all components)

Make sure to connect the sound sensor OUT pin to an analog-capable pin (e.g., GPIO34) on the ESP32. Calibrate the analog threshold based on the sensor used.

Connection Table

  • -> GPIO 34
  • -> GPIO 21
  • -> GPIO 22
  • -> 3.3V and GND

Arduino Code

1#include <Wire.h>
2#include <Adafruit_GFX.h>
3#include <Adafruit_SSD1306.h>
4
5#define SOUND_SENSOR_PIN 34
6#define SCREEN_WIDTH 128
7#define SCREEN_HEIGHT 64
8Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
9
10void setup() {
11  Serial.begin(115200);
12  pinMode(SOUND_SENSOR_PIN, INPUT);
13
14  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
15    Serial.println("SSD1306 allocation failed");
16    while(1);
17  }
18  display.clearDisplay();
19  display.setTextSize(1);
20  display.setTextColor(WHITE);
21  display.setCursor(0,0);
22  display.println("Sound Logger Init...");
23  display.display();
24  delay(2000);
25}
26
27void loop() {
28  int soundLevel = analogRead(SOUND_SENSOR_PIN);
29  float voltage = soundLevel * (3.3 / 4095.0);
30  float dB = 20 * log10(voltage / 0.0066); // Approx reference voltage
31
32  Serial.print("Analog: "); Serial.print(soundLevel);
33  Serial.print(" | Voltage: "); Serial.print(voltage);
34  Serial.print(" V | dB: "); Serial.println(dB);
35
36  display.clearDisplay();
37  display.setCursor(0,0);
38  display.print("Sound Level:");
39  display.setCursor(0,20);
40  display.print(dB);
41  display.println(" dB");
42  display.display();
43
44  delay(1000);
45}

Code Explanation

  • analogRead(SOUND_SENSOR_PIN);: Reads the raw analog value from the sound sensor module.
  • voltage = soundLevel * (3.3 / 4095.0);: Converts the analog reading to a voltage using ESP32's ADC resolution.
  • 20 * log10(voltage / 0.0066);: Estimates sound level in dB relative to a baseline reference voltage.
  • display.print(dB);: Displays the current sound level on the OLED screen.

Applications

  • Monitoring classroom noise levels
  • Noise control in libraries or study zones
  • Industrial or office noise monitoring
  • Data logging for sound analysis

Conclusion

This ESP32-based sound logger effectively captures and logs environmental noise levels, providing a simple but insightful tool for monitoring classroom discipline and focus. With optional cloud integration, long-term noise trends can be visualized and analyzed for better classroom management.