Example Code for Arduino-Serial Query Output

This article offers a comprehensive guide on executing serial query output using Arduino, featuring hardware and software preparations, wiring diagrams, and sample code to effectively use TOF sensors with ESP32 development boards.

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 machine software through the USB to TTL module (refer to the data manual for line sequence and power supply voltage), after successful identification, click to enter the setting page, configure the parameters and click the write parameter button to save the parameters, after writing the parameters successfully, you can read the parameters once to confirm whether the parameters are written successfully.

UART query output mode can be used in single module.

The interface type is set to UART, the data output mode is set to INQUIRE, and the UART query output mode is configured as shown in the figure. After writing the parameters, the data will no longer be reported actively. In this mode, the controller needs to send a query command containing the module ID to the expected query module, and the module can output a frame of measurement information. The query frame format follows the NLink_TOFSense_Read_Frame0 protocol, and the output frame format follows the NLink_TOFSense_Frame0 protocol.

Sample Code

HardwareSerial mySerial(1);

typedef struct {
  unsigned char id;               //ID of TOF module
  unsigned long system_time;      //The time elapsed after the TOF module is powered on, unit: ms
  float dis;                      //The distance output by the TOF module, unit: m
  unsigned char dis_status;       //Distance status indicator output by the TOF module
  unsigned int signal_strength;   //Signal strength output by the TOF module
  unsigned char range_precision;  //Reference value of repeated ranging accuracy output by the TOF module, valid for TOFSense-F series, unit: cm
} tof_parameter;                  //Data structure after decoding TOF data

/**
  @brief  Read data from the serial port
  @param  buf Stores the read data
  @param  len The number of bytes to read
  @return  The return value is the actual number of bytes read
*/
size_t readN(uint8_t *buf, size_t len);

/**
  @brief  Read a complete data packet
  @param  buf Stores the read data
  @param  id Module ID
  @return  True means success, false means failure
*/
bool recdData(tof_parameter *buf, uint8_t id = 0);

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, 0);
  // Print data through 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 id) {
  uint8_t rx_buf[16];  //Serial receive array
  int16_t Tineout = 5000;
  uint8_t ch;
  bool ret = false;
  uint8_t Sum;
  uint8_t cmdBuf[8] = { 0x57, 0x10, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00 };
  cmdBuf[4] = id;
  cmdBuf[7] = 0;
  for (int i = 0; i < 7; i++)
    cmdBuf[7] += cmdBuf[i];
  long timeStart = millis();
  long timeStart1 = 0;

  while (!ret) {
    if (millis() - timeStart > Tineout) {
      break;
    }

    if ((millis() - timeStart1) > 1000) {
      while (mySerial.available() > 0) {
        mySerial.read();
      }
      mySerial.write(cmdBuf, 8);
      timeStart1 = millis();
    }

    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];                                                                                                                                                   //Get 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]);  //Get the time elapsed after the TOF module is 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;                 //Get the distance output by the TOF module
                buf->dis_status = rx_buf[11];                                                                                                                                          //Get the distance status indicator output by the TOF module
                buf->signal_strength = (unsigned int)(((unsigned int)rx_buf[13] << 8) | (unsigned int)rx_buf[12]);                                                                     //Get the signal strength output by the TOF module
                buf->range_precision = rx_buf[14];                                                                                                                                     //Get the reference value of the repeated ranging accuracy output by the TOF module
                ret = true;
              }
            }
          }
        }
      }
    }
  }
  return ret;
}

Result

Output distance and other information

Was this article helpful?

TOP