Help! I cannot get this to give me any measurement data. I need help with setup. I am connecting this sensor to an Arduino using a Max485 TTL to RS-485 interface module.
JaneYu 2022-10-24 11:14:57 31 Views5 Replies Help! I cannot get this to give me any measurement data. I need help with setup. I am connecting this sensor to an Arduino using a Max485 TTL to RS-485 interface module.
I had an issue with range mode 1 (1.5m) only reading once on power up and then reading zero about 10 times before sending the first read value again. Value changed only on power cycle.
Switched to range mode 2 (3m) and got readings every 10 ms as expected.
Here is the python code I got working to read the sensor on windows pc. It initializes to range 2 (3m) and reads every 10 ms. You'll have to update the COM port you use on line 4 manually.
import time
import serial
com_port = 'COM3'
def rec_data(ser):
jl = 0
COM = bytearray([0x50, 0x03, 0x00, 0x34, 0x00, 0x01, 0xC8, 0x45]) #command to read data
ser.write(COM)
if ser.in_waiting >= 7:
data = ser.read(7)
if data[0] == 0x50 and data[1] == 0x03 and data[2] == 0x02 and CRC16_2(data, 5) == (data[5] * 256 + data[6]):
jl = data[3] * 256 + data[4]
return jl
def initialize_range(ser):
range_command = bytearray([0x50, 0x06, 0x00, 0x36, 0x00, 0x02, 0xE5, 0x84]) #command to set range to 3m.
ser.write(range_command)
if ser.in_waiting >= 8:
data = ser.read(8)
time.sleep(0.1)
def CRC16_2(buf, length):
crc = 0xFFFF
for pos in range(length):
crc ^= buf[pos]
for i in range(8, 0, -1):
if crc & 0x0001:
crc >>= 1
crc ^= 0xA001
else:
crc >>= 1
crc = ((crc & 0x00ff) << 8) | ((crc & 0xff00) >> 8)
return crc
if __name__ == "__main__":
try:
ser = serial.Serial(com_port, 115200, timeout=1) #Change to current COM port.
time.sleep(2)
initialize_range(ser)
while True:
distance = rec_data(ser)
print(f"{distance} mm")
time.sleep(0.1)
except serial.SerialException as e:
print(f"Unable to open serial port: {e}")
finally:
if ser.is_open:
ser.close()
JaneYu Can you provide an sample code for a circuit with Opta finder, or generic for Arduino?
Please help :)
JaneYu Sorry we only got the Raspberry Pi sample code and the protocal sheet for this sensor.
I successfully changed the code for a different sensor to work with this one and an arduino uno . I have pasted in in the comments of a forumn post i made when trying to solve the puzzle. I am a Arduino newbie so use the code at your own risk, but it works with my probes. https://www.dfrobot.com/for...
What code did you upload? Please ensure that the voltage is stable. It is recommended to use external power supply
JaneYu 

