BakeBit - Ultrasonic Ranger
From FriendlyELEC WiKi
Contents
1 Introduction
- The BakeBit - Ultrasonic Ranger is a ultrasonic module.The module's sensor emits a sound wave whose wave length is around 6mm and frequency is 40K Hz, which bounces off a reflective surface and returns to the sensor. Then, using the amount of time it takes for the wave to return to the sensor, the distance to the object can be computed.The receiver converts ultrasound waves to electrical signals in mV.
- The master sends a signal to the module starting to emit a sound wave. After the module receives the returned signal it will will generate a high level indicating the elapsed time and the distance will be calculated by distance = (elapsed time * speed of sound)/2.
2 Hardware Spec
- Standard 2.0mm pitch 4-Pin BakeBit Interface
- Range: 5cm - 300cm
- Accuracy: 1cm
- PCB dimension(mm): 24 x 42
- Pin Desription:
Pin | Description |
GND | Ground |
5V | 5V Supply Voltage |
NC | Not Connected |
SIG | Signal |
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 相关资料
- [Schematic](BakeBit - Ultrasonic Ranger.pdf)
- [BakeBit Github项目](https://github.com/friendlyarm/BakeBit)
- [BakeBit Starter Kit手册](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_cn.pdf)