X
Home > Blog > STEM for ESP32 > Noise! HOW to Use Sound Sensor Control LEDwith ESP32

Noise! HOW to Use Sound Sensor Control LEDwith ESP32

By r January 6th, 2025 161 views

1. Overview

The sound sensor captures the sound signals of the surrounding environment. When the sound reaches the threshold, the red LED lights up, otherwise the green LED turns on.

In this project, the technology of sound sensing and electronic alarm is combined with high sensitivity and fast response. It is suitable for home security, warehouse monitoring and laboratory.

With simple programming and circuit connection, we can monitor sound and alarm, providing an effective technical means for security protection.

2. Code Flow

a33

3. Wiring Diagram

b10

4. Test Code

/*  
  Project name: 15_Noise_Alarm
  Function: The sound in the environment is monitored, and the red LED on the threshold is exceeded
  Author: keyestudio
  Hardware connection:
    - Connect sound sensor to port 7
    - Connect traffic light module to port 9
  Library:  
    - none (no need to import additional library)  
  Cautions:  
    - Make sure you are connected to the correct interface   
    - Before uploading test code, please correctly connect to the development board and port
*/
#define sound_Pin 35      //set sound sensor pin to IO35
#define RED_LED_PIN 17    //Connect the red LED to port IO17
#define GREEN_LED_PIN 19  //Connect the green LED to port IO19

void setup() {
  //Set the baud rate
  Serial.begin(9600);
  //set IO35 to intput
  pinMode(sound_Pin, INPUT);
  // Initialize the digital port to output
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
}

void loop() {
  //Read the sound analog value
  int sound_value = analogRead(sound_Pin);
  //Determine whether the sound analog value is greater than 200, if yes, red LED on, otherwise green LED on
  if (sound_value > 200) {
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    delay(2000);
  } else {
    digitalWrite(RED_LED_PIN, LOW);
    digitalWrite(GREEN_LED_PIN, HIGH);
  }
}

5. Test Result

After uploading the test code, if the ambient sound value exceeds 200, the red LED will turn on for two seconds, otherwise the green LED will turn on.

OLED Reveal! HOW to Show Humidity And Temperature with ESP32
Previous
OLED Reveal! HOW to Show Humidity And Temperature with ESP32
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us