标签双色LED

声控亮灯实验

    生活中经常能看到声控灯,只要有声音,灯变会亮起来;然后没有声音灯又会灭掉;这篇实验将通过一个声音探测传感器加上一个双色LED灯,来尝试模拟声控灯效果;感觉还是蛮好玩;对了,实验用的树莓派zero,可以更好的封装起来。
先看效果:
从上动图可见,当手指弹出声音时,LED灯亮起,然后等待0.5s后熄灭;具体接线图下图:
接线示例
其中声音探测传感器使用可参考文章:https://www.shumeijiang.com/2019/12/28/声音探测传感器实验;
双色LED灯使用可参考文章:https://www.shumeijiang.com/2019/10/27/双色led变化实验
具体实现代码如下:

#!/usr/bin/env python
#coding:utf-8

'''
from JiuJiang
树莓酱的操作实例
https:://www.suhmeijiang.com
'''

import RPi.GPIO as GPIO  ##引入GPIO模块
import time              ##引入time库

#声音探测
detectPin = 27

GPIO.setmode(GPIO.BCM)   ##此处采用的BCM编码
GPIO.setup(detectPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) ##初始化高电平

#注册声音探测事件
GPIO.add_event_detect(detectPin, GPIO.FALLING, bouncetime=600)

ledOne = 17
ledTwo = 18
GPIO.setup(ledOne, GPIO.OUT)
GPIO.setup(ledTwo, GPIO.OUT)

try:
    while True:
        #检测是否发现声音
        status = GPIO.event_detected(detectPin)

        if status:
            print('亮灯')
            GPIO.output(ledOne, True)
            time.sleep(0.5) #亮灯持续时间
            GPIO.output(ledOne, False)
        else:
            pass

        time.sleep(0.5) #检测间隔

except KeyboardInterrupt:
    print('停止检测')

GPIO.cleanup()
保存代码为jiujiang.py,然后执行python jiujiang.py,如果发现探测不灵或者过度灵敏,可以旋转声音探测传感器灵敏度调节按钮。