Example Code for Arduino-Obtain Specified Point Data via I2C

This article offers a detailed tutorial on how to obtain specified point data via the I2C communication protocol using Arduino components. It includes hardware setup, wiring instructions, and sample code to help users successfully retrieve distance data from a matrix laser sensor. The guide also provides essential troubleshooting tips and additional resources for Arduino IDE usage.

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

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_8x8) != 0){ //The default matrix mode is 8x8, which can be set to 4x4
    Serial.println("init error !!!!!");
    delay(1000);
  }
  Serial.println("init success");
}

void loop(void){

    uint16_t data = tof.getFixedPointData(1,0); //Read the distance value of the x1, y0 point, in mm
    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 > I2C > get FixedPoint

Was this article helpful?

TOP