Ultrasonic Ranging Sensor(5m) Arduino WiKi - DFRobot

Introduction

This is an ultrasonic obstacle avoidance sensor designed for robot automation control applications. It addresses the issues commonly found in the current market such as large blind zones, wide measurement angles, long response times, and poor installation adaptability for ultrasonic sensors. With a measurement range of up to 5m, it offers advantages such as small blind zones, narrow measurement angles, short response times, filtering of same-frequency interference, compact size, high installation adaptability, dust and waterproof capabilities, long lifespan, and high reliability.

Utilizing innovative ultrasound processing technology, it can identify and filter out foreign ultrasound, resulting in more stable and reliable data output. This greatly reduces the probability of same-frequency interference, meeting the complex environmental application requirements in the field of robot automation control.

It features RS4585 output for sensor data, and users can use it in conjunction with Arduino's TTL to RS485 expansion board (DFR0259). It finds wide applications in robot obstacle avoidance, automatic control, parking management systems, security gate detection, access control systems, and object proximity and presence detection.

Features

Specification

Board Overview

Num Label Description
Red line VCC power supply input positive pole
Black line GND power ground wire
Yellow line B RS485 B line
white line A RS485 A line

Dimensional Drawing

Installation opening suggestion

Modbus protocol description

1、Default communication parameters

Data bit stop bit parity baud rate
8 1 No default 115200

2、Modbus protocol parameters

Mode check sensor address reading function code writing function code
Modbus-RTU CRC-16/Modbus can be set, and the default value is 0x01 0x03 0x06

3、Modbus protocol format

The user machine is the master device and the sensor is the slave device.

Host send (read):

Name Device address Function code 0x03 Register address Number of registers CRC16 check
Length(Byte) 1 1 2 2 2

Slave response (read):

Name Device address Function code 0x03 Number of bytes returned Data area CRC16 check
Length(Byte) 1 1 1 N 2

Host send (write):

Name Device address Function code 0x03 Register address Data area CRC16 check
Length(Byte) 1 1 2 2 2

Slave response (write):

Name Device address Function code 0x03 Register address Data area CRC16 check
Length(Byte) 1 1 2 2 2

4、Modbus register

Register data is high byte before and low byte after.

Permission Address Function Data Type Description
Read-only 0x0100 Processing value Unsigned integer, 16 bits Start ranging after receiving the instruction, and output the distance value in mm with a response time of about 190 ~ 750 ms (different ranges vary)
Read-only 0x0101 Real-time value Unsigned integer, 16-bit The module starts ranging once after receiving the instruction, and outputs the real-time distance value in mm, and the response time is about 15 ~ 140 ms (different ranges vary)
Read-only 0x0102 Temperature Unsigned integer, 16-bit Unit: 0.1℃, resolution: 0.5℃, response time about 5 ~ 140 ms (range varies)
Read-only 0x010A Echo Time Unsigned integer, 16-bit The module starts ranging once after receiving the instruction, and outputs the real-time echo time in us. This value is divided by 5.75 to get the distance value in mm, and the response time is about 5 ~ 140 ms (different ranges vary)
Permission Address Function Data Type Description
Read/write 0x0200 Slave address Unsigned integer, 16 bits Range: 0x01~0xFE (default 0x01), 0xFF is the broadcast address
Read/write 0x0201 Baud rate Unsigned integer, 16 bits Serial baud rate (default 115200), in bps, which will take effect immediately after setting. The baud rates corresponding to the register values are as follows: 0x0002: 4800, 0x0003: 9600, 0x0004: 14400, 0x0005: 10.
Read and write 0x0205 Switch output polarity Unsigned integer, 16 bits Set the switch output polarity, only the switch mode is valid; 0x00: negative output, less than the threshold value, low output; 0x01: Positive output, less than the threshold output high (default)
Read and write 0x0206 Set the value of switch value Unsigned integer, 16 bits Set the threshold value of switch value, unit: mm, range: 30 ~ 5000 mm, only the switch mode is valid
Read and write 0x0208 Detect angle level Unsigned integer, 16 bits The angle level can be set to level 1 ~ 4 (default level 4); The greater the grade, the greater the detection angle, the more sensitive the induction, and vice versa. 1- The single angle is about 40, the horizontal angle of double angle is about 45, and the vertical angle is about 60; 2- Single angle is about 45, double angle is about 50, and vertical angle is about 65; 3- The single angle is about 55, the horizontal angle of double angle is about 55, and the vertical angle is about 70; 4- Single angle is about 65, double angle is about 65, and vertical angle is about 80;
Read and write 0x0209 Output distance value data unit Unsigned integer, 16 bits Controlled/automatic output protocol distance value unit, 0x00-mm, 0x01-us (this value is divided by 5.75 to get the distance value in mm units), which is only valid for UART automatic and UART controlled modes
Read and write 0x021A Power noise reduction level Unsigned integer, 16-bit Power noise reduction level is divided into 1-5 levels (default is 1), which is suitable for different power supply scenarios; The higher the grade, the greater the noise suppression, and the overall angle will also be affected. The higher the grade, the greater the angle will be affected. Description of different grades: 1- Suitable for battery-powered occasions; 2- Suitable for occasions with certain high-frequency noise such as USB power supply; 3- Suitable for long-distance USB power supply; 4- Suitable for switching power supply; 5- Suitable for switching power supply and complicated environmental interference, generally not recommended;
Read and write 0x021F Range level Unsigned integer, 16 bits Range level 1 ~ 5 (default is 5), range: 1- about 50cm, 2- about 150cm, 3- about 250cm, 4- about 350cm, 5- about 500cm
Serial number Baud rate Communication duration
1 4800 16ms
2 9600 8ms
3 14400 5.6ms
4 19200 4ms
5 38400 2.4ms
6 57600 1.6ms
7 76800 0.8ms
8 115200 0.6ms

