Difference between revisions of "BakeBit - Sound Sensor/zh"
From FriendlyELEC WiKi
(→相关资料) |
(→硬件连接) |
||
Line 22: | Line 22: | ||
|SIG || 信号 | |SIG || 信号 | ||
|} | |} | ||
− | ==硬件连接== | + | == 示例程序:Sound Sensor == |
− | + | ||
+ | 这个示例用Sound Sensor实现一个声控开关,当检测到环境声音达到指定的分贝时,点亮LED。 | ||
+ | |||
+ | === 硬件连接 === | ||
+ | 简单的将 Sound Sensor 模块插入 A0 接口,将 LED 插入 D5 接口,如下面这样: | ||
+ | |||
+ | ::{| class="wikitable" | ||
+ | |- | ||
+ | |[[File:Sound Sensor-1.jpg |frameless|300px]] || [[File:Sound Sensor-2.jpg |frameless|300px]] | ||
+ | |- | ||
+ | |} | ||
+ | |||
+ | === 示例源代码 === | ||
+ | |||
+ | <syntaxhighlight lang="python"> | ||
+ | import time | ||
+ | import bakebit | ||
+ | |||
+ | # Connect the BakeBit Sound Sensor to analog port A0 | ||
+ | # SIG,NC,VCC,GND | ||
+ | sound_sensor = 0 | ||
+ | |||
+ | # Connect the BakeBit LED to digital port D5 | ||
+ | # SIG,NC,VCC,GND | ||
+ | led = 5 | ||
+ | |||
+ | bakebit.pinMode(sound_sensor,"INPUT") | ||
+ | bakebit.pinMode(led,"OUTPUT") | ||
+ | |||
+ | # The threshold to turn the led on 400.00 * 5 / 1024 = 1.95v | ||
+ | threshold_value = 400 | ||
+ | |||
+ | while True: | ||
+ | try: | ||
+ | # Read the sound level | ||
+ | sensor_value = bakebit.analogRead(sound_sensor) | ||
+ | |||
+ | # If loud, illuminate LED, otherwise dim | ||
+ | if sensor_value > threshold_value: | ||
+ | bakebit.digitalWrite(led,1) | ||
+ | else: | ||
+ | bakebit.digitalWrite(led,0) | ||
+ | |||
+ | print("sensor_value = %d" %sensor_value) | ||
+ | time.sleep(.2) | ||
+ | |||
+ | except IOError: | ||
+ | print ("Error") | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | [https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_sound_sensor.py Github] | ||
+ | |||
+ | |||
+ | === 运行示例 === | ||
+ | |||
+ | 假设你已经参考[http://wiki.friendlyarm.com/bakebit bakebit教程]安装了BakeBit源代码,<br /> | ||
+ | 要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_sound_sensor.py: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | cd ~/BakeBit/Software/Python | ||
+ | sudo python bakebit_sound_sensor.py | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === 运行结果 === | ||
+ | |||
+ | 对着声音传感器制造点声音,例如拍打手掌,LED将被亮起。 | ||
− | |||
− | |||
==代码说明== | ==代码说明== | ||
==相关资料== | ==相关资料== | ||
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/f/ff/02-SCHEMATIC_Sound_Sensor.pdf BakeBit - Sound Sensor.pdf]) | *[Schematic]([http://wiki.friendlyarm.com/wiki/images/f/ff/02-SCHEMATIC_Sound_Sensor.pdf BakeBit - Sound Sensor.pdf]) |
Revision as of 10:15, 28 November 2016
1 介绍
- BakeBit - Sound Sensor是一个声音检测模块,用于检测声音信号。模块使用了一个内置对声音敏感的电容式驻极体话筒检测声波信号,声波使话筒内的驻极体薄膜振动,导致电容的变化,而产生与之对应变化的微小电压,经过LM358放大后输出。
- 在默认状态下模块输出高电平,当检测到有声音的时候输出低电平,声音消失后恢复高电平,没有延时。
2 特性
- 使用标准的2.0mm 4 Pin BakeBit接口
- 引脚说明:
名称 | 描述 |
GND | 地 |
5V | 电源5V |
NC | 空 |
SIG | 信号 |
3 示例程序:Sound Sensor
这个示例用Sound Sensor实现一个声控开关,当检测到环境声音达到指定的分贝时,点亮LED。
3.1 硬件连接
简单的将 Sound Sensor 模块插入 A0 接口,将 LED 插入 D5 接口,如下面这样:
3.2 示例源代码
import time import bakebit # Connect the BakeBit Sound Sensor to analog port A0 # SIG,NC,VCC,GND sound_sensor = 0 # Connect the BakeBit LED to digital port D5 # SIG,NC,VCC,GND led = 5 bakebit.pinMode(sound_sensor,"INPUT") bakebit.pinMode(led,"OUTPUT") # The threshold to turn the led on 400.00 * 5 / 1024 = 1.95v threshold_value = 400 while True: try: # Read the sound level sensor_value = bakebit.analogRead(sound_sensor) # If loud, illuminate LED, otherwise dim if sensor_value > threshold_value: bakebit.digitalWrite(led,1) else: bakebit.digitalWrite(led,0) print("sensor_value = %d" %sensor_value) time.sleep(.2) except IOError: print ("Error")
3.3 运行示例
假设你已经参考bakebit教程安装了BakeBit源代码,
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_sound_sensor.py:
cd ~/BakeBit/Software/Python sudo python bakebit_sound_sensor.py
3.4 运行结果
对着声音传感器制造点声音,例如拍打手掌,LED将被亮起。
4 代码说明
5 相关资料
- [Schematic](BakeBit - Sound Sensor.pdf)