RS-485 and Modbus Wiring for Industrial Sensors with ESP32 and Raspberry Pi
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
- Twisted pair for A/B: RS-485's noise rejection depends on A and B being a tightly twisted pair, not two separate wires routed loosely — use actual twisted-pair cable (shielded twisted pair for noisy environments), not a flat ribbon or two arbitrary conductors from a multi-core cable.
- Common ground reference: RS-485 is differential, but devices on the bus still need a common ground reference between them for the receivers to interpret the voltage difference correctly — most RS-485 devices provide a ground terminal alongside A/B specifically for this; don't skip it just because the data signal itself is differential.
- Daisy-chain topology, not a star: RS-485 is designed for a linear bus with devices connected along its length, with the two physical ends of the bus terminated (see below). Star-wiring multiple devices from one central point, rather than a continuous bus, degrades signal integrity and is a common source of unreliable communication on multi-device networks.
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.
- Half-duplex direction control: Most low-cost RS-485 modules are half-duplex and have a DE/RE (driver enable/receiver enable) pin that must be toggled by software to switch between transmitting and receiving — forgetting to manage this pin correctly in code is the most common reason a wired-up RS-485 link transmits but never receives, or vice versa.
- ESP32: use a hardware UART (not the USB-serial UART used for flashing/debugging) connected to the transceiver module's TX/RX, with a free GPIO driving the DE/RE pin.
- Raspberry Pi: the Pi's primary UART (GPIO 14/15) works the same way, though on Pi models where that UART is also used for Bluetooth, the device tree needs the UART freed up for general use first (most Pi OS images document this as "disabling the Bluetooth UART" in config.txt), or a USB-to-RS485 adapter sidesteps the Pi's onboard UART entirely.
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
- No response at all → check A/B polarity isn't swapped (reversing A and B between two otherwise-correct devices is a common wiring mistake), and verify a common ground exists.
- Garbled or inconsistent data → check termination at both bus ends, and confirm baud rate/parity settings match on every device on the bus — Modbus RTU devices don't auto-negotiate these.
- Works with one device, breaks when adding a second → check for star wiring instead of a proper daisy-chain bus, and confirm slave addresses are actually unique.