Getting Started with Raspberry Pi GPIO: Voice-Controlled Light Using Sound Sensor

Getting Started with Raspberry Pi GPIO: Voice-Controlled Light Using Sound Sensor

Click on the above
“blue text”
to follow us!

Limited Time Download: Follow us on “Microcontroller”, reply “Tutorial” to get the microcontroller eBook, reply Simulation” to get Proteus simulation materials. Continuously updated…

Source: Geek Fans (http://www.geekfans.com/article-5095-1.html)

In this article, we will use a sound sensor to create a simple voice-controlled light.

Final Effect

Getting Started with Raspberry Pi GPIO: Voice-Controlled Light Using Sound Sensor

Raspberry Pi GPIO Getting Started 07 – Voice-Controlled Light Using Sound Sensor video demonstration:

Hardware

  • Breadboard

  • Dupont Wire

  • 1 LED Light Emitting Diode

  • 1 Sound Sensor

    Getting Started with Raspberry Pi GPIO: Voice-Controlled Light Using Sound Sensor

    Hardware Diagram

Principle Explanation

The sound sensor has 3 pins: positive power, negative power, and data OUT, which are connected to the Raspberry Pi’s VCC, GND, and any GPIO pin (input mode), respectively. When the sound sensor detects sound, it outputs a low level from the OUT pin. Note that the low level does not last; once the sound ends, the sensor will stop outputting low level immediately. Additionally, there is a potentiometer on the sensor with a cross-shaped knob that can be rotated with a small screwdriver or knife to adjust the sound sensitivity (the threshold for triggering volume). The short pin of the LED is connected to GND, and the long pin is connected to another GPIO pin (output mode). When the Raspberry Pi receives the low level signal from the sensor, it outputs high/low levels to turn the LED on/off.

Hardware Connection

  1. Sound sensor positive power – Raspberry Pi’s VCC

  2. Sound sensor negative power – Raspberry Pi’s GND

  3. Sound sensor data OUT – Raspberry Pi’s GPIO4

  4. LED long pin – Raspberry Pi’s GPIO17

  5. LED short pin – Raspberry Pi’s GND

Code (Python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO
import time

# LED positive pin connected GPIO
LED = 17

# Sound sensor OUT pin connected GPIO
SENSOR = 4

# Current state of the LED light
flg = False

RPi.GPIO.setmode(RPi.GPIO.BCM)

# Set GPIO4 (the OUT pin of the sound sensor) to input mode
# Default pull-up to high level, low level indicates OUT pin has output
RPi.GPIO.setup(SENSOR, RPi.GPIO.IN, pull_up_down=RPi.GPIO.PUD_UP)

# Set GPIO17 (the long pin of the LED) to output mode
RPi.GPIO.setup(LED, RPi.GPIO.OUT)

try:
 while True:
 # Check if the sound sensor outputs low level, if so, it indicates sound is detected, turn LED on/off
 if (RPi.GPIO.input(SENSOR) == 0):
 flg = not flg
 RPi.GPIO.output(LED, flg)
 # Slight delay to avoid turning on and off too quickly.
 time.sleep(0.5)

except KeyboardInterrupt:
 pass

RPi.GPIO.cleanup()

Resource Download

prog.py (click to read the original text to download)

Limited Time Download: Follow us on “Microcontroller“, reply “Tutorial” to get the microcontroller eBook, reply “Simulation” to get Proteus simulation materials.

> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >

How to Share to Moments

Click the top right corner “…” three dots, and select “Share to Moments

Microcontroller Subscription Account

WeChat Name: Microcontroller Daily updates on various knowledge of microcontrollers, electronic DIY, and the latest news in the electronics industry. Follow us, it’s awesome!Getting Started with Raspberry Pi GPIO: Voice-Controlled Light Using Sound Sensor

Recommended Popular Articles

Click directly to enterRelated Articles:

001:Must-Read for Microcontroller Beginners

002:Words from Zhou Ligong to Young People Learning Microcontrollers

003:Experts Discuss: The Difficulty of Software and Hardware Entry and Mastery Time Span

004:Reflections on Learning the 51 Microcontroller; Recommended Learning Materials; Essential Programs

005:Comparison of Several Microcontrollers Used

006:《ARM+LINUX Learning Route (Learning Order, Knowledge Points, and Recommended Books)

007:Differences and Connections between ARM/DSP/FPGA/CPLD/SOPC/SOC

008:Fun Electronic Production: Food Power Generation in the Hands of Artists – Electronic DIY

009:My Experience: From a Production Line Worker to a Microcontroller Engineer

010:My Friend Brought Back a Toolbox from Germany Worth 200,000

Click the bottom left corner “Read the Original Text“, to enter theForum for Discussion!!!

Leave a Comment

×