This circuit is designed to measure sound levels using a sound sensor and display the loudness in decibels (dB) on a 16x2 I2C LCD. Additionally, it uses three LEDs (red, green, and yellow) to indicate different sound levels: Quiet, Moderate, and Loud. The circuit is controlled by an Arduino UNO microcontroller.
Arduino UNO
LED: Two Pin (red)
LED: Two Pin (green)
LED: Two Pin (yellow)
Resistor (200 Ohms)
16x2 I2C LCD
Sound Sensor
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Library for the LCD screen
// Create an instance of the LCD with the address 0x27 and 16x2 dimensions
LiquidCrystal_I2C lcd(0x3F, 16, 2);
const int SENSOR_PIN = A0; // Pin for the sound sensor (Analog pin A0)
const int PIN_QUIET = 11; // Pin for "Quiet" level output
const int PIN_MODERATE = 10; // Pin for "Moderate" level output
const int PIN_LOUD = 9; // Pin for "Loud" level output
const int sampleWindow = 50; // Sample window size in milliseconds (50 ms = 20 Hz)
unsigned int sample;
void setup() {
lcd.init(); // Initialize the LCD screen
pinMode(SENSOR_PIN, INPUT); // Set the sound sensor pin as an input
pinMode(PIN_QUIET, OUTPUT); // Set the pins for sound level indicators as output
pinMode(PIN_MODERATE, OUTPUT);
pinMode(PIN_LOUD, OUTPUT);
// Set the sound level output pins to LOW initially
digitalWrite(PIN_QUIET, LOW);
digitalWrite(PIN_MODERATE, LOW);
digitalWrite(PIN_LOUD, LOW);
Serial.begin(9600); // Initialize serial communication for debugging
lcd.backlight(); // Turn on the LCD backlight
}
void loop() {
lcd.clear(); // Clear the LCD screen
unsigned long startMillis = millis(); // Start timing for the sample window
float peakToPeak = 0; // Variable to store the peak-to-peak value
unsigned int signalMax = 0; // Maximum signal value during the window
unsigned int signalMin = 1024; // Minimum signal value during the window
// Collect data for the duration of the sample window (50 ms)
while (millis() - startMillis < sampleWindow) {
sample = analogRead(SENSOR_PIN); // Read the value from the sound sensor
// Filter out invalid values
if (sample < 1024) {
if (sample > signalMax) {
signalMax = sample; // Update the maximum value
} else if (sample < signalMin) {
signalMin = sample; // Update the minimum value
}
}
}
peakToPeak = signalMax - signalMin; // Calculate the peak-to-peak amplitude
int db = map(peakToPeak, 20, 750, 49.5, 90); // Map the amplitude to decibels (dB)
// Display the loudness in decibels on the LCD
lcd.setCursor(0, 0);
lcd.print("Loudness: ");
lcd.print(db);
lcd.print(" dB");
// Set the output level based on the dB value
if (db <= 60) {
lcd.setCursor(0, 1);
lcd.print("Level: Quiet");
digitalWrite(PIN_QUIET, HIGH); // Activate Quiet level
digitalWrite(PIN_MODERATE, LOW); // Deactivate Moderate level
digitalWrite(PIN_LOUD, LOW); // Deactivate Loud level
} else if (db > 60 && db < 85) {
lcd.setCursor(0, 1);
lcd.print("Level: Moderate");
digitalWrite(PIN_QUIET, LOW); // Deactivate Quiet level
digitalWrite(PIN_MODERATE, HIGH); // Activate Moderate level
digitalWrite(PIN_LOUD, LOW); // Deactivate Loud level
} else if (db >= 85) {
lcd.setCursor(0, 1);
lcd.print("Level: High");
digitalWrite(PIN_QUIET, LOW); // Deactivate Quiet level
digitalWrite(PIN_MODERATE, LOW); // Deactivate Moderate level
digitalWrite(PIN_LOUD, HIGH); // Activate Loud level
}
delay(1500); // Wait for 1.5 seconds before the next reading
}
This code initializes the LCD and sets up the pins for the sound sensor and LEDs. It continuously reads the sound level, calculates the peak-to-peak amplitude, maps it to decibels, and displays the loudness on the LCD. Based on the dB value, it activates the corresponding LED to indicate the sound level.