It take me a couple of hours to find documentation for this sensor. But finally i have working code for the Arduino esp32 and I just want to share it here:
/* Arduino ESP32 code to read the tof200h sensor
Edgar Mondragón 2023 @montdraco_
WTFPL license
*/
// Define the serial port
HardwareSerial SerialPort(1); // Using Serial1 on pins 12 (TX) and 13 (RX)
void setup() {
Serial.begin(115200); // Initialize the serial monitor for debugging
SerialPort.begin(115200, SERIAL_8N1, 12, 13); // Initialize the serial port for communication
}
void loop() {
if (SerialPort.available() >= 7) {
// Check if there's enough data to extract the range
byte data[7];
SerialPort.readBytes(data, 7);
// Extract the range from bytes at positions 3 and 4
int range = (data[3] << 8) | data[4];
// Print the range and the modbus data buffer to the serial monitor for debugging
Serial.print(" Buffer: ");
for (int i = 0; i < 7; i++) {
Serial.print("0x");
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" Range: ");
Serial.println(range);
}
}