Raspberry Pi Pico - Sound Sensor

This guide will teach us how to operate the Raspberry Pi Pico and a sound sensor to recognize sounds. We will discuss the following points:

Raspberry Pi Pico 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×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Digital Sound Sensor
1×Analog Sound Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

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

You can use a sound sensor to detect sound nearby. There are two types of sound sensor modules:

  • Digital sound sensor module: provides a simple ON or OFF signal.
  • Analog sound sensor module: gives continuous range values and ON/OFF signals.

You can change how sensitive the digital output is by using the potentiometer that is included.

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 is the output pin. It is HIGH with no noise and LOW when there is sound. Connect it to the input pin on your Raspberry Pi Pico.
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.
  • G pin: Connect to GND (0V).
  • DO pin: This is a digital output pin. It stays HIGH when there is no sound and goes LOW when sound is detected. Connect it to a digital input pin on the Raspberry Pi Pico.
  • AO Browpin: This is an analog output pin. It outputs the sound level as an analog value. Connect it to an analog input pin on the Raspberry Pi Pico.
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 that you can adjust to change how sensitive it is to sound.

  • When the sensor detects sound, its output pin is LOW. When the sensor does not detect sound, its output pin is HIGH.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Sound Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Sound Sensor

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

Raspberry Pi Pico Code - Detecting the sound

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-sound-sensor """ from machine import Pin import time SENSOR_PIN = 0 # The Raspberry Pi Pico pin (GP0) 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

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Wire the sound sensor to the Raspberry Pi Pico according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • 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 (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Troubleshooting

If the sound sensor is not functioning properly, follow these steps:

  • Adjust the sensitivity: You can change how sensitive the sensor is by turning the small screw on it.
  • Reduce vibrations: The sound sensor might capture vibrations and noise from the wind. To avoid this, fix it firmly to a stable surface.
  • Consider the sensing range: The sound sensor can only hear sounds that are within 10 inches. To measure sound accurately, make sure to make sounds close to the sensor.
  • Check the power supply: Ensure that the power supply is consistent. Fluctuations in power can disturb the functioning of this sensor, which uses analog technology.

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!