云中树莓派(1):环境准备

云中树莓派(2):将传感器数据上传到AWS IoT 并利用Kibana进行展示

云中树莓派(3):通过 AWS IoT 控制树莓派上的Led

云中树莓派(4):利用声音传感器控制Led灯

1. 声音传感器及其配置

声音传感器如下图所示:

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

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

2. GPIO Event 机制

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

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

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

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

  1. GPIO.add_event_detect(channel, GPIO.RISING) # add rising edge detection on a channel
  2. do_something()
  3. if GPIO.event_detected(channel):
  4. print('Button pressed')

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

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

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

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

3.1 代码

  1. import RPi.GPIO as GPIO
  2. import time
  3. from time import sleep
  4.  
  5. SOUND_PIN_NUM = 20 #声音模块的输出引脚接的GPIO
  6. LED_PIN_NUM = 26 #LED 的长脚接的GPIO
  7.  
  8. state = 0 #保存led 的状态
  9. timeLast = time.time() #保存上次触发的时间
  10. # in one sounding, the callback function will be invoked for a few times, so need wait for some time to
  11. validDuration = 0.1
  12.  
  13. GPIO.setmode(GPIO.BCM)
  14. GPIO.setup(SOUND_PIN_NUM, GPIO.IN)
  15. GPIO.setup(LED_PIN_NUM, GPIO.OUT)
  16.  
  17. def callback_fun_soundOccurred(input_pint):
  18. global timeLast
  19. timeNow = time.time()
  20. duration = timeNow - timeLast
  21. if (duration < validDuration):
  22. print("ignored because duration " + str(duration) + " is too short")
  23. timeLast = timeNow
  24. return
  25. print("accepted for valid duration " + str(duration))
  26. timeLast = timeNow
  27. switchLed()
  28.  
  29. def switchLed():
  30. global state
  31. if (state):
  32. turnOffLed()
  33. state = 0
  34. else:
  35. turnOnLed()
  36. state = 1
  37.  
  38. def turnOnLed():
  39. print("Turn on")
  40. GPIO.output(LED_PIN_NUM,GPIO.HIGH)
  41.  
  42. def turnOffLed():
  43. print("Turn off")
  44. GPIO.output(LED_PIN_NUM, GPIO.LOW)
  45.  
  46. GPIO.add_event_detect(SOUND_PIN_NUM, GPIO.RISING, callback=callback_fun_soundOccurred)
  47.  
  48. try:
  49. while True:
  50. sleep(0.1)
  51. except KeyboardInterrupt:
  52. GPIO.remove_event_detect(SOUND_PIN_NUM)
  53. GPIO.cleanup()

3.2 两个小技巧

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

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

  1. ignored because duration 0.000501155853271 is too short
  2. ignored because duration 0.000110864639282 is too short
  3. ignored because duration 0.00215411186218 is too short
  4. ignored because duration 0.000218868255615 is too short
  5. ignored because duration 0.000470161437988 is too short
  6. ignored because duration 0.000167846679688 is too short
  7. ignored because duration 0.000583171844482 is too short
  8. ignored because duration 0.000425815582275 is too short
  9. ignored because duration 0.0010621547699 is too short
  10. ignored because duration 0.000314950942993 is too short
  11. ignored because duration 0.000555038452148 is too short
  12. ignored because duration 0.000130891799927 is too short
  13. ignored because duration 0.000461101531982 is too short
  14. ignored because duration 0.00022292137146 is too short
  15. ignored because duration 0.00274705886841 is too short
  16. ignored because duration 0.000133037567139 is too short
  17. ignored because duration 0.00597095489502 is too short
  18. ignored because duration 0.000155925750732 is too short
  19. ignored because duration 0.00107598304749 is too short
  20. ignored because duration 0.000198125839233 is too short

参考链接:

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

