Example Code for Arduino-Read 4*4 Matrix Data via I2C

The article provides a practical guide on setting up and reading 4x4 matrix data using Arduino through I2C communication, detailing hardware requirements, wiring, and coding procedures with ESP32 and Matrix Lidar Sensor.

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 position as shown in the figure:

  • At this time, the communication mode is set to I2C mode. It can be known from the table that the I2C address at this time is 0x33.
  • Each time the I2C address is switched or the I2C/UART function is changed, the power supply must be disconnected and restarted for the change to take effect.

Wiring diagram:

Other Preparation Work

  • Ensure the I2C address is set to 0x33
  • Restart the sensor after switching modes

Sample Code

#include "DFRobot_MatrixLidar.h"
    
DFRobot_MatrixLidar_I2C tof(0x33);  //Default I2C address 0x33
uint16_t buf[16]; //A total of 16 points of data
     
void setup(void){
  Serial.begin(115200);//Set the serial communication baud rate to 115200.
  while(tof.begin() != 0){
    Serial.println("begin error !!!!!");
  }
  Serial.println("begin success");
  //config matrix mode
  while(tof.setRangingMode(eMatrix_4x4) != 0){ //Set to 4*4 mode
    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.

The horizontal direction is the X-axis from left to right, with the unit being mm. The vertical direction is the Y-axis from top to bottom.

For example, the number 12 in the data at the third row and second column in the figure is the distance value of 12 corresponding to the point (x1, y2), with the unit being mm.

Additional Information

  • This example code can also be found in ArduinoIDE under File > Examples > DFRobot_MatrixLidar > I2C > get4*4data

Was this article helpful?

TOP