ESP32 MicroPython Sound Sensor

This guide will teach us how to use a sound sensor and the ESP32 with MicroPython to detect sounds. In detail, we will learn:

ESP32 MicroPython sound sensor

Later, you can change the code to make an LED or a light turn on (using a relay) when it detects sound, or to make a servo motor rotate.

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Digital Sound Sensor
1×Analog Sound Sensor
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Sound Sensor

A sound sensor can be used to detect nearby sounds. There are two types of sound sensor modules:

  • Digital sound sensor module: Provides a simple ON or OFF signal.
  • Analog sound sensor module: Offers continuous range values along with ON/OFF signals.

The sensitivity of the digital output can be adjusted using a built-in potentiometer.

The Digital Sound Sensor Pinout

The digital sound sensor has three pins:

  • VCC pin: Connect this to VCC, between 3.3V and 5V.
  • GNC pin: Connect this to GND, which is 0V.
  • OUT pin: This output pin stays HIGH when there is no sound and goes LOW when sound is detected. Connect it to the input pin on your ESP32.
Sound Sensor Pinout
image source: diyables.io

The sound sensor has a sensitivity control knob and two lights that show its status.

  • One LED light shows power is on
  • One LED light for sound: it lights up with sound, turns off when quiet

The Analog Sound Sensor Pinout

The analog sound sensor has four connectors.

  • + pin: Connect to 5V or 3.3V.
  • G pin: Connect to GND (0V).
  • DO pin: This is a digital output pin that stays HIGH when no sound is present and goes LOW when sound is detected. Connect it to a digital input pin on the ESP32.
  • AO pin: This analog output pin provides the sound level as an analog value. Connect it to an analog input pin on the ESP32.
analog sound sensor Pinout
image source: diyables.io

The analog sound sensor has a built-in potentiometric device for easy adjustment of sensitivity. It also features two LED indicators.

  • One LED light indicates the power is on.
  • Another LED light signals the presence of sound. It turns on when there is noise and turns off when it is silent.

How It Works

The module includes a potentiometer that allows you to adjust its sensitivity to sound. The output pin goes LOW when the sensor detects sound and remains HIGH when no sound is detected.

Wiring Diagram

  • How to connect ESP32 and sound sensor using breadboard
The wiring diagram between ESP32 MicroPython Sound Sensor

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and sound sensor

How To Program For Sound Sensor

  • Sets the ESP32 pin 8 as a digital input.
sensor_pin = Pin(SENSOR_PIN, Pin.IN)
  • It checks the status of a pin on the ESP32.
sound_state = sensor_pin.value()

ESP32 MicroPython Code - Detecting the sound

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-sound-sensor """ from machine import Pin import time SENSOR_PIN = 18 # The ESP32 pin GPIO18 connected to OUT pin of the sound sensor prev_sound_state = 1 # the previous state from the input pin sound_state = 1 # the current reading from the input pin # Initialize the sensor pin as an input sensor_pin = Pin(SENSOR_PIN, Pin.IN) # Main loop while True: # Read the state of the input pin sound_state = sensor_pin.value() if prev_sound_state == 1 and sound_state == 0: print("The sound has been detected") elif prev_sound_state == 0 and sound_state == 1: print("The sound has disappeared") # Save the last state prev_sound_state = sound_state time.sleep(0.1) # Delay for 100 milliseconds to mimic Arduino's loop delay

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the sound sensor to the ESP32 according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Clap near the sound sensor.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The sound has been detected The sound has disappeared The sound has been detected The sound has disappeared
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

If the LED light is always on or always off, even when there is sound, you can adjust the potometer settings to improve how the sensor responds to sound.

Troubleshooting

If the sound sensor isn't working properly, try the following steps:

  • Adjust the sensitivity: Turn the small screw on the sensor to change its sensitivity level.
  • Reduce vibrations: To prevent the sensor from picking up vibrations or wind noise, secure it firmly to a stable surface.
  • Consider the sensing range: The sensor can only detect sounds within 10 inches. For accurate measurement, ensure sounds are made close to the sensor.
  • Check the power supply: Make sure the power supply is stable, as fluctuations can affect the performance of this analog sensor.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!