Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using Python. This tutorial focuses on wiring and coding to measure the digital output signal of the sensor. The KY-037 is a highly versatile sensor that can detect sound levels and is often used in projects requiring sound-activated switches, noise monitoring systems, or smart home automation.
If you’re not familiar with the KY-037, it has two types of outputs: a digital output and an analog output. The digital output is a simple on/off signal (0 for no sound detected, 1 for sound detected), making it ideal for applications that need to trigger actions when a specific sound threshold is reached — such as clapping to turn on a light. The analog output, on the other hand, provides a range of values representing sound intensity, making it useful for scenarios that require more precise sound measurements. We’ll cover the analog output in a future tutorial, as it’s less commonly used for basic sound detection applications.
Overall, this sensor is incredibly useful because it’s cost-effective, easy to set up, and versatile enough for a variety of sound-based detection projects. Whether you’re building a sound-activated alarm system or experimenting with noise sensitivity in different environments, the KY-037 is a great sensor to start with.
— — -
Before we delve into the topic, we invite you to support our ongoing efforts and explore our various platforms dedicated to enhancing your IoT projects:
  • Subscribe to our YouTube Channel: Stay updated with our latest tutorials and project insights by subscribing to our channel at YouTube — Shilleh.
  • Support Us: Your support is invaluable. Consider buying me a coffee at Buy Me A Coffee to help us continue creating quality content.
  • Hire Expert IoT Services: For personalized assistance with your IoT projects, hire me on UpWork.
ShillehTek Website (Exclusive Discounts):
ShillehTekAmazon Store:
Step 1: Wiring the KY-037 Sound Sensor to the Raspberry Pi
Components Needed:
Wiring Diagram:
  • Connect the VCC pin of the KY-037 to pin 4 (5V) on the Raspberry Pi.
  • Connect the GND pin of the KY-037 to pin 6 (Ground) on the Raspberry Pi.
  • Connect the D0 (Digital Output) pin of the KY-037 to GPIO 4 on the Raspberry Pi (pin 7 if you’re using a physical pin layout).
Your setup should look like this:
KY-037 Raspberry Pi ----------------------------- VCC ----> 5V (Pin 2) GND ----> GND (Pin 4) D0 ----> GPIO 4 (Pin 7)
Here is how it looks in real life fyi.
Important Note:
Before we run the code, make sure you have a small blade or screwdriver handy. The KY-037 sound sensor has a potentiometer (a small screw-like component) that you’ll need to adjust while the code is running to get the sensitivity just right. Once the code is running we should reduce or increase sensitivity depending on the output we get in the logs.
Step 2: Understanding the Python Code and Running It
Now that we have the hardware set up, let’s go through the Python script used to read the KY-037’s digital output. This script will help us detect sound and print whether sound has been detected in real time.
You can simply copy this code onto the Raspberry Pi in any Python file and run it.
import RPi.GPIO as GPIO import time # Set up GPIO pin numbering mode and the sound sensor pin GPIO.setmode(GPIO.BCM) SOUND_SENSOR_PIN = 4 # GPIO pin number (adjust as needed) GPIO.setup(SOUND_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Function to check and print sound detection status def detect_sound(): sound_detected = GPIO.input(SOUND_SENSOR_PIN) # Read the digital signal # The KY-037 module has a potentiometer (small screw-like component) that adjusts the sensitivity. # Tuning this potentiometer is crucial to get accurate readings. Turning it clockwise increases sensitivity, # which means the sensor will detect lower sound levels and produce more "1" (sound detected) outputs. # Turning it counter-clockwise decreases sensitivity, making it less likely to detect sound (producing more "0" outputs). # If the sensor is only showing "1" (sound detected) values, rotate the potentiometer counter-clockwise # (you may need to rotate it up to 100 times) until you start seeing "0" values. This adjustment sets the sensitivity threshold # so that it can correctly detect and respond to sound changes. if sound_detected == 1: print("Sound detected!") # Sound detected else: print("No sound detected.") # No sound detected # Main program loop try: print("Tuning your KY-037 sound sensor: Adjust the potentiometer to get accurate 0/1 readings.") print("If you see only 'Sound detected!' messages, reduce sensitivity by rotating the screw counter-clockwise.") while True: detect_sound() # Check sound sensor status except KeyboardInterrupt: print("Program interrupted. Cleaning up GPIO settings.") GPIO.cleanup() # Clean up all GPIO settings
Note: By default, the RPi.GPIO library should already be pre-installed on the Raspberry Pi OS. However, if it’s not available, you can install it using the following command:
sudo apt-get install python3-rpi.gpio
Code Explanation:
Import Libraries:
  • The code uses the RPi.GPIO library to interact with the GPIO pins on the Raspberry Pi.
