Raspberry Pi - Sound Sensor

The sound sensor has the capability to detect the presence of sound in its surroundings. It can be employed to create projects that respond to sound, like lights that activate with a clap or a pet feeder that responds to sound cues.

This tutorial instructs you how to use the Raspberry Pi and a sound sensor to detect sound. We will explore:

Following that, you have the flexibility to modify the code and customize it to trigger an LED or a light (using a relay) upon sound detection. Alternatively, you can also configure it to control the rotation of a servo motor.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Sound Sensor
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

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. We appreciate your support.

Overview of Sound Sensor

The sound sensor can detect sound in its surroundings. You can easily adjust the sensitivity of the sensor by using the built-in potentiometer.


The sound sensor includes three pins:

  • VCC pin: needs to be connected to VCC (3.3V to 5V)
  • GND pin: needs to be connected to GND (0V)
  • OUT pin: is an output pin: HIGH if quiet and LOW if sound is detected. This pin needs to be connected to Raspberry Pi's input pin.
Sound Sensor Pinout
image source: diyables.io

The sound sensor has a handy built-in potentiometer that allows you to adjust its sensitivity. Additionally, it features two LED indicators:

  • One LED indicates the power status.
  • Another LED indicates the sound state, turning on when there is sound and off when it's quiet.

How It Works

The module includes a convenient potentiometer that allows you to adjust the sound sensitivity. Here's how the output pin of the sensor behaves:

  • When sound is detected, the output pin is set to LOW.
  • When sound is not detected, the output pin is set to HIGH.

Wiring Diagram

The wiring diagram between Raspberry Pi and Sound Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Sound Sensor

  • Initializes the Raspberry Pi pin to the digital input mode by using GPIO.setup() function.
  • Reads the state of the Raspberry Pi pin by using GPIO.input() function.
sound_state = GPIO.input(SENSOR_PIN)

Raspberry Pi Code - Detecting the sound

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file sound_sensor.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-sound-sensor import RPi.GPIO as GPIO from time import sleep # Set the Raspberry Pi GPIO pin number where the sound sensor is connected SOUND_SENSOR_PIN = 7 # Set the GPIO mode and configure the sound sensor pin as INPUT GPIO.setmode(GPIO.BCM) GPIO.setup(SOUND_SENSOR_PIN, GPIO.IN) # Initialize the previous state variable with the current state prev_sound_state = GPIO.input(SOUND_SENSOR_PIN) try: while True: # Read the current state of the sound sensor sound_state = GPIO.input(SOUND_SENSOR_PIN) # Check for a state change (LOW to HIGH or HIGH to LOW) if sound_state != prev_sound_state: if sound_state == GPIO.LOW: print("Sound detected!") # Update the previous state variable prev_sound_state = sound_state # Add a small delay to prevent continuous readings sleep(0.1) except KeyboardInterrupt: # Clean up GPIO settings when Ctrl+C is pressed GPIO.cleanup() print("\nExiting the program.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 sound_sensor.py
  • Clap your hand in front of the sound sensor
  • See the result in the Terminal.
PuTTY - Raspberry Pi
Sound detected! Sound detected! Sound detected!

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Please keep in mind that if you notice the LED status being continuously on or off, even when there is sound present, you may need to adjust the potentiometer to fine-tune the sound sensitivity of the sensor.

Now, we have the flexibility to modify the code and make it trigger an LED or a light when sound is detected. Additionally, we can even program it to rotate a servo motor. For detailed instructions and more information, please refer to the tutorials provided at the end of this guide.


If you encounter any issues with the sound sensor's functionality, try the following troubleshooting steps:

  • Reduce vibrations: The sound sensor is sensitive to mechanical vibrations and wind noise. Mounting it on a stable surface can help minimize these disturbances.
  • Consider the sensing range: Keep in mind that this sound sensor has a limited sensing range of approximately 10 inches. To obtain accurate readings, make sure the sound source is positioned closer to the sensor.
  • Check the power supply: Ensure that the power supply is stable and free from any electrical noise. The sound sensor, being an analog circuit, can be affected by power supply disturbances.

By following these steps, you should be able to address any potential issues with the sound sensor.

Video Tutorial

Function References

Learn More


  • 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!