Arduino MicroPython Sound Sensor

This guide shows you how to use a sound sensor with an Arduino and MicroPython to detect sounds. You will learn:

Arduino MicroPython sound sensor

You can later modify the code to turn on an LED or a light (using a relay) when sound is detected, or to make a servo motor rotate.

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Digital Sound Sensor
1×Analog Sound Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

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 sounds nearby. There are two types of sound sensor modules:

  • Digital sound sensor module: Provides a basic ON or OFF signal.
  • Analog sound sensor module: Offers a continuous range of values in addition to ON/OFF signals.

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

Digital Sound Sensor Pinout

The digital sound sensor has three pins:

  • VCC pin: Connect to VCC (3.3V to 5V).
  • GND pin: Connect to GND (0V).
  • OUT pin: This output pin remains HIGH when no sound is detected and goes LOW when sound is detected. Connect it to an input pin on your Arduino.
Sound Sensor Pinout
image source: diyables.io

The sound sensor features a sensitivity control knob and two status LEDs:

  • One LED indicates the power is on.
  • One LED indicates sound detection: it lights up when sound is detected and turns off when it's quiet.

The Analog Sound Sensor Pinout

The analog sound sensor has four pins:

  • + pin: Connect to 5V or 3.3V.
  • G pin: Connect to GND (0V).
  • DO pin: Digital output pin that stays HIGH when no sound is detected and goes LOW when sound is detected. Connect it to a digital input pin on the Arduino.
  • AO pin: Analog output pin that provides the sound level as an analog value. Connect it to an analog input pin on the Arduino.
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 has a potentiometer to adjust its sensitivity to sound. The output pin goes LOW when sound is detected and stays HIGH when no sound is detected.

Wiring Diagram

The wiring diagram between Arduino MicroPython Sound Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Sound Sensor

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

Arduino MicroPython Code - Detecting the sound

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-sound-sensor """ from machine import Pin import time SENSOR_PIN = 'D2' # The Arduino Giga R1 WiFi pin 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 run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the sound sensor to the Arduino according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • 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 (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

If the LED light is always on or always off, even when there is sound, you can adjust the potentiometer settings to improve the sensor's response 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!