US-100超声波模块介绍
US-100超声波测距模块可实现2cm~4.5m的非接触测距功能,拥有2.4~5.5V的宽电压输入范围,静态功耗低于2mA,自带温度传感器对测距结果进行校正,同时具有GPIO,串口等多种通信方式,内带看门狗,工作稳定可靠。
模式选择跳线接口如图所示。模式选择跳线的间距为2.54mm,当插上跳线帽时为UART(串口)模式,拔掉时为电平触发模式。
5 Pin接口从左到右依次编号1,2,3,4,5。它们的定义如下:
- 1号Pin:接VCC电源(供电范围2.4V~5.5V)。
- 2号Pin:当为UART模式时,接外部电路UART的TX端;当为电平触发模式时,接外部电路的Trig端。
- 3号Pin:当为UART模式时,接外部电路UART的RX端;当为电平触发模式时,接外部电路的Echo端。
- 4号Pin:接外部电路的地。
- 5号Pin:接外部电路的地。

本次测试采用串口模式,使用串口1作为数据收发接口,源码如下:
import time
from machine import UART
uart = UART(1, 9600) # init with given baudrate
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
while True:
time.sleep(1)
uart.read()
uart.write(b'\x55')
time.sleep(1)
buf1 = bytearray(2)
uart.readinto(buf1, 2)
distance = (buf1[0] * 256 + buf1[1])/10
print("Distance = {}cm".format(distance))
time.sleep(1)
uart.read()
uart.write(b'\x50')
time.sleep(1)
buf2 = bytearray(1)
uart.readinto(buf2, 1)
temperature = buf2[0] - 45
print("Temperature = {}℃".format(temperature))
将程序保存为us-100.py并使用Ymodem文件传输工具下载至开发板,使用`python us-100.py`命令运行程序,运行效果如下:


1118

被折叠的 条评论
为什么被折叠?



