HOW to Control Light by Sound with ESP32
Introduction
In this lesson, we will make a smart sound activated light using a sound sensor and an LED module. When we make a sound, the light will automatically turn on; when there is no sound, the light will automatically turn off. How it works? Because the sound-controlled light is equipped with a sound sensor, and this sensor converts the intensity of external sound into a corresponding value. Then set a threshold, when the threshold is exceeded, the light will go on, and when it is not exceeded, the light will go off.
Components
|
|
|
---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
Keyestudio Sound Sensor*1 |
|
|
|
Keyestudio Purple LED Module*1 |
3P Dupont Wire*2 |
MicroUSBCable*1 |
Connection Diagram
Test Code
//**********************************************************************************
/*
* Description : Sound sensor controls LED on and off
* Auther : http//www.keyestudio.com
*/
int ledPin = 15;//LED is connected to GP15
int microPin = 34;//Sound sensor is connected to GPIO34
void setup() {
Serial.begin(9600);//Set baud rate to 9600
pinMode(ledPin, OUTPUT);//LED is the output mode
}
void loop() {
int val = analogRead(microPin);//Read analog value
Serial.print(val);// Serial port print
if(val > 600){//exceed the threshold value
digitalWrite(ledPin, HIGH);//Lighting LED 3s,and print the corresponding information
Serial.println(" led on");
delay(3000);
}else{//otherwise
digitalWrite(ledPin, LOW);//Turn off the LED and print the corresponding information
Serial.println(" led off");
}
delay(100);
}
//**********************************************************************************
Code Explanation
We set the ADC threshold value to 600. If more than 600, LED will be on 3s; on the contrary, it will be off.
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. Open the serial monitor and set the baud rate to 9600, then the corresponding volume ADC value will be displayed.
When the analog value of sound is greater than 600, the LED on the LED module will light up 3s, otherwise it will go off.