haas506 2.0开发教程-example-超声波测距
最新案例教程点击下方链接跳转,CSDN已停止更新
点
击
跳
转
1.JSN-SR04T超声波模块
(1)模块说明
- 规格参数- 功能说明
- 模式0:Mode=悬空 高电平(PWM)脉宽输出
- 引脚定义
Pin | 说明 |
---|---|
Trig | 触发控制引脚 |
Echo | 高电平脉宽输出 |
(2)工作原理
使用IO口Trig触发测距,至少给10us的高电平。模块自动发送8个40KHZ的方波,自动检测是否有信号返回。当有信号返回时,通过IO口Echo输出一个高电平,高电平持续的时间就是超声波从发射到返回的时间。常温下测试距离=(高电平时间*声速(348M/S))/2。模块被触发测距后,如果接收不到回波(原因超过所测范围或是探头没有正对被测物),ECHO 口会在 40MS 后自动变为低电平,标志着此次测量结束,不论成功与否。LED 上电时末亮,只有在给了 TRIG 引脚触发信号后才亮,灯闪频率与触发周期同步,说明此时模块接收到正确的指令进入工作状态。建议测量周期为 50ms 以上,以防止发射信号对回响信号的影响。
简单来说就是主板通过IO口Trig给个不小于10us的高电平,触发测距。测距成功,模块通过IO口Echo输出一个高电平给主板;测距失败,ECHO 口会在 40MS 后自动结束本次测量。
- 其他模式可以百度一下,查看模块手册
2.测距
(1) 接线
JSN-SR04T超声波模块,有四个pin脚,分别是5V、GND、Trig、Echo。将这4个pin脚分别接入到haas506开发板上,io口可以自行定义。当前案例,将Trig接到了GPIO18,Echo接到了GPIO17,5v和GND接到了开发板的5V和GND上。
需要注意的是,5v脚接入到开发板电源3.3v或5v都行,但不能接入开发板的排母中的io口上。因为排母处的io口的输出电压为1.8V,电压低,无法触发测距。
(2) 代码
- main.py
#Import library
from driver import GPIO
import utime as time
#creat instances
TRIG = GPIO()
ECHO = GPIO()
TRIG.open('trig') # Set pin as GPIO out
ECHO.open('echo') # Set pin as GPIO in
def getData():
TRIG.write(0)
#set period,above 50ms
time.sleep_ms(500)
TRIG.write(1) #Set TRIG as LOW
time.sleep_us(10) #Delay of 10us
TRIG.write(0) #Set TRIG as HIGH
while ECHO.read()==0: #Check if Echo is LOW
pass
pulse_start = time.ticks_us() #Time of the last LOW pulse
while ECHO.read()==1: #Check whether Echo is HIGH
pass
pulse_end = time.ticks_us() #Time of the last HIGH pulse
#print("pulse_duration",time.ticks_diff(pulse_end,pulse_start)) # us
pulse_duration = time.ticks_diff(pulse_end,pulse_start)/10000 #pulse duration: s
d=pulse_duration*340/2
return d
if __name__=="__main__":
try:
while True:
print ("-----measure distance test-----")
if getData() > 20 and getData() < 600:
print ("distance:%0.2f cm" % getData())
else:
print ("Out Of Range")
except KeyboardInterrupt:
print("exit")
- board.json
{
"name": "haas506",
"version": "2.0.0",
"io": {
"ADC0": {
"type": "ADC",
"port": 0,
"sampling": 12000000
},
"ADC1": {
"type": "ADC",
"port": 1,
"sampling": 12000000
},
"qma8981": {
"type": "I2C",
"port": 1,
"addrWidth": 7,
"freq": 400000,
"mode": "master",
"devAddr": 18
},
"KEY1": {
"type": "GPIO",
"port": 44,
"dir": "irq",
"pull": "pullup",
"intMode": "rising"
},
"led1": {
"type": "GPIO",
"port": 7,
"dir": "output",
"pull": "pulldown"
},
"led_g": {
"type": "GPIO",
"port": 32,
"dir": "output",
"pull": "pulldown"
},
"cs": {
"type": "GPIO",
"port": 15,
"dir": "output",
"pull": "pullup"
},
"echo":{
"type":"GPIO",
"port": 17,
"dir": "input",
"pull":"pullup"
},
"trig":{
"type":"GPIO",
"port": 18,
"dir": "output",
"pull":"pullup"
},
"SPI0": {
"type": "SPI",
"port": 0,
"mode": "master",
"freq": 2000000
},
"serial1": {
"type": "UART",
"port": 0,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial2": {
"type": "UART",
"port": 1,
"dataWidth": 8,
"baudRate": 9600,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial3": {
"type": "UART",
"port": 2,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
}
},
"debugLevel": "ERROR",
"repl": "enable",
"replPort": 0
}
3.效果
- 串口工具大概0.5s会更新一次当前测试的距离数据。
- 日志输出