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:
- How to connect a sound sensor to an Arduino
- How to write MicroPython code for the Arduino to detect sound using the 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
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.
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.
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
This image is created using Fritzing. Click to enlarge image
How To Program For Sound Sensor
- Sets the Arduino pin as a digital input.
- It checks the status of a pin on the Arduino.
Arduino MicroPython Code - Detecting the sound
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.
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.