← How-Tos
electronics 15 hr ago ◯ 5 min read

RS-485 and Modbus Wiring for Industrial Sensors with ESP32 and Raspberry Pi

rs-485modbusmodbus rtuindustrial sensorsesp32raspberry pitermination resistordifferential signalinguart

Most maker IoT content defaults to I2C or simple UART sensors, but a huge share of real industrial sensors — energy meters, VFDs, flow meters, environmental monitoring stations, PLCs — speak RS-485 with Modbus RTU on top of it, because RS-485's differential signaling tolerates long cable runs and electrically noisy environments that I2C and plain UART simply can't survive. Bringing one of these devices into a Raspberry Pi or ESP32 project means understanding RS-485 wiring and termination, not just wiring up a sensor library. This guide covers the physical layer first, then the Modbus RTU basics needed to actually talk to a device once it's wired correctly.

Why RS-485 Instead of Plain UART

Standard UART (TTL-level serial) is single-ended, referenced to a shared ground, and realistically limited to short cable runs before noise and voltage drop corrupt the signal. RS-485 instead sends data as a voltage difference between two wires (A and B, sometimes labeled D+ and D-), which rejects common-mode noise picked up along the cable run — this is what lets RS-485 reliably run hundreds of meters in an electrically noisy shop or industrial environment where a UART connection would fail within a few meters.

Wiring Basics

Termination Resistors

Each physical end of an RS-485 bus needs a termination resistor — typically 120Ω, matching the characteristic impedance of standard RS-485 twisted-pair cable — to prevent signal reflections off the unterminated end of the cable from corrupting data. A short bus with just two devices sitting right next to each other can sometimes get away without termination; any longer run, or a bus with more than two or three devices, needs it at both ends (and only at the two physical ends — adding termination at every device, or at points in the middle of the bus, degrades the signal rather than helping it). Many RS-485 transceiver modules include a jumper or solder pad specifically for enabling onboard termination, which is more convenient than wiring a separate resistor when the device happens to sit at a bus endpoint.

Connecting to ESP32 or Raspberry Pi

Neither board has native RS-485 hardware — both need an RS-485 transceiver module (commonly built around a MAX485 or similar chip) between the board's UART pins and the actual RS-485 A/B lines. These modules convert the board's single-ended TTL UART signal into RS-485's differential signal and back.

Modbus RTU Basics Once Wiring Is Solid

Modbus RTU is a request/response protocol riding on top of the RS-485 physical layer — one master (your ESP32 or Pi) polls devices by their configured slave address and register addresses, and devices respond with the requested data. Each device on the bus needs a unique slave address (set via DIP switches, a config menu, or a software command on the device itself) and the master needs to know, from the device's documentation, which Modbus register numbers hold which values — there's no universal "read temperature" command, every device's register map is specific to that device and comes from its datasheet or manual. Libraries exist for both ESP32 (ModbusMaster, among others) and Python on Raspberry Pi (pymodbus) that handle the Modbus framing and CRC so you only need to supply the slave address, register address, and data type from the device's documentation.

Troubleshooting Checklist