Set Up GPIO Pin:
  • GPIO.setmode(GPIO.BCM) sets the pin numbering system to BCM mode.
  • GPIO.setup(SOUND_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) configures GPIO pin 4 as an input pin with a pull-down resistor.
Define detect_sound() Function:
  • This function reads the digital output of the sensor (either 1 or 0) and prints whether sound is detected.
  • The comments in the function guide you on adjusting the sensitivity using the potentiometer to ensure you get proper readings.
Main Program Loop:
  • The code runs an infinite loop that calls detect_sound() every loop. Keep in mind it prints quickly so if it switches back and forth you wont see it long!
  • You can adjust the potentiometer while the code is running to see changes in the printed output.
Interrupt Handling:
  • If you press Ctrl + C, the program will stop, and GPIO.cleanup() will reset the GPIO settings to avoid issues in future runs.
Tuning the Sensor While the Code is Running! Important
With the code running, this is where you’ll start adjusting the potentiometer. Use a thin blade or screwdriver to rotate the potentiometer slowly. If the output constantly shows Sound detected!, reduce the sensitivity by rotating the screw counter-clockwise. You should aim to see the output fluctuate between Sound detected! and No sound detected. when there is a change in the sound environment.
When I first got my sensor I saw this in the logs. Meaning it was sensitive.
After rotations counter clockwise (many of them). I finally saw these values.
You can adjust it to the point where it is right in between, so when you talk or make any noise, it will briefly show “Sound Detected” and then switch back to “No Sound Detected.” This is the sweet spot we need for our next tutorial, where we can snap and trigger an LED to turn on or off. Play around with it as you like — the world is yours.
Please note that it could take dozens of rotations to finally see the value change! Yes, I know it can be frustrating, but be patient with these KY-037s, as their factory default is usually set to the highest sensitivity. I had to turn mine about 100 times counterclockwise before I saw it finally change from “Sound Detected” to “No Sound Detected.”
Step 3: Conclusion and What’s Next?
In this tutorial, we successfully connected and tuned the ShillehTek KY-037 sound sensor with the Raspberry Pi. We covered:
  • Wiring the sensor to the Raspberry Pi
  • Running a Python script to read the digital output
  • Tuning the potentiometer for accurate sound detection
In the next tutorial, we’ll take it a step further and use the KY-037 to control an LED, turning it on and off with a snap of your fingers! Don’t forget to subscribe to my YouTube channel for more cool tutorials, and feel free to reach out if you have any questions or need help with your project. If you’re interested in purchasing this sensor, you can get it directly from the ShillehTek Amazon store. Also, if you need custom projects or help with similar electronics, you can hire me on Upwork!
Good luck, my friends.

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

Implementing Google reCAPTCHA in a Simple React and Node.js App

Implementing Google reCAPTCHA in a Simple React and Node.js App

Learn how to protect your React applications from bots and spam with Google reCAPTCHA integration! This step-by-step tutorial...

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

In this tutorial, I will guide you through the process of running Selenium with ChromeDriver inside an AWS...

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

Learn how to use the MLX90614 with the Raspberry Pi Pico W and get infrared values in MicroPython.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.