Difference between revisions of "BakeBit - Ultrasonic Ranger/zh"
From FriendlyELEC WiKi
(→介绍) |
(→硬件连接) |
||
Line 26: | Line 26: | ||
|SIG || 信号 | |SIG || 信号 | ||
|} | |} | ||
− | == | + | == 示例程序:Ultrasonic Sensor with LED == |
− | + | ||
− | + | 这个示例演示使用距离传感器来探测障碍物,当探测到前方有障碍物时,亮起LED进行报警。 | |
− | [[File: | + | |
+ | === 硬件连接 === | ||
+ | 简单的将 LED 模块插入 D3接口,将 距离传感器 插入 D4 接口,如下面这样: | ||
+ | |||
+ | ::{| class="wikitable" | ||
+ | |- | ||
+ | |[[File:Ultrasonic Sensor with LED-1.jpg |frameless|300px]] || [[File:Ultrasonic Sensor with LED-1.jpg |frameless|300px]] | ||
+ | |- | ||
+ | |} | ||
+ | |||
+ | === 示例源代码 === | ||
+ | |||
+ | <syntaxhighlight lang="python"> | ||
+ | import bakebit | ||
+ | import time | ||
+ | |||
+ | # Connect the BakeBit Ultrasonic Ranger to digital port D4 | ||
+ | # SIG,NC,VCC,GND | ||
+ | ultrasonic_ranger = 4 | ||
+ | |||
+ | # Connect the BakeBit LED to digital port D3 | ||
+ | led = 3 | ||
+ | |||
+ | bakebit.pinMode(led,"OUTPUT") | ||
+ | light = 0 | ||
+ | |||
+ | while True: | ||
+ | try: | ||
+ | # Read distance value from Ultrasonic | ||
+ | distance = bakebit.ultrasonicRead(ultrasonic_ranger) | ||
+ | print(distance) | ||
+ | if distance > 0: | ||
+ | if distance<10: | ||
+ | if light == 0: | ||
+ | print("\ton") | ||
+ | bakebit.digitalWrite(led,1) | ||
+ | light = 1 | ||
+ | else: | ||
+ | if light == 1: | ||
+ | print("\toff") | ||
+ | bakebit.digitalWrite(led,0) | ||
+ | light = 0 | ||
+ | time.sleep(.2) | ||
+ | |||
+ | except KeyboardInterrupt: | ||
+ | bakebit.digitalWrite(led,0) | ||
+ | break | ||
+ | |||
+ | except TypeError: | ||
+ | print ("Error") | ||
+ | except IOError: | ||
+ | print ("Error") | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | [https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_prj_Ultrasonic_Sensor_with_LED.py Github] | ||
+ | |||
+ | |||
+ | === 运行示例 === | ||
+ | |||
+ | 假设你已经参考[http://wiki.friendlyarm.com/bakebit bakebit教程]安装了BakeBit源代码,<br /> | ||
+ | 要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_Ultrasonic_Sensor_with_LED.py: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | cd ~/BakeBit/Software/Python | ||
+ | sudo python bakebit_prj_Ultrasonic_Sensor_with_LED.py | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === 运行结果 === | ||
+ | |||
+ | 当距离传感器检测到前方10厘米处有障碍物时,LED将被点亮来告警,没有障碍物挡住时LED自动熄灭。 | ||
==代码说明== | ==代码说明== | ||
==相关资料== | ==相关资料== | ||
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/e/e0/12-SCHEMATIC_Ultrasonic.pdf BakeBit - Ultrasonic Ranger.pdf]) | *[Schematic]([http://wiki.friendlyarm.com/wiki/images/e/e0/12-SCHEMATIC_Ultrasonic.pdf BakeBit - Ultrasonic Ranger.pdf]) |
Revision as of 10:30, 28 November 2016
Contents
1 介绍
- BakeBit - Ultrasonic Ranger是超声波模块,模块使用了一个人超声波发射器和一个接收器,经发射器发射出波长约6mm,频率为40KHZ的超声波信号,在传输的过程中,超声波碰到障碍物会反射形成反射回波,接收器接收到反射回波,通过压电效应的换能器产生mV级的微弱电压信号。
- 给模块发送一个触发信号,模块开始发出超声波测距,同时在输出脚产生高电平,当检测到反射回波后,模块输出脚输出低电平。模块输出的高电平信号持续的时间就是超声波在空气中传播的时间,按照测试距离=(高电平时间*声速(340M/S))/2就可以算出超声波到障碍物的距离。
2 特性
- 使用标准的2.0mm 4 Pin BakeBit接口
- 测试距离:5cm-300cm
- 测试精度:1cm
- PCB尺寸(mm):24x42
- 引脚说明:
名称 | 描述 |
GND | 地 |
5V | 电源5V |
NC | 空 |
SIG | 信号 |
3 示例程序:Ultrasonic Sensor with LED
这个示例演示使用距离传感器来探测障碍物,当探测到前方有障碍物时,亮起LED进行报警。
3.1 硬件连接
简单的将 LED 模块插入 D3接口,将 距离传感器 插入 D4 接口,如下面这样:
3.2 示例源代码
import bakebit import time # Connect the BakeBit Ultrasonic Ranger to digital port D4 # SIG,NC,VCC,GND ultrasonic_ranger = 4 # Connect the BakeBit LED to digital port D3 led = 3 bakebit.pinMode(led,"OUTPUT") light = 0 while True: try: # Read distance value from Ultrasonic distance = bakebit.ultrasonicRead(ultrasonic_ranger) print(distance) if distance > 0: if distance<10: if light == 0: print("\ton") bakebit.digitalWrite(led,1) light = 1 else: if light == 1: print("\toff") bakebit.digitalWrite(led,0) light = 0 time.sleep(.2) except KeyboardInterrupt: bakebit.digitalWrite(led,0) break except TypeError: print ("Error") except IOError: print ("Error")
3.3 运行示例
假设你已经参考bakebit教程安装了BakeBit源代码,
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_Ultrasonic_Sensor_with_LED.py:
cd ~/BakeBit/Software/Python sudo python bakebit_prj_Ultrasonic_Sensor_with_LED.py
3.4 运行结果
当距离传感器检测到前方10厘米处有障碍物时,LED将被点亮来告警,没有障碍物挡住时LED自动熄灭。
4 代码说明
5 相关资料
- [Schematic](BakeBit - Ultrasonic Ranger.pdf)