I tried hard to get a measurement but I couldn't.
The code that i was trying is something like this:
Code: Select all
from machine import UART, Pin
import time
uart1 = UART(1, baudrate=38400, tx=10, rx=9)
uart1.init(38400, bits=8, parity=None, stop=1)
en_pwr = Pin(22, Pin.OUT)
def get_distance():
en_pwr.on()
time.sleep(1.1)
command = bytearray([0x01, 0x03, 0x00, 0x0F, 0x00, 0x02, 0xF4, 0x08])
uart1.write(command)
time.sleep(0.1)
response = uart1.read(9)
print("Sensor response:", response)
en_pwr.off()
if response and len(response) == 9:
if response[0] == 0x01 and response[1] == 0x03:
distance = (response[3] << 24) | (response[4] << 16) | (response[5] << 8) | response[6]
print("Distance (mm):", distance)
return distance
else:
print("Invalid response format")
else:
print("Invalid or no response")
return None
distance = get_distance()