Reference
DFRobot ultrasonic distance sensor with UART and Modbus‑RTU, perfect for Arduino, Raspberry Pi, ESP32, micro:bit and PLC integration.
1. UART Automatic Output Instructions
1.1 Basic UART Communication Parameters
The module operates automatically with a 100ms cycle. The algorithm mode can be set by writing to the 0x228 register of the Modbus protocol. Algorithm modes 0, 4, and 5 output real-time values, while algorithm modes 1, 2, and 3 output processed values. Real-time value output has a response time of 100ms; processed value output provides more stable data with a response time ≥2s. The module operates in algorithm mode 1 by default. When co-channel interference is detected, 0xFFFE data is output as a warning; when no object is detected, 0xFFFD is output. Note: For a detailed explanation of the algorithm modes, please refer to the 0x228 register section of "Modbus Registers".
| UART | Data Bits | Stop Bits | Parity | Baud Rate |
|---|---|---|---|---|
| TTL Level | 8 | 1 | None | 115200bps |
1.2 UART Output Format
| Frame Data | Description | Bytes |
|---|---|---|
| Frame Header | Fixed at 0xFF | 1 byte |
| Data_H | High 8 bits of data | 1 byte |
| Data_L | Low 8 bits of data | 1 byte |
| SUM | Communication checksum | 1 byte |
1.3 UART Output Example
| Frame Header | Data_H | Data_L | SUM |
|---|---|---|---|
| 0XFF | 0X07 | 0XA1 | 0XA7 |
Note: The checksum only retains the lower 8 bits of the accumulated value;
SUM = (Frame Header + Data_H + Data_L) & 0x00FF
= (0XFF + 0X07 + 0XA1) & 0x00FF
= 0XA7;
Distance value = Data_H * 256 + Data_L = 0X07A1; Converted to decimal, this equals 1953; When the parameter value of Modbus register 0x0209 is 0x00, the unit is mm, indicating that the currently measured distance is 1953mm; When the parameter value of Modbus register 0x0209 is 0x01, the unit is us, indicating that the currently measured distance echo time is 1953us. Dividing this value by 5.75 gives the distance value in mm: 1953/5.75 ≈ 340mm.
2. Modbus Protocol Description
2.1 Modbus Protocol Parameters
| Mode | Checksum | Sensor Address | Read Function Code | Write Function Code |
|---|---|---|---|---|
| Modbus-RTU | CRC-16/MODBUS | Configurable, default 0x01 | 0x03 | 0x06 |
2.2 Modbus Protocol Format The user terminal is the master device, and the sensor is the slave device.
Master Send (Read):
| Name | Device Address | Function Code 0x03 | Register Address | Number of Registers | CRC16 Checksum |
|---|---|---|---|---|---|
| Length (Byte) | 1 | 1 | 2 | 2 | 2 |
Slave Response (Read):
| Name | Device Address | Function Code 0x03 | Number of Returned Bytes | Data Area | CRC16 Checksum |
|---|---|---|---|---|---|
| Length (Byte) | 1 | 1 | 1 | N | 2 |
Master Send (Write):
| Name | Device Address | Function Code 0x06 | Register Address | Data Area | CRC16 Checksum |
|---|---|---|---|---|---|
| Length (Byte) | 1 | 1 | 2 | 2 | 2 |
Slave Response (Write):
| Name | Device Address | Function Code 0x06 | Register Address | Data Area | CRC16 Checksum |
|---|---|---|---|---|---|
| Length (Byte) | 1 | 1 | 1 | 2 | 2 |
2.3 Modbus Registers
Register data is formatted with the high byte first, followed by the low byte.
2.3.1 Modbus Register Table 1
| Permissions | Address | Function | Data Type | Description |
|---|---|---|---|---|
| Read-only | 0x0100 | Processed Value | Unsigned Integer, 16-bit | After receiving the command, ranging is initiated. After algorithm processing, the distance value is output. Unit: mm. Response time is approximately 100-230ms (varies depending on the range). |
| Read-only | 0x0101 | Real-time Value | Unsigned Integer, 16-bit | After receiving the command, the sensor initiates ranging once and outputs the real-time distance value. Unit: mm. Response time is approximately 15-40ms (varies depending on the range). |
| Read-only | 0x0102 | Temperature | Signed Integer, 16-bit | Unit: 0.1℃, Resolution: 0.5℃, Response time approximately... 15-50ms (varies depending on the range) |
| Read-only | 0x010A | Echo time | Unsigned integer, 16 bits | After receiving the command, the sensor starts ranging once and outputs the real-time echo time in µs. This value divided by 5.75 gives the distance in mm. The response time is approximately 15-40ms (varies depending on the range) |
Notes:
(1) The response time is obtained from testing with a range of 0.5-3 meters. The shorter the range, the faster the response time.
(2) The 0x0100/0x0101/0x010A registers output 0xFFFE data as a prompt when there is co-frequency interference in the detection environment, and output 0xFFFD when no object is detected.
2.3.2 Modbus Register Table II
| Permissions | 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 Port Baud Rate (default) 115200), unit: bps, takes effect immediately after setting. The baud rates corresponding to the register values are as follows: 0x0002: 4800, 0x0003: 9600 0x0004: 14400, 0x0005: 19200 0x0006: 38400, 0x0007: 57600 0x0008: 76800, 0x0009: 115200 |
| Read/Write | 0x0208 | Signal Level | Unsigned integer, 16 bits | The signal level can be set from 1 to 9 (default is level 5); the higher the level, the larger the detection angle, the more sensitive the sensing, and the longer the measurable range. Level 1: Measurable range approximately 50cm Level 2: Measurable range approximately 80cm Level 3: Measurable range approximately 110cm Level 4: Measurable range approximately 170cm Level 5: Measurable range approximately 200cm Level 6: Measurable range approximately 210cm Level 7: Measurable range approximately 260cm Level 8: Measurable range approximately 280cm Level 9: Measurable range approximately 300cm Note: 1. Measurable range is measured at 25℃, 65% RH, with the object being a 50cm×60cm flat cardboard box, and the transducer needing to be as perpendicular to the object as possible. 2. For the relationship between the detection angles of each level, please refer to "4. Effective Detection Range Reference Diagram" |
| Read/Write | 0x0209 | Output distance value data unit | Unsigned integer, 16 bits | Automatically outputs the distance value in the protocol unit, 0x00-mm, 0x01-us (this value divided by 5.75 gives the distance value in mm) |
| Read/Write | 0x021A | Power Supply Noise Reduction Level | Unsigned Integer, 16 bits | The power supply noise reduction level is divided into 1 to 5 levels (default is 1) to suit different power supply scenarios; the higher the level, the greater the noise suppression, but the overall angle will also be affected, with higher levels having a greater impact on the angle. Different Level Descriptions: 1- Suitable for battery-powered applications; 2- Suitable for USB-powered applications with some high-frequency noise; 3- Suitable for USB-powered applications over longer distances; 4- Suitable for switching power supply applications; 5- Suitable for switching power supply applications with complex environmental interference, generally not recommended; |
| Read/Write | 0x021F | Range | Unsigned Integer, 16-bit | Setting range 300mm~3000mm, default: 2000mm, Note: The maximum measurable range depends on the angle class; the smaller the angle class, the shorter the maximum measurable range. |
| Read/Write | 0x0228 | Algorithm Mode | Unsigned Integer, 16-bit | Five algorithm modes are available: 0: Real-time value 1: Liquid surface sloshing filtration level 1 2: Liquid surface sloshing filtration level 2 3: Liquid surface sloshing filtration level 3 4: Small step filtration mode 5: High sensitivity mode UART output default: Mode 1, Note: 1, 0, 4, and 5 are real-time value outputs 2. After setting to modes 1-3, the distance value becomes the processing value, and the response time is ≥2 seconds; 3. Algorithm mode 5 has the highest sensitivity and largest angle, with a blind zone of approximately 8-11 cm, suitable for scenarios with foamy surfaces |
2.3.3 The Impact of Baud Rate on Single Packet Communication Duration
The higher the baud rate, the shorter the communication time per packet.
| 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 |
2.4 Modbus Communication Examples
Example 1: Reading Processed Value Data
Master: 01 03 01 00 00 01 85 F6
Slave: 01 03 02 02 F2 38 A1
Explanation: Sensor address is 0x01, processing distance value is 0x02F2, which is 754mm in decimal.
Example 2: Reading Real-Time Value Data
Master: 01 03 01 01 00 01 D4 36
Slave: 01 03 02 02 EF F8 A8
Explanation: Sensor address is 0x01, real-time distance value is 0x02EF, which is 751mm in decimal.
Example 3: Reading Temperature Data
Master: 01 03 01 02 00 01 24 36
Slave: 01 03 02 01 2C B8 09
Explanation: The sensor address is 0x01, and the real-time temperature value is 0x012C, which is 30.0℃ in decimal.
Example 4: Modifying the Slave Address
Master: 01 06 02 00 00 05 48 71
Slave: 01 06 02 00 00 05 48 71
Explanation: The sensor address is changed from 0x01 to 0x05.
Example 5: Read Baud Rate
Master: 01 03 02 01 00 01 D4 72
Slave: 01 03 02 00 03 F8 45
Explanation: Read the baud rate. The baud rate read is 9600bps.
Example 6: Set Baud Rate
Master: 01 06 02 01 00 03 99 B3
Slave: 01 06 02 01 00 03 99 B3
Explanation: Set the baud rate to 9600bps.
3. Effective detection range reference diagram
The object being tested is a white cylindrical tube made of PVC, 100cm high and 7.5cm in diameter.


Was this article helpful?
