Example Code for Arduino-IIC Output
IIC mode can be used in single module and cascade. In IIC communication mode, the controller sends a read frame to the expected query module with the specified slave address according to the IIC communication timing, and can obtain the distance and other related information of the module. In addition, you can also change the module's output mode and other parameters through IIC communication. The read frame and write frame formats follow the NLink_TOFSense_IIC_Frame0 protocol.
Hardware Preparation
- TOF Laser Ranging Sensor-7.8 meters, SEN0646, 1, DFRobot Store Purchase Link
- ESP32 Series Development Board, Any, 1, Purchase Link
Software Preparation
- Sensor Host Machine, click to download the host machine
- Arduino IDE, click to download Arduino IDE
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
IIC mode can be used in single module and cascade. In IIC communication mode, the controller sends a read frame to the expected query module with the specified slave address according to the IIC communication timing, and can obtain the distance and other related information of the module. In addition, you can also change the module's output mode and other parameters through IIC communication. The read frame and write frame formats follow the NLink_TOFSense_IIC_Frame0 protocol.
When the module is in UART mode (note that the host machine cannot recognize the module in IIC mode), connect the TOFSense 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, IIC output mode configuration is as shown in the figure, you can change the IIC slave address of the module by setting the module's ID (7-bit slave address is 0x08+module ID, ID setting range is 0~111), after configuring the parameters, you need to click the write parameter button to save the parameters. Note: After switching to IIC mode, if you need to change Band_Start, Bandwidth and other parameters, you can refer to the way in the FAQ section to change back to UART mode for configuration.
[!WARNING]
Please remember your baud rate (Baudeate) before modifying the configuration, so that you can 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 indicator
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?
