Example Code for Arduino-RTU Slave Address
The article guides users to communicate with BMV080 via the UART interface of ESP32-E, modify the Modbus-RTU slave address of the sensor, configure baud rate, parity check and stop bits, read back registers to verify communication parameters and print results through serial port.
Hardware Preparation
- SEN0662 Gravity: BMV080 PM2.5 Sensor × 1
- DFR0654 FireBeetle 2 ESP32-E Development Board × 1
- Several Dupont Wires
Software Preparation
- Download and install Arduino IDE: Download Link
- Download DFRobot_BMV080_Gravity library: DFRobot_BMV080_Gravity Library
- Download and install the DFRobot_RTU library: DFRobot_RTU Library
- Library Installation Guide: View Installation Tutorial
Wiring Diagram

Connection Description:
| Sensor Pin: VCC | Connect to | Main Controller Pin: 3.3V |
|---|---|---|
| Sensor Pin: GND | Connect to | Main Controller Pin: GND |
| Sensor Pin: R | Connect to | Main Controller Pin: 26/TX |
| Sensor Pin: T | Connect to | Main Controller Pin: 25/RX |
- Sensor DIP switch configuration: Set the communication mode to UART side (the factory default UART address is 0x57).
Sample Code
#include "DFRobot_BMV080_Gravity.h"
// Target UART address after modification, valid range: 0x01 ~ 0xF7
const uint8_t NEW_UART_ADDR = 0x11;
// Current UART Modbus address of module before modification
const uint8_t CURRENT_UART_ADDR = 0x57;
DFRobot_BMV080_Gravity_UART sensor(&Serial1, 9600, CURRENT_UART_ADDR, /*rx =*/25, /*tx =*/26);
const char *parityToText(uint8_t parity)
{
switch (parity) {
case DFRobot_BMV080_Gravity::eParityNone:
return "No parity";
case DFRobot_BMV080_Gravity::eParityEven:
return "Even parity";
case DFRobot_BMV080_Gravity::eParityOdd:
return "Odd parity";
default:
return "Unknown";
}
}
const char *stopBitToText(uint8_t stopBit)
{
switch (stopBit) {
case DFRobot_BMV080_Gravity::eStopBit1:
return "1 stop bit";
case DFRobot_BMV080_Gravity::eStopBit1_5:
return "1.5 stop bits";
case DFRobot_BMV080_Gravity::eStopBit2:
return "2 stop bits";
default:
return "Unknown";
}
}
void setup()
{
delay(2000);
uint16_t fmtReg = 0;
uint8_t parity = 0;
uint8_t stop = 0;
Serial.begin(115200);
while (!Serial) {
delay(100);
}
// UART sensor initialization, loop until initialization succeeds
while (!sensor.begin()) {
Serial.println("Sensor init failed, please check UART wiring and current Modbus address!");
delay(1000);
}
Serial.println("BMV080 Gravity sensor init succeeded");
/**
* Set UART Modbus‑RTU slave address (holding register 0x0000)
* Valid address range: 0x01 ~ 0xF7, 0x00 is broadcast address and prohibited
*/
if (sensor.setUartAddress(NEW_UART_ADDR) == 0) {
Serial.print("UART slave address saved as 0x");
Serial.println(NEW_UART_ADDR, HEX);
} else {
Serial.println("Set UART slave address failed");
}
/**
* Set UART baud rate, keep 9600 unchanged
* Available baud rate: e2400, e4800, e9600, e14400, e19200, e38400, e57600, e115200
*/
if (sensor.setBaud(DFRobot_BMV080_Gravity::e9600) == 0) {
Serial.println("Baud rate register saved as 9600");
} else {
Serial.println("Set baud rate failed");
}
/**
* Set UART parity and stop‑bit (holding register 0x0002)
* Parity: eParityNone / eParityEven / eParityOdd
* StopBit: eStopBit1 / eStopBit1_5 / eStopBit2
*/
if (sensor.setUartFormat(DFRobot_BMV080_Gravity::eParityNone, DFRobot_BMV080_Gravity::eStopBit1) == 0) {
Serial.println("UART format saved (8‑N‑1)");
} else {
Serial.println("Set UART format failed");
}
// Read back registers for parameter verification
Serial.print("Read UART slave address: 0x");
Serial.println(sensor.getUartAddress(), HEX);
Serial.print("Read baud rate: ");
Serial.print(sensor.getBaud());
Serial.println(" bps");
fmtReg = sensor.getUartFormat();
parity = (uint8_t)((fmtReg >> 8) & 0xFF);
stop = (uint8_t)(fmtReg & 0xFF);
Serial.print("UART format register value: 0x");
Serial.println(fmtReg, HEX);
Serial.print("Parity config: ");
Serial.print(parity);
Serial.print(" (");
Serial.print(parityToText(parity));
Serial.println(")");
Serial.print("Stop bit config: ");
Serial.print(stop);
Serial.print(" (");
Serial.print(stopBitToText(stop));
Serial.println(")");
Serial.println();
Serial.println("Notice: Re‑power the module under UART mode for new UART settings to take effect!");
Serial.println("Use new address 0x11 for subsequent UART communication.");
}
void loop()
{
}
Result

Was this article helpful?