云中树莓派(4):利用声音传感器控制Led灯的更多相关文章

  1. 树莓派开机运行Python脚本 控制LED灯闪烁

    一.新建一个开机运行文件 在 /home/pi/.config 下创建一个文件夹,名称为 autostart,并在该文件夹下创建一个led.desktop文件(文件名以.desktop结尾) 编辑le ...

  2. C#与Arduino通过串口通信来控制LED灯的状态

    一.引言 最近摆弄了一段时间的Arduino,发现Arduino做一些电子类项目.监控.机器人.电子玩具比较容易,并且Arduino与.NET程序集成也不难.接下来介绍一个简单的小程序,C#做的一个W ...

  3. 嵌入式Linux学习入门:控制LED灯

    记录自己linux学习过程,让自己能够一直坚持下去 1.原理图分析: nLED_1, nLED_2, nLED_4, 给低电平则对应LED灯亮,高电平则对应LED灯灭, S3C2440芯片GPF4-G ...

  4. arduino 红外遥控器控制LED灯

    /* 日期:2016.9.1 功能:红外遥控器控制LED灯 开,关,闪烁,呼吸 元件: 跳线公公头 * 5 led 220欧电阻 红外接收管,红外遥控 接线: 红外灯面向自己从左到右分别接 IO3 , ...

  5. arduino入门学习实现语音控制LED灯

    需要的准备的硬件arduino+PC+麦克风实现语音命令控制LED灯的亮灭. 首先需要将写好的arduino程序烧录到arduino uno主板中,下面是代码如下: int val;//定义变量val ...

  6. BLE 安卓APP控制LED灯的实现(转)

    源:BLE 安卓APP控制LED灯的实现 //注:参考AmoMcu源代码修改. 打开APP,检查蓝牙是否打开 BluetoothAdapter mBluetoothAdapter; final Blu ...

  7. enc28J60 网页控制LED灯

    软件IDE:Arduino 1.6.3 1.库的安装: 从https://github.com/jcw/ethercard 下载源码包,解压,复制ethercard-master文件夹到Arduino ...

  8. Arduino控制LED灯(开关控制)

    问题:当使用"digitalRead(BUT) == 1"控制LED灯时会出现"digitalWrite(LED, ledState);"的值出现跳动. 原因: ...

  9. 树莓派 使用python来操作GPIO 控制LED灯

    一.创建python驱动和控制GPIO 先新建一个文件夹用于放置脚本 mkdir python_gpio 进入文件夹内新建一个gpio_blink.py的脚本 cd python_gpio touch ...

随机推荐

  1. 矩阵快速幂 51nod

    基准时间限制:3 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 给出一个N * N的矩阵,其中的元素均为正整数.求这个矩阵的M次方.由于M次方的计算结果太大,只需要输出 ...

  2. C++学习(六)(C语言部分)之 输出

    输出学习时的笔记(其实也没什么用,留着给自己看的) printf 用于输出内容 控制台黑窗口printf("要输出的内容"); //可以是任意内容-->如果要输出变量 1.格 ...

  3. 【HDOJ1069】【动态规划】

    http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java/Othe ...

  4. MYSQL 常用函数大全

    1. 数学函数 greatest(x1,x2,...,xn)返回集合中最大的值 least(x1,x2,...,xn) 返回集合中最小的值 rand()返回0到1内的随机值,可以通过提供一个参数(种子 ...

  5. gxx -L和/etc/ld.so.conf的理解

    编程之路刚刚开始,错误难免,希望大家能够指出. 今天编了个动态库,然后自己测试了一下. 忘记设置程序运行时系统搜索库的路径发生错误: 忘记设置程序编译的时候 -L 指定路径报的错误: -L : 告诉程 ...

  6. [工作记录] NDK: AKEYCODE_DEL not notified

    https://code.google.com/p/android/issues/detail?id=42904#makechanges 我们游戏的输入系统是自己渲染(通过跨平台渲染接口)的. 首先有 ...

  7. Oracle 11g direct path read 等待事件的理解

    在Oracle 11g中,全表扫描可能使用direct path read方式,绕过buffer cache,这样的全表扫描就是物理读了. 在10g中,都是通过gc buffer来读的,所以不存在di ...

  8. oracle修改字符集方法

    查看源数据库字符集 在sql命令行执行,即可查看 cat exp.dmp |od -x|head -1|awk '{print $2 $3}'|cut -c 3-6 例如我的返回结果为0362,对照以 ...

  9. Web读取指定的config文件的内容

    需求: 什么时候会用到动态改变Web.config内的值? 在Web.config定义了一个全局设置值A,因为程序运行中满足了某个条件,要将A的值改变 Web.config中定义: <appSe ...

  10. Day36 数据库的操作

    视图操作: 1.左连接查询 select * from person left join dept on person.dept_id = dept.did 2. 右连接 3. 内连接  inner ...