Project Overview
The Arduino will read the analog values from the sound sensor using the analogRead()
function. These readings represent the intensity of the sound detected by the sensor. The higher the value, the louder the sound. The sensor will detect ambient noise, and higher values will appear when there are loud sounds, such as claps or nearby thunderstorms.
The I2C LCD will display the sound intensity in real-time. Additionally, the Arduino Nano will send the readings to the Serial Monitor for debugging purposes.
Components Needed
- Arduino Nano
- Analog Sound Sensor
- I2C LCD Display (16×2)
- Jumper wires
- Breadboard (optional)
Wiring Instructions
1. Wiring the Analog Sound Sensor to Arduino
- VCC Pin of the Sound Sensor → 5V Pin on Arduino Nano
- GND Pin of the Sound Sensor → GND Pin on Arduino Nano
- OUT Pin of the Sound Sensor → A0 Pin on Arduino Nano
2. Wiring the I2C LCD to Arduino
- VCC Pin of the LCD → 5V Pin on Arduino Nano
- GND Pin of the LCD → GND Pin on Arduino Nano
- SDA Pin of the LCD → A4 Pin on Arduino Nano (This is the I2C data line)
- SCL Pin of the LCD → A5 Pin on Arduino Nano (This is the I2C clock line)
Required Libraries:
How to Install:
- In the Arduino IDE, go to Sketch → Include Library → Manage Libraries…
- Search for LiquidCrystal_I2C in the Library Manager. (Author: Frank de Brabander or any other)
- Click Install for the latest version.
Arduino Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2-line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int soundPin = A0; // Analog sound sensor connected to pin A0
int soundLevel = 0; // Variable to store the sound sensor value
// Variables for smoothing
int previousSoundLevel = 0; // Previous sound level
int soundLevelSum = 0; // Sum of several readings for averaging
const int numReadings = 10; // Number of readings to average
int readings[numReadings]; // Array to hold sensor readings
int readIndex = 0; // Index for the readings array
int total = 0; // Running total for averaging
// Threshold for detecting significant sound (adjust as needed)
int threshold = 200;
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the LCD
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
// Initialize the readings array to 0
for (int i = 0; i < numReadings; i++) {
readings[i] = 0;
}
// Display initial message
lcd.setCursor(0, 0);
lcd.print("Sound Intensity");
lcd.setCursor(0, 1);
lcd.print("Measuring...");
delay(2000); // Wait for 2 seconds
}
void loop() {
// Read the sound intensity from the sound sensor
soundLevel = analogRead(soundPin);
// Update the total and remove the last reading from the total
total -= readings[readIndex];
// Add the new reading to the total
readings[readIndex] = soundLevel;
total += readings[readIndex];
// Move to the next position in the array
readIndex = (readIndex + 1) % numReadings;
// Calculate the average sound level
int averageSoundLevel = total / numReadings;
// Clear previous reading on the LCD
lcd.clear();
// Display the average sound intensity on the LCD
lcd.setCursor(0, 0);
lcd.print("Sound Intensity:");
// Display the average sound level value
lcd.setCursor(0, 1);
lcd.print(averageSoundLevel);
// If the sound level exceeds the threshold, detect a significant noise
if (averageSoundLevel > threshold) {
Serial.println("Loud sound detected!");
// Optional: You can add more code here to trigger other actions (e.g., turn on a LED, activate a buzzer, etc.)
}
// Print the sound level to the serial monitor for debugging
Serial.print("Average Sound Level: ");
Serial.println(averageSoundLevel);
delay(100); // Wait a bit before reading again
}
Code Logic
- The sound sensor measures the intensity of the sound.
- The Arduino Nano reads the analog value from the sensor and processes it to smooth the values over multiple readings.
- The I2C LCD displays the average sound intensity on a scale from 0 to 1023 (the range of
analogRead()
). - If the sound intensity exceeds a certain threshold, it indicates that a loud sound has been detected.
- The Serial Monitor displays the real-time values for debugging.
Adjustments for Better Results
- Sensor Sensitivity: If the readings are consistently low, try adjusting the sensitivity of the sound sensor (if it has a built-in potentiometer).
- Threshold: You may need to adjust the threshold for detecting loud sounds based on your environment. This value can be tweaked in the code.
- Averaging: The code smooths the readings by averaging multiple readings to reduce noise and get more consistent output.
Testing the System
- Upload the Code: Upload the provided code to your Arduino Nano via the Arduino IDE.
- Open the Serial Monitor: Observe the sound level values printed to the serial monitor. When there is no noise, the values should be low. When you make a loud noise (such as a clap), the value should increase significantly.
- Watch the LCD Display: The LCD should display the current sound intensity value. The LCD should show a steady reading, and when you produce loud sounds, the value should increase.
Conclusion
This project demonstrates how to measure sound intensity and display the results using an Arduino Nano, a sound sensor, and an I2C LCD display. It can be used for various applications, such as detecting weather-related sounds or monitoring noise levels in a room.
Feel free to experiment with the sensitivity of the sound sensor and adjust the threshold to match your specific needs.