Example Code for Arduino-Serial

Last revision 2025/12/31

This example is designed for use on an Arduino Mega2560. It prints the distance information between the measured object and the sensor via serial port on a PC.

Hardware Preparation

Software Preparation

Wiring Diagram(UART)

Use the serial port software on the PC end to display the detected distance and power the entire system.

left PIN Name Sensor right (Pin numbers, from left to right )
Arduino Mega Due VCC VCC SEN0524 1(VCC)
Arduino Mega Due GND GND SEN0524 2(GND)
Arduino Mega Due TX Pin 4 SEN0524 3(RX)
Arduino Mega Due RX Pin3 SEN0524 4(TX)

Sample Code

#define TOF_FRAME_HEADER 0x57                 //Define the frame header for the TOFSense series and TOFSense-F series
#define TOF_FUNCTION_MARK 0x00                //Define the function codes for the TOFSense series and TOFSense-F series

typedef struct {
  unsigned char id;                           //TOF id
  unsigned long system_time;                  //msPower-on time, unit: ms
  float dis;                                  //Output distance, unit: meters
  unsigned char dis_status;                   //Distance status indication of the output
  unsigned int signal_strength;               //Signal strength of the output
  unsigned char range_precision;              //cmReference value for the repeating ranging accuracy of the output, unit: cm
} tof_parameter;

unsigned int count_i,count_j=0;
tof_parameter tof0;
unsigned char check_sum=0;
unsigned char rx_buf[32];

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  
  if(Serial1.available()>0)                   
  {
    if(Serial1.peek() == TOF_FRAME_HEADER)  
    {
      count_i=0;  
      rx_buf[count_i]=Serial1.read(); 
    }
    else
    {
      rx_buf[count_i]=Serial1.read(); 
    }
        
    count_i++;  
    
    if(count_i>15)  
    {
      count_i=0;

      for(count_j=0;count_j<15;count_j++)
      {
        check_sum+=rx_buf[count_j]; 
      }

      if((rx_buf[0] == TOF_FRAME_HEADER)&&(rx_buf[1] == TOF_FUNCTION_MARK)&&(check_sum == rx_buf[15]))  
      {
        tof0.id=rx_buf[3];  
        tof0.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]);      
        tof0.dis=((float)(((long)(((unsigned long)rx_buf[10]<<24)|((unsigned long)rx_buf[9]<<16)|((unsigned long)rx_buf[8]<<8)))/256))/1000.0;
        tof0.dis_status=rx_buf[11];
        tof0.signal_strength=(unsigned int)(((unsigned int)rx_buf[13]<<8)|(unsigned int)rx_buf[12]);
        tof0.range_precision=rx_buf[14];

      
        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("");
      }
      
    }
    check_sum=0;
  }  
  
}


Result

The serial port software displays the data format as follows.

Was this article helpful?

TOP