Controlling an LED With a Snap Using the KY-037 Sound Sensor and Raspberry Pi
by mahmoodmustafashilleh in Circuits > Raspberry Pi
66 Views, 2 Favorites, 0 Comments
Controlling an LED With a Snap Using the KY-037 Sound Sensor and Raspberry Pi
Welcome back to part 2 of our KY-037 sound sensor tutorial series! In part 1, we successfully set up the KY-037 sound sensor with a Raspberry Pi to detect sound using its digital output. If you haven’t gone through that tutorial yet, I recommend starting there, as this guide builds upon the previous one.
In this tutorial, we’ll go a step further by integrating an LED that turns on and off based on sound detection. With just a snap of your fingers or a clap, the LED will react accordingly. This project is a fun and interactive way to learn how to control physical components with sound, making it perfect for beginner IoT enthusiasts.
— — -
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):
https://shillehtek.com/collections/all
ShillehTekAmazon Store:
Supplies
To get started, you’ll need the following components:
- KY-037 Sound Sensor: Available at ShillehTek Amazon Store.
- Raspberry Pi (any model with GPIO pins): I’m using a Raspberry Pi 4B.
- 3 Male-to-Female Jumper Wires: For connecting the sensor to the Raspberry Pi.
- 1 LED: Any color will work, but I’ll be using a red LED.
- 1 220-ohm Resistor: To prevent the LED from drawing too much current.
- Breadboard (optional): For organizing your connections.
- A thin screwdriver: For adjusting the potentiometer on the KY-037 sensor.
Wiring the KY-037 Sound Sensor and LED to the Raspberry Pi
Before we dive into the wiring, let’s first understand our setup. The sound sensor will detect a snap or clap and send a digital signal to the Raspberry Pi. The Pi will then process this signal and decide whether to turn the LED on or off.
Here’s how you’ll wire the components:
Wiring the KY-037 Sound Sensor:
- VCC (Power) pin of the KY-037 connects to pin 2 (5V) on the Raspberry Pi.
- GND (Ground) pin of the KY-037 connects to pin 6 (Ground) on the Raspberry Pi.
- D0 (Digital Output) pin of the KY-037 connects to GPIO 4 on the Raspberry Pi (pin 7 in physical pin layout).
Wiring the LED:
- Connect the longer leg (positive) of the LED to GPIO 17 on the Raspberry Pi (pin 11 in physical layout).
- Connect the shorter leg (negative) of the LED to one end of the 220-ohm resistor.
- Connect the other end of the resistor to GND on the Raspberry Pi (pin 9 in physical layout).
Your setup should look something like this:
KY-037 to Raspberry Pi:
- VCC -> 5V (Pin 2)
- GND -> GND (Pin 6)
- D0 -> GPIO 4 (Pin 7)
LED to Raspberry Pi:
- Long Leg (Anode) -> GPIO 17 (Pin 11)
- Short Leg (Cathode) -> 220-ohm Resistor -> GND (Pin 9)
- In summary, the GPIO pin acts as a power source when set to High, allowing the LED to be powered and controlled through software.
Important Note: Make sure to connect the resistor to prevent too much current from flowing through the LED, which could damage it.
As I mentioned in Part 1, you should fine-tune the potentiometer to the point just below the detection threshold, allowing a snap to easily push it past the limit and trigger a response.
Here is how it looks in real life:
Writing the Python Code
Now that we have our components connected, let’s write the Python script to control the LED based on sound detection.
Create a new Python file called sound_led_control.py on your Raspberry Pi and add the following code:
Explanation of the Code:
- Setup: We configure the sound sensor pin as an input and the LED pin as an output.
- Global LED State: We keep track of whether the LED is on or off using the led_state variable.
- toggle_led() Function: This function toggles the LED based on its current state.
- Main Loop: The loop continuously checks for sound detection. If a sound is detected, it calls toggle_led() and then waits for 1 second to avoid rapid toggling.
Running the Code
Once you’ve copied the code into sound_led_control.py, it’s time to run it! Open a terminal on your Raspberry Pi and navigate to the directory where the file is located. Then, run:
You should see a message in the terminal saying, “Starting sound sensor program. Snap or clap to toggle the LED.”
Testing: Try snapping your fingers or clapping near the sensor. The LED should turn on or off with each snap or clap, depending on its current state. If the LED isn’t responding as expected, adjust the potentiometer on the KY-037 to fine-tune its sensitivity.
Conclusion and What’s Next?
Congratulations! You’ve successfully created a sound-activated LED control system using the KY-037 sound sensor and Raspberry Pi. In this tutorial, we learned:
- How to wire the KY-037 sound sensor and an LED to the Raspberry Pi.
- How to write a Python script to toggle the LED based on sound detection.
This project can serve as the foundation for more advanced sound-activated systems, such as creating a sound-responsive smart home lighting system or building an interactive sound-based game.
In the next tutorial, we’ll explore connecting multiple sound sensors to create more complex systems or use the analog output of the KY-037 for measuring sound intensity. Until then, keep experimenting and learning!
Support and Resources:
- Don’t forget to check out my YouTube Channel for more tutorials.
- Consider supporting me on Buy Me a Coffee to help fund future content creation.
- For custom projects or professional consulting, feel free to hire me on Upwork.
Happy coding, and see you in the next tutorial!