Getting Started With KY-037 Sound Sensor & Raspberry Pi (P1)

by mahmoodmustafashilleh in Circuits > Raspberry Pi

544 Views, 0 Favorites, 0 Comments

Getting Started With KY-037 Sound Sensor & Raspberry Pi (P1)

KY037 and Raspberry Pi.png

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:

  1. Subscribe to our YouTube Channel: Stay updated with our latest tutorials and project insights by subscribing to our channel at YouTube — Shilleh.
  2. Support Us: Your support is invaluable. Consider buying me a coffee at Buy Me A Coffee to help us continue creating quality content.
  3. Hire Expert IoT Services: For personalized assistance with your IoT projects, hire me on UpWork.

ShillehTek Website (Exclusive Discounts):

https://shillehtek.com/collections/all

ShillehTekAmazon Store:

ShillehTek Amazon Store — US

ShillehTek Amazon Store — Canada

ShillehTek Amazon Store — Japan

Supplies

Components Needed:

  1. ShillehTek KY-037 Sound Sensor
  2. Raspberry Pi (any model with GPIO pins). I used a 4B model.
  3. 3 Jumper Wires (Male-to-Female)
  4. A thin screwdriver or blade (for adjusting the potentiometer

Wiring the KY-037 Sound Sensor to the Raspberry Pi

Wiring Diagram:

  1. Connect the VCC pin of the KY-037 to pin 4 (5V) on the Raspberry Pi.
  2. Connect the GND pin of the KY-037 to pin 6 (Ground) on the Raspberry Pi.
  3. 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.






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:

  1. The code uses the RPi.GPIO library to interact with the GPIO pins on the Raspberry Pi.

Set Up GPIO Pin:

  1. GPIO.setmode(GPIO.BCM) sets the pin numbering system to BCM mode.
  2. 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:

  1. This function reads the digital output of the sensor (either 1 or 0) and prints whether sound is detected.
  2. The comments in the function guide you on adjusting the sensitivity using the potentiometer to ensure you get proper readings.

Main Program Loop:

  1. 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!
  2. You can adjust the potentiometer while the code is running to see changes in the printed output.

Interrupt Handling:

  1. 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.”

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:

  1. Wiring the sensor to the Raspberry Pi
  2. Running a Python script to read the digital output
  3. 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.