Example Code for Arduino-Obtain Specified Point Data via UART
This article provides a detailed guide on obtaining specified point data via UART using Arduino. It includes hardware and software preparation, wiring instructions, and sample code for using the DFRobot Matrix Lidar sensor with an ESP32 board, offering insights for tech enthusiasts to enhance their projects.
Hardware Preparation
- DFR0654: FireBeetle ESP32-E main board *1
- SEN0628: Matrix Laser Ranging Sensor *1
- PH2.0-4P data cable *1
- USB TYPE-C data cable *1
- Arduino library download link: https://github.com/DFRobot/DFRobot_MatrixLidar
Software Preparation
- Arduino IDE
Wiring Diagram
Toggle the switch to the UART position as shown in the figure:
- At this point, the communication mode is set to UART mode.
- Each time the I2C/UART function is switched, the power supply must be disconnected and restarted for the change to take effect.

Wiring diagram:

Other Preparation Work
- Ensure the communication mode is set to UART
- Restart the sensor after switching modes
- Connect the R of the sensor to D2 of ESP32-E, and T to D3
Sample Code
#include "DFRobot_MatrixLidar.h"
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
#include <SoftwareSerial.h>
#endif
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
DFRobot_MatrixLidar_UART tof(&mySerial);
#else
DFRobot_MatrixLidar_UART tof(&Serial1);
#endif
void setup(void){
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
mySerial.begin(115200);
#elif defined(ESP32)
Serial1.begin(115200, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
// Connect the R of the sensor to D2 of ESP32-E, and T to D3
#else
Serial1.begin(115200);
#endif
Serial.begin(115200);
while(tof.begin() != 0){
Serial.println("begin error !!!!!");
}
Serial.println("begin success");
//config matrix mode
while(tof.setRangingMode(eMatrix_8X8) != 0){
Serial.println("init error !!!!!");
delay(1000);
}
Serial.println("init success");
}
void loop(void){
uint16_t data = tof.getFixedPointData(1,0);
Serial.print(data);
Serial.println(" mm");
delay(10);
}
Result
Click the button in the upper right corner of the IDE to open the serial monitor, as shown in the figure:

The output data is as shown in the figure below:

Additional Information
- This example code can also be found in ArduinoIDE via File > Examples > DFRobot_MatrixLidar > UART > get FixedPoint
- The baud rate of this product's serial communication is 115200 and cannot be modified. If you need to use Arduino UNO, please use the I2C mode. The stable communication rate of UNO's software serial port is 9600, which cannot communicate stably with this product and may cause stuttering.
Was this article helpful?
