Example Code for Arduino-IIC Output

This project demonstrates how to use the TOF laser ranging sensor in IIC mode. IIC mode supports both single module and cascade. The controller sends a read frame to the module with a specified slave address to retrieve distance and other information. Users will learn how to configure the module for IIC mode and read data using an Arduino board.

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
SCL Green IO4
SDA 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.

IIC mode can be used in both single module and cascade. In IIC communication mode, the controller sends a read frame to the expected query module with a specified slave address according to the IIC communication timing, and can obtain the distance and other related information of the module. In addition, parameters such as the module's output mode can also be changed through IIC communication. The format of the read frame and write frame follows the NLink_TOFSense_IIC_Frame0 protocol.

When the module is in UART mode (note that the host computer software cannot recognize the module in IIC mode), connect the TOFSense 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. Note: After switching to IIC mode, you can refer to the method in the FAQ section to change back to UART mode.

[!WARNING]

Please remember your baud rate (Baudeate) before modifying the configuration, so as to switch back to UART mode later.

Sample Code

#include <Wire.h>  //I2C library

#define deviceaddress 0x08

typedef struct {
  uint8_t id;                //ID
  uint8_t interface_mode;    //Communication interface mode, 0-UART, 1-CAN, 2-I/O, 3-IIC
  uint32_t uart_baudrate;    //UART baud rate
  uint32_t system_time;      //System time
  float dis;                 //Distance
  uint16_t dis_status;       //Distance status indication
  uint16_t signal_strength;  //Signal strength
  uint8_t range_precision;   //Ranging accuracy
} tof_parameter;             //Parameters output by TOFSense-F

tof_parameter tof0;  //Define a structure to store the decoded data

uint8_t DATA[4] = { 0 };

uint8_t bbt[4] = { 0x00, 0xc2, 0x01, 0x00 };

void i2c_writeN(uint8_t registerAddress, uint8_t *buf, size_t len) {

  Wire.beginTransmission(deviceaddress);
  Wire.write(registerAddress);  // MSB
  Wire.write(buf, len);
  Wire.endTransmission();
  // delay(4);
}

int16_t i2c_readN(uint8_t registerAddress, uint8_t *buf, size_t len) {
  uint8_t i = 0;
  Wire.beginTransmission(deviceaddress);
  Wire.write(registerAddress);
  if (Wire.endTransmission(false) != 0) {
    return -1;
  }

  Wire.requestFrom(deviceaddress, len);
  delay(100);
  while (Wire.available()) {
    buf[i++] = Wire.read();
  }
  return i;
}

bool recdData() {
  uint8_t pdata[48];  //Read cache array
  // Serial.println(i2c_readN(0x00, iic_read_buff, 48));
  if (i2c_readN(0x00, pdata, 32) == 32) {
    if (i2c_readN(0x20, &pdata[32], 16) == 16) {
      tof0.interface_mode = pdata[0x0c] & 0x07;
      tof0.id = pdata[0x0d];
      tof0.uart_baudrate = (uint32_t)(((uint32_t)pdata[0x10]) | ((uint32_t)pdata[0x11] << 8) | ((uint32_t)pdata[0x12] << 16) | ((uint32_t)pdata[0x13] << 24));
      tof0.system_time = (uint32_t)(((uint32_t)pdata[0x20]) | ((uint32_t)pdata[0x21] << 8) | ((uint32_t)pdata[0x22] << 16) | ((uint32_t)pdata[0x23] << 24));
      tof0.dis = (float)(((uint32_t)pdata[0x24]) | ((uint32_t)pdata[0x25] << 8) | ((uint32_t)pdata[0x26] << 16) | ((uint32_t)pdata[0x27] << 24)) / 1000;
      tof0.dis_status = (uint16_t)(((uint16_t)pdata[0x28]) | ((uint16_t)pdata[0x29] << 8));
      tof0.signal_strength = (uint16_t)(((uint16_t)pdata[0x2a]) | ((uint16_t)pdata[0x2ab] << 8));
      tof0.range_precision = pdata[0x2c];
    } else
      return false;
  } else
    return false;
  return true;
}

void setup() {
  Wire.begin(5, 4);  // initialise the connection
  Serial.begin(115200);
  // i2c_writeN(0x10, bbt, 4);
}

void loop() {
  recdData();

  //Print data through the serial port
  Serial.print("id:");
  Serial.println(tof0.id);
  Serial.print("interface_mode:");
  Serial.println(tof0.interface_mode);
  Serial.print("uart_baudrate:");
  Serial.println(tof0.uart_baudrate);
  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);  //Query every 1000ms
}

Result

Output distance and other information

Was this article helpful?

TOP