Example Code for Raspberry Pi-Laser Ranging

This project demonstrates how to connect the SEN0492 Laser Ranging Sensor RS485 (4-400cm) to a Raspberry Pi and read the accurate measured distance. Users will learn how to use the wiringPi library for serial communication and interact with the sensor using the Modbus-RTU protocol.

Hardware Preparation

  • Raspberry Pi, Quantity: 1
  • 6-in-1 multi-function to serial port module (SKU: DFR0829), Quantity: 1, Purchase Link: DFRobot
  • or USB to RS485 module (SKU: DFR0572), Quantity: 1, Purchase Link: DFRobot
  • Laser Ranging Sensor RS485 (4-400cm) (SKU: SEN0492), Quantity: 1, Purchase Link: DFRobot

Software Preparation

  • Development Environment: Raspberry Pi OS (any recent version)
  • Libraries: wiringPi library (installation steps provided in Other Preparation Work)
  • Tools: Terminal (for command execution), Text editor (e.g., nano, vim, or Geany)

Wiring Diagram

Other Preparation Work

  1. Check USB Device Port:
    Enter the following command in the terminal to find the USB-to-RS485 module port (the port may change on each connection):
    sudo ls -l /dev

  2. Install wiringPi Library:
    Run these commands to download and install the wiringPi library:
    cd /tmp
    wget https://project-downloads.drogon.net/wiringpi-latest.deb //Download wiringpi library
    sudo dpkg -i wiringpi-latest.deb //Install wiringpi library

  3. Prepare the Code File:
    Create a new folder on the Raspberry Pi desktop, then create a file named LaserRanging.c inside it. Copy the sample code (provided in Sample Code) into this file.

Sample Code

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <wiringPi.h>

#include <wiringSerial.h>

int recData(unsigned char* buf);
unsigned int CRC16_2(unsigned char *buf, int len);

int fd;

unsigned char Data[8] = {0};

int main()
{
    if ((fd = serialOpen("/dev/ttyUSB0", 115200)) < 0) {     //You can change usb device port in line with the actual conditions here.
        fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno));
        return 0;
    }
    while(1){
        delay(100);
        printf("%d mm\n",recData(Data));
    }
    return 1;

}

int recData(unsigned char* buf)
{
    char ret = 0;
    int jl=0;
    long curr = millis();
    char ch = 0;
    unsigned char COM[8]={0x50, 0x03, 0x00, 0x34, 0x00, 0x01, 0xC8, 0x45};
    write(fd, COM, 8);
    while(!ret){
        if (millis() - curr > 1000){
            //write(fd, COM, 8);
            curr = millis();
            printf("OK\n");
        }

        if(serialDataAvail(fd) > 0){
            delay(10);
            if (read(fd, &ch, 1) == 1){
                if(ch == 0x50){
                    buf[0] = ch;
                    if (read(fd, &ch, 1) == 1){
                        if(ch == 0x03){
                            buf[1] = ch;
                            if (read(fd, &ch, 1) == 1){
                                if(ch == 0x02){
                                    buf[2] = ch;
                                    if (read(fd, &buf[3], 4) == 4){
                                        // for(int i=0; i<7; i++){
                                        //     if(buf[i] < 0x10){
                                        //         printf("0");
                                        //     }
                                        //     printf("%x ", buf[i]);
                                        // }
                                        // printf("\n");
                                        // printf("%x\n", CRC16_2(buf, 5));
                                        // printf("%x\n", buf[5]*256+buf[6]); //Used to view the raw data of the distance measured by the sensor.
                                        if(CRC16_2(buf, 5) == (buf[5] * 256 + buf[6])){
                                            jl = buf[3]*256+buf[4];
                                            ret = 1;
                                        }

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return jl;
}

unsigned int CRC16_2(unsigned char *buf, int len)
{
  unsigned int crc = 0xFFFF;
  for (int pos = 0; pos < len; pos++)
  {
    crc ^= (unsigned int)buf[pos];
    for (int i = 8; i != 0; i--)
    {
      if ((crc & 0x0001) != 0)
      {
        crc >>= 1;
        crc ^= 0xA001;
      }
      else 
      {
        crc >>= 1; 
      }
    }
  }

  crc = ((crc & 0x00ff) << 8) | ((crc & 0xff00) >> 8);
  return crc;
}

Result

After compiling the code with:
gcc -Wall -lwiringPi -o LaserRanging LaserRanging.c

and running the executable:
./LaserRanging

You will see the measured distance (in millimeters) printed in the terminal. A sample output is shown below:

Additional Information

  • USB Port Note: The USB device port (e.g., /dev/ttyUSB0) may change when reconnecting the USB-to-RS485 module. Always verify the port with sudo ls -l /dev before running the program.
  • wiringPi Dependency: The code requires the wiringPi library for serial communication. Ensure it is installed correctly using the commands in Other Preparation Work.

Was this article helpful?

TOP