Example Code for Arduino-Serial Active Output
This article offers comprehensive instructions on how to set up Arduino for serial active output, including hardware and software preparation, wiring diagrams, and sample code for TOF sensors, focusing on efficient communication and parameter configuration.
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 |
|---|---|---|
| TX | Green | IO4 |
| RX | Blue | IO5 |
| GND | Black | G |
| VCC | Red | 5V |
Other Preparation Work
UART主动输出模式只能在单个模块中使用。
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 active output mode can only be used in single module.
The interface type is set to UART, the data output mode is set to ACTIVE, and the UART active output mode is configured as shown in the figure. In this mode, the module outputs measurement information actively at a default 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 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
@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 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]; //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?