Note: The higher the baud rate, the shorter the single packet communication time.

5、Examples of Modbus communication

Example 1: Reading Processing Value Data

Host: 01 03 01 00 00 01 85 F6

Slave: 01 03 02 02 F2 38 A1

Explain: The address of the sensor is 0x01, and the processing distance value is 0x02F2, which is converted into decimal 754 mm..

Example 2: Reading Real-time Value Data

Host: 01 03 01 01 00 01 D4 36

Slave: 01 03 02 02 EF F8 A8

Explain: The address of the sensor is 0x01, and the real-time distance value is 0x02EF, which is converted into decimal 751mm.

Example 3: Reading Temperature Value Data

Host: 01 03 01 02 00 01 24 36

Slave: 01 03 02 01 2C B8 09

Explain: The sensor address is 0x01, the real-time temperature value is 0x012C, and it is 30.0℃ when converted into decimal.

Example 4: Modify the slave address

Host: 01 06 02 00 05 48 71

Slave: 01 06 02 00 05 48 71

Explain: The sensor address was changed from 0x01 to 0x05.

Example 5: Reading Baud Rate

Host: 01 03 02 01 00 01 D4 72

Slave: 01 03 02 00 03 F8 45

Explain: Read the baud rate, which is 9600bps.

Example 6: Setting Baud Rate

Host: 01 06 02 01 00 03 99 B3

Slave: 01 06 02 01 00 03 99 B3

Explain: Set the baud rate to 9600bps.

Tutorial

Requirements

Connection Diagram

Before burning the code, please switch the transceiver mode switch of the expansion board to AUTO, and the run/compile switch to OFF; after burning the code, switch the run/compile switch to ON, and select the serial port baud rate of 115200.

Connection Diagram

Sample Code

uint8_t Com[8] = {0x01,0x03,0x01,0x01,0x00,0x01,0xd4,0x36};
void setup()
{
  Serial.begin(115200);    
}
void loop()
{
  int Distance =readDistance();
  Serial.print("Distance = ");
  Serial.print(Distance);
  Serial.println(" mm");
  delay(500);
}

int readDistance(void)
{
  uint8_t Data[10] = {0};
  uint8_t ch = 0;
  bool flag = 1;
  int Distance = 0;
  while (flag) {
    delay(100);
    Serial.write(Com, 8);
    delay(10);
    if (readN(&ch, 1) == 1) {
      if (ch == 0x01) {
        Data[0] = ch;
        if (readN(&ch, 1) == 1) {
          if (ch == 0x03) {
            Data[1] = ch;
            if (readN(&ch, 1) == 1) {
              if (ch == 0x02) {
                Data[2] = ch;
                if (readN(&Data[3], 4) == 4) {
                  if (CRC16_2(Data, 5) == (Data[5] * 256 + Data[6])) {
                    Distance = Data[3] * 256 + Data[4];
                    //Serial.println(Distance);
                    flag = 0;
                  }
                }
              }
            }
          }
        }
      }
    }
    Serial.flush();

  }
  return Distance;
}

uint8_t readN(uint8_t *buf, size_t len)
{
  size_t offset = 0, left = len;
  int16_t Tineout = 500;
  uint8_t  *buffer = buf;
  long curr = millis();
  while (left) {
    if (Serial.available()) {
      buffer[offset] = Serial.read();
      offset++;
      left--;
    }
    if (millis() - curr > Tineout) {
      break;
    }
  }
  return offset;
}

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;
}

Expected Results

View the distance value detected by the sensor through the serial port.

Result

FAQ

For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents