RT-Thread Micropython US-100超聲波測距模塊測試


  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`命令運行程序,運行效果如下:




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM