博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
云中树莓派(4):利用声音传感器控制Led灯
阅读量:6698 次
发布时间:2019-06-25

本文共 3697 字,大约阅读时间需要 12 分钟。

 

 

1. 声音传感器及其配置

声音传感器如下图所示:

将 VCC 引脚接入树莓派 5V 引脚,将 GND 引脚接入树莓派 GND 引脚,将 OUT 引脚接入树莓派 GPIO20。

要注意,模块在环境声音强度达不到设定阈值时,OUT输出高电平(1),当外界环境声音强度超过设定阈值时,模块OUT输出低电平(0)。

 

2. GPIO Event 机制

树莓派提供了三种电信号事件反馈机制。

(1)GPIO.wait_for_edge:直接等待电信号达到某种条件(升高还是降低或者任意),并且可以设置超时时间。在超时时间内,函数会一直等待,直到期望的电信号改变出现,或者超时。

# wait for up to 5 seconds for a rising edge (timeout is in milliseconds)channel = GPIO.wait_for_edge(channel, GPIO_RISING, timeout=5000)if channel is None:    print('Timeout occurred')else:    print('Edge detected on channel', channel)

(2)GPIO.add_event_detect:设置事件触发检测,一旦检测到,会返回True。

GPIO.add_event_detect(channel, GPIO.RISING)  # add rising edge detection on a channeldo_something()if GPIO.event_detected(channel):    print('Button pressed')

(3)GPIO.add_event_detect:回调函数机制。注册回调函数,一旦指定事件触发,回调函数会被调用。

def my_callback(channel):    print('This is a edge event callback function!')    print('Edge detected on channel %s'%channel)    print('This is run in a different thread to your main program')GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)  # add rising edge detection on a channel

3. 利用声音检查模块控制Led灯

实现目标:当检测到声音时,改变Led 灯的状态。

3.1 代码

import RPi.GPIO as GPIOimport timefrom time import sleepSOUND_PIN_NUM = 20 #声音模块的输出引脚接的GPIOLED_PIN_NUM = 26   #LED 的长脚接的GPIOstate = 0  #保存led 的状态timeLast = time.time()  #保存上次触发的时间# in one sounding, the callback function will be invoked for a few times, so need wait for some time to validDuration = 0.1GPIO.setmode(GPIO.BCM)GPIO.setup(SOUND_PIN_NUM, GPIO.IN)GPIO.setup(LED_PIN_NUM, GPIO.OUT)def callback_fun_soundOccurred(input_pint):    global timeLast    timeNow = time.time()    duration = timeNow - timeLast    if (duration < validDuration):        print("ignored because duration " + str(duration) + " is too short")        timeLast = timeNow        return    print("accepted for valid duration " + str(duration))    timeLast = timeNow    switchLed()def switchLed():    global state    if (state):      turnOffLed()      state = 0    else:      turnOnLed()      state = 1def turnOnLed():    print("Turn on")    GPIO.output(LED_PIN_NUM,GPIO.HIGH)def turnOffLed():    print("Turn off")    GPIO.output(LED_PIN_NUM, GPIO.LOW)GPIO.add_event_detect(SOUND_PIN_NUM, GPIO.RISING, callback=callback_fun_soundOccurred)try:    while True:        sleep(0.1)except KeyboardInterrupt:    GPIO.remove_event_detect(SOUND_PIN_NUM)    GPIO.cleanup()

3.2 两个小技巧

(1)尽管一个只需要一块五毛钱,但声音检测模块的灵敏度是可以调节的。使用螺丝刀转动上面的旋钮,边转变说话,看其开关指示灯的反应,亮表示检测到声音,亮度表示声音大小。旋到合适的位置即可。默认时,它非常灵敏,任何细小的声音都会触发它。

(2)在一句话说话过程中,回调函数会被触发好多次。因此,需要的话,如上面代码,可以计算两次调用之间的事件间隔,把太短的间隔过滤掉。下面是一句短话过程中函数被触发的情况:

ignored because duration 0.000501155853271 is too shortignored because duration 0.000110864639282 is too shortignored because duration 0.00215411186218 is too shortignored because duration 0.000218868255615 is too shortignored because duration 0.000470161437988 is too shortignored because duration 0.000167846679688 is too shortignored because duration 0.000583171844482 is too shortignored because duration 0.000425815582275 is too shortignored because duration 0.0010621547699 is too shortignored because duration 0.000314950942993 is too shortignored because duration 0.000555038452148 is too shortignored because duration 0.000130891799927 is too shortignored because duration 0.000461101531982 is too shortignored because duration 0.00022292137146 is too shortignored because duration 0.00274705886841 is too shortignored because duration 0.000133037567139 is too shortignored because duration 0.00597095489502 is too shortignored because duration 0.000155925750732 is too shortignored because duration 0.00107598304749 is too shortignored because duration 0.000198125839233 is too short

 

参考链接:

欢迎大家关注我的个人公众号:

转载地址:http://qwmoo.baihongyu.com/

你可能感兴趣的文章
Sublime 资源汇总
查看>>
kotlin使用spring data jpa(一)
查看>>
Memcached 内存管理(一)
查看>>
编写数学公式的好工具
查看>>
前端那些事之原生 js实现贪吃蛇篇
查看>>
五种方式让你在java中读取properties文件内容不再是难题
查看>>
SEO新手入门笔记
查看>>
Myeclipse5.5获取注册码
查看>>
Zabbix中文模式:图片下面的字符乱码
查看>>
面向对象理论概述
查看>>
Flex与Java通信之HttpService方式
查看>>
2013 ACM/ICPC Asia Regional Changsha Online - C
查看>>
ACM中java快速入门
查看>>
Spark 宽依赖和窄依赖
查看>>
40 个重要的 HTML5 面试问题及答案
查看>>
Windows Phone 7实现图片数据绑定
查看>>
桥接模式
查看>>
itellyou操作系统,office等软件的很全的下载站
查看>>
Linux 下的多线程下载工具
查看>>
hotmail在outlook2007中的设置
查看>>