距離感測器實作 – HC-SR04超聲波測距模組2020/2022版本UART/I2C介面
模組簡介
|
|
HC-SR04
|
HC-SR04 新款改良版 |
HC-SR04
2020版本 |
HC-SR04
2022版本 |
|
專用晶片 |
|
|
RCWL-9600 |
RCWL-9610 |
|
偵測距離 |
2CM ~ 400CM |
2CM ~ 400CM |
2CM ~ 450CM |
2CM ~ 450CM |
|
偵測週期 |
|
|
50mS |
50mS |
|
輸出模式 |
GPIO trig (控制端)、echo (接收端) |
GPIO trig (控制端)、echo (接收端) |
GPIO,UART 和I2C |
GPIO,UART, I2C 和,1-WIRE |
|
工作電壓 |
5V |
3V ~ 5V |
3V ~ 5V |
3V ~ 5V |
HC-SR04超聲波測距模組2020/2022版本通訊格式
l UART 模式
UART 模式波特率设置: 9600 N 1
|
命令 |
返回值 |
說明 |
|
0xA0 |
BYTE_H BYTE_M BYTE_L |
輸出距離為: ((BYTE_H<<16)+(BYTE_M<<8)+ BYTE_L)/1000 單位mm |
l I2C 模式
I2C 地址: 0x57
命令格式:
|
地址 |
命令 |
返回值 |
說明 |
|
寫地址 0xAE |
0x01 |
|
開始測距命令 |
|
讀地址 0xAF |
|
BYTE_H BYTE_M BYTE_L |
輸出距離為: ((BYTE_H<<16)+(BYTE_M<<8)+ BYTE_L)/1000 單位mm 向模組寫入0x01,模組開始測距;等待100mS(模組最大測距時間) 以上。直接讀出3 個距離資料。BYTE_H,BYTE_M 與BYTE_L。 |
圖片來源: https://shopee.tw/HC-SR04-超聲波距離測量模組-數位訊號-(可自行調整為UART、I2C通訊)-超音波距離感測-i.656213378.14047627223
圖片來源: https://shopee.tw/HC-SR04-超聲波距離測量模組-數位訊號-(可自行調整為UART、I2C通訊)-超音波距離感測-i.656213378.14047627223
電路圖
程式列表
Python - ePy-Lite_HC-SR04_2020_UART.py
|
""" ePy-Lite_HC-SR04_2020_UART.py ePy-Lite HC-SR04(Ver.2020) ----------------- 3.3V VCC P1_TX3 Trig/SCL/Rx P0_RX3 Echo/SDA/Tx GND GND """ from
machine import Switch #Get button KEY
library from
machine import Pin,
LED, UART from
machine import Timer import
utime BLEOUT
= True class HC_SR04(): def __init__(self,port=3): self.sensor=UART(port,9600) def readReg(self): self.sensor.writechar(0xA0) #
Send 0xA0, Starts to measure distance utime.sleep_ms(120) # Delay 120mS LenData =
self.sensor.read(3) data = (LenData[0]<<16) + (LenData[1]<<8) +
LenData[2] return
data // 1000 # Calculate
distance(mm) value def deinit(self): self.sensor.deinit() def key_int(): global
KeyDone KeyDone = True def tick3(timer): global
TimerDone TimerDone = True #
Start Function if
__name__ == '__main__': ledY =
LED('ledy') ledY.off() KeyA =
Switch('keya') #Create button A KeyA.callback(key_int) KeyDone = False tim_3 =
Timer(3,freq
= 5) tim_3.callback(tick3) TimerDone = False if(BLEOUT): ble =
UART(1,115200) ble.write('AT+NAME=?\\r\\n') utime.sleep_ms(50) ble.write('AT+MODE_DATA\r\n') print(ble.readline()) print(ble.readline()) Ultrasonic =
HC_SR04(3) print("Start
Ultrasonic measurement.") while True: if
TimerDone == True: ledY.on() distx =
Ultrasonic.readReg() print("Distance
is: %dmm" %distx) if
distx >0 : if(BLEOUT): ble.write('USonic
0 dist is : {} mm\r\n'.format (distx)) pass ledY.off() TimerDone = False if
KeyDone == True: #Press
A Key break Ultrasonic.deinit() KeyA.callback(None) tim_3.callback(None) tim_3.deinit() if(BLEOUT): ble.deinit() print("Exit
Ultrasonic measurement.") |
Python - ePy-Lite_HC-SR04_2020_I2C.py
|
""" ePy-Lite_HC-SR04_2020_I2C.py Reference:
https://github.com/m5stack/M5Stack/blob/master/examples/Unit/ULTRA_SONIC/ULTRA_SONIC.ino https://www.sgbotic.com/index.php?dispatch=products.view&product_id=3028 ePy-Lite HC-SR04(Ver.2020) ----------------- 3.3V VCC P17_SCL0 Trig/SCL/Rx P16_SDA0 Echo/SDA/Tx GND GND """ from
machine import Switch #Get button KEY
library from
machine import Pin,
LED, I2C,
UART from
machine import Timer import
utime BLEOUT
= True #
I2C Address HC_SR04_ADDR
= 0x57 class HC_SR04(): def __init__(self,i2c,address=0x57): self.i2c
= i2c self.Address
= address def readReg(self,
addr): LenData=bytearray(3) self.i2c.send(bytearray([addr]),self.Address) utime.sleep_ms(120) # Delay 120mS self.i2c.recv(LenData,self.Address) data = (LenData[0]<<16) + (LenData[1]<<8) +
LenData[2] return
data // 1000 # Calculate
distance(mm) value def key_int(): global
KeyDone KeyDone = True def tick3(timer): global
TimerDone TimerDone = True #
Start Function if
__name__ == '__main__': ledY =
LED('ledy') ledY.off() KeyA =
Switch('keya') #Create button A KeyA.callback(key_int) KeyDone = False tim_3 =
Timer(3,freq
= 5) tim_3.callback(tick3) TimerDone = False if(BLEOUT): ble =
UART(1,115200) ble.write('AT+NAME=?\\r\\n') utime.sleep_ms(50) ble.write('AT+MODE_DATA\r\n') print(ble.readline()) print(ble.readline()) sensor_i2c =
I2C(1,I2C.MASTER,baudrate=100000) #Create
I2C0 Master Mode, Baudrate=100kHz print("I2C
Configuration: " + str(sensor_i2c)) # Display the
basic parameters of the I2C device in the command line print('Scan
i2c bus...') devices =
sensor_i2c.scan() if len(devices) == 0: print("No
i2c device !") else: print('i2c
devices found:',len(devices)) for
device in devices: print("Hexa
address: ",hex(device)) sensor =
HC_SR04(sensor_i2c) print("Start
Ultrasonic measurement.") while True: if
TimerDone == True: ledY.on() distx =
sensor.readReg(0x01) print("Distance
is: %dmm" %distx) if
distx >0 : if(BLEOUT): ble.write('USonic
0 dist is : {} mm\r\n'.format (distx)) pass ledY.off() TimerDone = False if
KeyDone == True: #Press
A Key break sensor_i2c.deinit() KeyA.callback(None) tim_3.callback(None) tim_3.deinit() if(BLEOUT): ble.deinit() print("Exit
Ultrasonic measurement.") |
執行結果
參考文獻
l https://sparks.gogo.co.nz/assets/_site_/downloads/RCWL.zip