Example Code for Arduino-Obtain 4*4 Matrix Data via UART
This article provides comprehensive instructions on setting up hardware and software to obtain 4x4 matrix data using Arduino via UART, including detailed wiring diagrams and sample code for successful communication and data collection.
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
uint16_t buf[16];
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_4x4) != 0){
Serial.println("init error !!!!!");
delay(1000);
}
Serial.println("init success");
}
void loop(void){
tof.getAllData(buf);
for(uint8_t i = 0; i < 4; i++){
Serial.print("Y");
Serial.print(i);
Serial.print(": ");
for(uint8_t j = 0; j < 4; j++){
Serial.print(buf[i * 4 + j]);
Serial.print(",");
}
Serial.println("");
}
Serial.println("------------------------------");
delay(100);
}
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 > get4 4data
- 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?
