Example Code for Arduino-UART Active Output

This guide focuses on setting up the TOF Laser Ranging Sensor with an ESP32 board for UART active output. It includes steps for hardware and software preparation, wiring, and sample code for data reading and output. Learn how to configure the sensor for precise distance measurement at 50 Hz. Ideal for enhancing projects with accurate serial communication and sensor data integration.

Hardware Preparation

Software Preparation

Wiring Diagram

Important: Please refer to the Wire Sequence section at this link to check the product wiring sequence.

Sensor Wire Color ESP32
TX Green IO4
RX Blue IO5
GND Black G
VCC Red 5V

Other Preparation Work

Connect the TOFSense-F/F2 series products to the host computer software through the USB to TTL module (refer to the data manual for the line sequence and power supply voltage). After successful recognition, click to enter the settings page, configure the parameters and click the write parameters button to save the parameters. After successful parameter writing, you can read the parameters once to confirm whether the parameters are successfully written.

UART active output mode can only be used in single module.

The interface type is set to UART, the data output method is set to ACTIVE, and the UART active output mode is configured as shown in the figure. In this mode, the module defaults to output measurement information actively at a frequency of 50Hz, and the output format follows the NLink_TOFSense_Frame0 protocol.

Sample Code

HardwareSerial mySerial(1);

typedef struct {
  unsigned char id;               //ID of the TOF module
  unsigned long system_time;      //Time elapsed since the TOF module was powered on, in ms
  float dis;                      //Distance output by the TOF module, in meters
  unsigned char dis_status;       //Distance status indication output by the TOF module
  unsigned int signal_strength;   //Signal strength output by the TOF module
  unsigned char range_precision;  //Reference value of repeat ranging accuracy output by the TOF module, valid for TOFSense-F series, in cm
} tof_parameter;                  //Decoded TOF data structure

/**
  @brief  Read data in the serial port
  @param  buf Storage for the read data
  @param  len Number of bytes to read
  @return  The return value is the number of bytes actually read
*/
size_t readN(uint8_t *buf, size_t len);

/**
  @brief  Read a complete data packet
  @param  buf Storage for the read data
  @return  True means success, false means failure
*/
bool recdData(tof_parameter *buf);

void setup() {
  Serial.begin(115200);
  mySerial.begin(921600, SERIAL_8N1, 4, 5);  //RX,TX
}

void loop() {
  tof_parameter tof0;  //Define a structure to store the decoded data
  recdData(&tof0);
  // Print data through the serial port
  Serial.print("id:");
  Serial.println(tof0.id);
  Serial.print("system_time:");
  Serial.println(tof0.system_time);
  Serial.print("dis:");
  Serial.println(tof0.dis);
  Serial.print("dis_status:");
  Serial.println(tof0.dis_status);
  Serial.print("signal_strength:");
  Serial.println(tof0.signal_strength);
  Serial.print("range_precision:");
  Serial.println(tof0.range_precision);
  Serial.println("");

  delay(1000);
}

size_t readN(uint8_t *buf, size_t len) {
  size_t offset = 0, left = len;
  int16_t Tineout = 1500;
  uint8_t *buffer = buf;
  long curr = millis();
  while (left) {
    if (mySerial.available()) {
      buffer[offset] = mySerial.read();
      offset++;
      left--;
    }
    if (millis() - curr > Tineout) {
      break;
    }
  }
  return offset;
}

bool recdData(tof_parameter *buf) {
  uint8_t rx_buf[16];  //Serial receive array
  int16_t Tineout = 5000;
  uint8_t ch;
  bool ret = false;
  uint8_t Sum;
  while (mySerial.available() > 0) {
    mySerial.read();
  }
  long timeStart = millis();

  while (!ret) {
    if (millis() - timeStart > Tineout) {
      break;
    }
    if (readN(&ch, 1) == 1) {
      if (ch == 0x57) {
        rx_buf[0] = ch;
        if (readN(&ch, 1) == 1) {
          if (ch == 0x00) {
            rx_buf[1] = ch;
            if (readN(&rx_buf[2], 14) == 14) {
              Sum = 0;
              for (int i = 0; i < 15; i++)
                Sum += rx_buf[i];
              if (Sum == rx_buf[15]) {
                buf->id = rx_buf[3];                                                                                                                                                   //Take the id of the TOF module
                buf->system_time = (unsigned long)(((unsigned long)rx_buf[7]) << 24 | ((unsigned long)rx_buf[6]) << 16 | ((unsigned long)rx_buf[5]) << 8 | (unsigned long)rx_buf[4]);  //Take the time elapsed since the TOF module was powered on
                buf->dis = ((float)(((long)(((unsigned long)rx_buf[10] << 24) | ((unsigned long)rx_buf[9] << 16) | ((unsigned long)rx_buf[8] << 8))) / 256)) / 1000.0;                 //Take the distance output by the TOF module
                buf->dis_status = rx_buf[11];                                                                                                                                          //Take the distance status indication output by the TOF module
                buf->signal_strength = (unsigned int)(((unsigned int)rx_buf[13] << 8) | (unsigned int)rx_buf[12]);                                                                     //Take the signal strength output by the TOF module
                buf->range_precision = rx_buf[14];                                                                                                                                     //Take the reference value of repeat ranging accuracy output by the TOF module
                ret = true;
              }
            }
          }
        }
      }
    }
  }
  return ret;
}

Result

Output distance and other information

Was this article helpful?

TOP