How-to · Advanced · 3 min read
Modbus RTU With Arduino and ESP32
RS485 Wiring, Addresses and Code
By Geekobot Learning Team · Updated 29 Oct 2026
On this page
Modbus RTU is a simple master-slave protocol over RS485. An Arduino or ESP32 acts as the master and reads registers from industrial sensors by address. You need a TTL to RS485 module, a twisted pair for A and B, a common ground and a 120 Ω terminator at each end. Match the sensor's baud rate, parity and slave address.
Key specs at a glance
- Physical layer
- RS485 half-duplex, two-wire A and B with a ground reference
- Typical serial setting
- 9600 baud, 8 data bits, no parity, 1 stop (8N1), but check the sensor
- Addresses
- 1 to 247 for slaves
- Common functions
- 03 read holding registers, 04 read input registers, 06 write a register
- Cable length
- Up to about 1200 m at low baud
- Termination
- 120 Ω across A and B at each end of the bus
How Modbus RTU works
One master (your microcontroller) sends a request to a slave address. The slave replies with data from numbered registers. Each frame ends with a CRC check. Only one device speaks at a time because RS485 is half-duplex. Industrial soil, temperature, dust, weather and energy sensors commonly use Modbus; the datasheet lists the register numbers and scaling.
Wiring to an ESP32
Use a TTL to RS485 module (MAX485 or similar) or an auto-direction board.
| Module pin | Connect to |
|---|---|
| RO (receiver output) | ESP32 RX2 (GPIO16) |
| DI (driver input) | ESP32 TX2 (GPIO17) |
| DE and RE tied together | ESP32 GPIO4 (direction) |
| VCC | 3.3 V or 5 V as the module requires |
| A / B | Sensor A / B, twisted pair |
| GND | Sensor GND |
- RO (receiver output)
- ESP32 RX2 (GPIO16)
- DI (driver input)
- ESP32 TX2 (GPIO17)
- DE and RE tied together
- ESP32 GPIO4 (direction)
- VCC
- 3.3 V or 5 V as the module requires
- A / B
- Sensor A / B, twisted pair
- GND
- Sensor GND
Reading a register with the ModbusMaster library
This ESP32 sketch reads two holding registers from slave 1 at 9600 baud. Use your sensor's register map for the address and scaling.
#include <ModbusMaster.h>
const int DE_RE = 4;
ModbusMaster node;
void preTx() { digitalWrite(DE_RE, HIGH); }
void postTx() { digitalWrite(DE_RE, LOW); }
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, 16, 17);
pinMode(DE_RE, OUTPUT); digitalWrite(DE_RE, LOW);
node.begin(1, Serial2); // slave address 1
node.preTransmission(preTx);
node.postTransmission(postTx);
}
void loop() {
uint8_t r = node.readHoldingRegisters(0x0000, 2); // start register, count
if (r == node.ku8MBSuccess) {
Serial.print(node.getResponseBuffer(0));
Serial.print(" ");
Serial.println(node.getResponseBuffer(1));
} else {
Serial.print("Modbus error "); Serial.println(r, HEX);
}
delay(1000);
}Troubleshooting
wrong address, baud rate or parity. Check the sensor label and manual.
A and B swapped, missing common ground or no termination on a long cable.
add a 120 Ω resistor at each end and keep the cable away from power lines.
each slave on the bus needs a different address.
to check the sensor before blaming your code.
Try it: Uart baud rate error calculator
Change the values and the diagram and results update as you type.
Common mistakes to avoid
- Leaving DE and RE unconnected so the module never transmits.
- Using the wrong parity or baud rate.
- Skipping termination on a long bus.
- Giving two slaves the same address.
- Assuming register numbers start at 1 when the library starts at 0.
Frequently asked questions
What is the difference between RS485 and Modbus?
RS485 is the electrical standard. Modbus is the message protocol that runs over it.
Can I use RS232 instead?
Modbus RTU also runs over RS232 for one device on a short cable. RS485 is better for several devices and long runs.
Which Arduino library should I use?
ModbusMaster for the classic master, or ArduinoModbus on boards that support it.
What does 8N1 mean?
8 data bits, no parity, 1 stop bit.
Do I need the 120 ohm resistors?
For short cables at low baud it often works without, but a long bus or high baud needs termination at both ends.
Parts for this guide
Calculators
Terms in this guide
Projects that use it
Put this into practice: 7 step-by-step builds in our project library use parts from this guide.
Keep learning
Sources
- Modbus Organization: Modbus application protocol and serial line specifications
- ModbusMaster library documentation
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.




