Introduction
This project is a sound-activated LED (light-emitting diode) in which the LED lights up whenever the sound sensor module detects a sound that exceeds the threshold that is set, such as a voice or clap. If a sound does not exceed the threshold of the sensor or in complete silence, the LED will remain unlit. The sound sensor module that is featured in this project has the capability of outputting a digital signal, although there are different variants that are capable of outputting both an analogue and digital signal. However, because this project makes use of digital signalling in switching the LED on/off, the digital output of the sensor will be used. This means that the sound sensor can only send two states depending on the input level (sound) that it receives: 1 (HIGH) or 0 (LOW). The sensor will send a 1 to the Arduino if it picks up any sound that exceeds its threshold value; otherwise, a 0 is sent for all sounds that do not exceed the threshold. In order to alter this threshold value, you can rotate the trimmer potentiometer that is mounted directly on the sensor module to modulate the sensitivity of the sensor. For the particular model in this project, rotating the potentiometer clockwise increases its sensitivity whereas rotating the potentiometer counter-counterclockwise decreases its sensitivity. It is important to note that depending on the model, the direction may be opposite for your sound sensor module. If the LED is not changing state (lighting up) when there is a loud sound nearby, you will need to alter the sensitivity of the sensor. Once again, any Python IDE installed on your Raspberry Pi should be compatible with this project but in this example, Thonny will be used. Alternatively, other IDEs such as mu IDE or even Nano that is built-in with the Raspberry Pi Terminal will work just fine. For this project, the components required are quite minimal and aside from the sound sensor module itself, you should be able to find all these components in your electronics workshop.
Wiring
The wiring for this project is fairly simple as aside from the main sound sensor module and LED, not a lot of extra components are needed. In terms of the sound sensor module, the sound sensor module that is featured in this project has a 3-pin configuration where there is an output pin (OUT) that sends a digital output signal back to your Raspberry Pi. If your module has a 4-pin configuration, use the digital output (DO) pin but if your module has this same 3-pin configuration, the standard output/signal pin will work. In terms of the LED, the reason that a 220Ω resistor is connected in series with the cathode is to prevent the LED from burning out when applying a supply voltage of +5 volts. This serves as a current-limiting resistor so it can technically be connected in series with either the anode or cathode of the LED. If you are brand new to the Raspberry Pi, connecting wires to the Pi may be a bit daunting at first due to none of the 40 pins having any labels to indicate what pin it may be. This means that it is recommended that you always follow a pinout guide to identify pins and to additionally double-check your wiring before powering up the Pi to avoid any shorts. Other than that, the wiring for this project is fairly simple as aside from the main sensor, not a lot of extra components are needed. A circuit diagram of the wiring is also featured below.
- Sound Sensor Module: Connect the VCC pin to 5v (Pin 04) on your Raspberry Pi, the GND pin to GND (Pin 06) and the OUT pin to GPIO3 (Pin 05) on your Raspberry Pi.
- LED: Insert one 220Ω resistor in the breadboard in series with the cathode/negative (-) pin for the LED. Connect the end of that resistor to GND (Pin 09) on your Raspberry Pi. Connect the anode/positive (-) pin of the LED to GPIO4 (Pin 07) on your Raspberry Pi.
- Now, you are ready to get started with running the code and getting this project up and running!
Project Code
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(4, GPIO.OUT, initial = GPIO.LOW)
while True:
sensor = GPIO.input(3)
if sensor == 0:
print("No sound!")
GPIO.output(4, 0)
elif sensor == 1:
print("Sound detected!")
GPIO.output(4, 1)
About the code
- The first line of this code is in place to import the Raspberry Pi GPIO (general purpose input output) module, which is essentially a Python library that enables us to control GPIO devices, such as the sound sensor module and LED, with the Raspberry Pi GPIO pins. Next, two functions from the RPI.GPIO module is used: GPIO.setwarnings() and GPIO.setmode(). The former function is used to ignore any warnings that may come up during the operation of this code by setting the function with a False argument. This ensures that the code is running smoothly and that it will be able to work with the sensor. The latter function selects whether we would like to define the pins of the Raspberry Pi by their GPIO pin number or their physical pin number on the 40-pin header of the Pi. In this case, the GPIO.BCM argument is used instead of the GPIO.BOARD argument to define pins by their GPIO pin number instead of their physical pin number.
- In the next two lines of code, GPIO pins 3 and 4, which are connected to the sound sensor and LED respectively, are both set up individually. GPIO pin 3 for the sound sensor module is set as an input device, and with the GPIO.PUD_DOWN function, it is set to be pulled down by default. This means that if the output voltage fluctuates between a 1 and 0 state, the output signal will be 0 by default (i.e. no sound). With GPIO pin 4 for the LED, it is set up as an output device and we use the GPIO.LOW function to set it to LOW (turned off) in its default state.
- A while loop is then introduced which basically means that the following lines of code will repeat over and over again after the code is run. Next, the GPIO pin that is connected to the sound sensor (GPIO3) is assigned to the variable sensor, which makes it easier to refer to if it is needed later in the code.
- Now, a simple if statement is used which states that if the sound sensor sends back a 0 signal (i.e. no sound is detected), a message will be printed to the monitor and the LED will be switched off. An else if statement is introduced (known as an elif statement) that consequently states that if the sound sensor detects a sound, another message is printed and the LED is switched on.
Next steps
From today's project, hopefully, you will have gained the technical know-how on how to set up a sound sensor with Python and the Raspberry Pi. Simply said, it is not very complicated now with the multitude of compatible Python resources that can be interfaced with GPIO devices. Upon understanding the fundamental principles of this project, it will certainly be easier to incorporate and integrate some of the concepts demonstrated in this circuit into other applications. The basis of this project essentially relies on the sound sensor picking up an input (sound) and acting as a switch to activate a change in the LED module, the output device. With this concept in mind, the output device can easily be substituted with a piezo buzzer (to generate noise), a visual display (LCD, OLED, TFT, etc.), a relay (connected in line with other external devices), a wireless transmitter etc. Additionally, in many consumer electronic devices, a similar type of circuit is used when creating voice-activated electronic devices or even home automation systems. Of course, these devices are made into such a compact size mostly due to the use of SMT PCBA.
Amazing Opportunities
Also, be sure to check out PCBWay, a leading manufacturer and distributor in PCB design and manufacturing. They have amazing prices and excellent quality in their services, so don't miss out on them! Plus, PCBWay has an amazing website, online Gerber viewer function and a gift shop so make sure to check out their links below:
PCBWay Free Online Gerber Viewer Function: https://www.pcbway.com/project/OnlineGerberViewer.html
PCBWay Gift Shop: https://www.pcbway.com/project/gifts.html
Enjoy! Contact us for any inquiries!