If you are learning about the ESP32 and how to interact with sensors, you might want to explore a simple project where a sound sensor triggers an LED light. This tutorial will guide you through interfacing an ESP32 with a sound sensor and making an LED glow when sound is detected. Additionally, the LED will stay on for 3 seconds after the sound is detected.
Components Needed:
Before we dive into the code and wiring, let’s first look at what components you’ll need:
- ESP32 Development Board: This is the microcontroller that will process the sound sensor input and control the LED.
- Sound Sensor: This sensor detects sound and outputs an analog or digital signal depending on the sound level.
- LED: This will be controlled by the ESP32 to light up when sound is detected.
- 220 or 330 -ohm Resistor: This is required to protect the LED by limiting the current passing through it.
- Breadboard and Jumper Wires: For connecting the components together.
Wiring the Components:
1. Sound Sensor Wiring:
- VCC Pin of the sound sensor goes to 3.3V (or 5V, depending on your sensor) on the ESP32.
- GND Pin of the sound sensor connects to the GND pin on the ESP32.
- OUT Pin of the sound sensor goes to GPIO 19 (or any available GPIO pin for reading the sensor data). The sound sensor provides an analog output that can be used to determine sound intensity.
2. LED Wiring:
- The Anode (long leg) of the LED connects to GPIO 2 (or any available GPIO pin on the ESP32).
- The Cathode (short leg) of the LED connects to a 220-ohm resistor, and the other end of the resistor connects to GND.
Now, with the hardware properly wired, we can move on to the code that will make the LED glow based on the sound detected by the sensor.
Code for Sound Detection and LED Control:
In this step, we’ll write a simple program to read the sound sensor’s output and turn on an LED when sound is detected. We will also ensure that the LED stays on for 3 seconds before turning off.
// Define the pins
const int soundSensorPin = 19; // Pin connected to the sound sensor's OUT
const int ledPin = 2; // Pin connected to the LED
// Time duration for LED to stay on after sound detection (in milliseconds)
const unsigned long ledOnDuration = 3000; // 3000 ms = 3 seconds
// To store the time when sound was detected
unsigned long soundDetectedTime = 0;
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
// Set the LED pin as output
pinMode(ledPin, OUTPUT);
// Set the sound sensor pin as input
pinMode(soundSensorPin, INPUT);
}
void loop() {
// Read the value from the sound sensor
int sensorValue = analogRead(soundSensorPin);
// Print the sensor value to the Serial Monitor (optional for debugging)
Serial.println(sensorValue);
// If the sensor detects sound (threshold based on sensor value)
if (sensorValue > 1000) { // Adjust the threshold value as needed
digitalWrite(ledPin, HIGH); // Turn LED on
soundDetectedTime = millis(); // Record the time sound was detected
}
// Check if 3 seconds have passed since the sound was detected
if (digitalRead(ledPin) == HIGH && millis() - soundDetectedTime >= ledOnDuration) {
digitalWrite(ledPin, LOW); // Turn LED off after 3 seconds
}
// Delay a little to avoid overwhelming the serial monitor
delay(100);
}
Code Breakdown:
- Defining Pins:
soundSensorPin
is connected to the output of the sound sensor, which is read by the ESP32.ledPin
controls the LED light. The ESP32 will turn this pin HIGH to turn the LED on and LOW to turn it off.
- Setting LED Duration:
ledOnDuration
is the constant that defines how long the LED should stay on. We set it to 3000 milliseconds (or 3 seconds) in this case.
- Sound Detection Logic:
- The
analogRead()
function is used to read the sensor’s output. The value returned will indicate the sound intensity. - If the sensor value is above a threshold (set here to 1000, but you may need to adjust this based on your environment), the LED is turned on.
- Once the LED is turned on, we store the current time (in milliseconds) using the
millis()
function.
- The
- LED Off Logic:
- The program continuously checks whether the LED has been on for 3 seconds using
millis()
. If the time has passed, the LED is turned off.
- The program continuously checks whether the LED has been on for 3 seconds using
Adjusting the Threshold:
The threshold value (1000 in the code) defines the sound level at which the LED should turn on. You may need to adjust this value based on the sensitivity of your sound sensor and the ambient noise level. A higher value means that the sensor will only trigger the LED for louder sounds, while a lower value will make the sensor more sensitive to quieter sounds.
Testing the Circuit:
Once the wiring is complete and the code is uploaded to the ESP32, open the Serial Monitor in Arduino IDE to observe the sound sensor’s readings. When a sound above the threshold is detected, the LED will glow for 3 seconds and then turn off automatically.
Conclusion:
In this tutorial, we’ve successfully interfaced an ESP32 with a sound sensor and an LED. The sound sensor detects noise, and when it detects sound above a certain threshold, it triggers the ESP32 to turn on the LED. The LED will stay on for 3 seconds before turning off.
This is a simple yet powerful project that demonstrates the capabilities of the ESP32 in handling sensors and controlling output devices like LEDs. You can expand on this project by adding more sensors, controlling multiple LEDs, or triggering other actions based on sound detection.
Feel free to experiment with the code and the wiring to suit your specific needs.