How to use Modbus RTU with ESP32 to read Sensor Data
Modbus RTU Communication protocol using ESP32 & RS485 for reading sensor register data with C++ Code with CRC check, inquiry & response frame
- Suitable forB.Tech/Diploma ESP32/IoT students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
Overview
In this tutorial, we’ll dive into the Modbus RTU protocol and learn how to implement it with an ESP32 to read sensor data from a slave device using RS485. We’ll focus on using a humidity and temperature sensor that operates under the Modbus RTU protocol to simplify and enhance our understanding. The ESP32 will be programmed to retrieve sensor data using RS-485, providing a hands-on approach to understanding this communication method.
Modbus is a communication protocol initially developed in 1979 for use with programmable logic controllers (PLCs). Over the years, it has evolved into a standard protocol widely adopted in the industry for connecting a variety of industrial electronic devices.
Modbus RTU (Remote Terminal Unit) is a specific variant of the Modbus protocol that employs binary coding and serial communication for fast data transmission. It is particularly prevalent in industrial settings where it links sensors, instruments, and actuaries to controllers and computers, primarily through RS-485 serial interfaces. This makes it ideal for applications requiring robust and reliable data exchange over longer distances or in electrically noisy environments.
Components Required
To learn, how to implement Modbus RTU protocol with ESP32, we need the following components.
S.N.
Components Name
Quantity
Purchase Link
ESP32 Board
Amazon
AliExpress
MAX485 Modbus Module
Amazon
AliExpress
RS485 Temperature Humidity Sensor
AliExpress
Connecting Wires
Amazon
AliExpress
Breadboard
Amazon
AliExpress
What is Modbus?
Modbus is a serial communication protocol developed by Modicon in 1979, initially designed for Modicon programmable logic controllers (PLCs) for industrial applications. Over time, it has become a standard protocol used widely across various automation products to connect industrial electronic devices. Modbus is particularly useful for monitoring and communicating between intelligent devices, sensors, or instruments, and for managing field devices using computers and Human Machine Interfaces (HMIs). It is also well-suited for Remote Terminal Unit (RTU) applications that require wireless communication, making it ideal for gas and oil substation applications due to its openness, simplicity, low-cost development, and minimal hardware requirements.
In a typical Modbus network, there is one master device and up to 247 slave devices, each with a unique address. The master device orchestrates the data management and writes data to the slaves. Data transfer in Modbus is in the form of binary bits—zeroes and ones—which represent positive and negative voltages respectively. These bits are transmitted rapidly between devices due to their small size.
There are three main variations of the Modbus protocol:
Modbus ASCII
Modbus RTU
Modbus TCP/IP
These variations accommodate different types of networks and data transmission requirements.
What is Modbus RTU?
Modbus RTU (Remote Terminal Unit) is a widely utilized serial communication protocol in building management and industrial automation systems.
It facilitates the connection and communication between various devices, such as controllers, sensors, and actuators, often in environments where the devices may be spread over distances up to 15 meters. This protocol operates on a point-to-point basis, featuring a single master device that communicates with one or more slave devices in a network.
Technically, Modbus RTU employs binary data encoding for efficient data transmission and includes CRC (Cyclic Redundancy Check) error checking to ensure the integrity of data during transmission. This robust error-checking mechanism helps prevent data corruption across the physical network.
Modbus RTU is strictly confined to its specific communication standards, meaning devices configured for different modes (e.g., ASCII versus RTU) cannot interact. For instance, a device set up for ASCII communication will not be able to interpret data from a device sending data in RTU binary format.
The physical layer of Modbus RTU typically uses one of three types of electrical interfaces:
RS-232:
Suitable for short-distance communications and often used for connections between a device and a PC.
RS-485:
The most common interface for Modbus RTU, allowing for longer distances and the connection of multiple devices on a single bus network.
RS-422:
Similar to RS-485 but with greater resistance to electrical noise, suitable for industrial environments.
Additionally, the technical implementation of Modbus RTU includes defining slave IDs, function codes, and the structure of data packets, which consist of addresses, data values, and checksums to validate the communication. This meticulous organization enables precise control and monitoring of networked devices in complex systems.
MAX485 Module for RS-485 communication
The
MAX485 IC
is a low-power
transceiver
designed for RS-485 communication. It is widely used in
industrial
and commercial applications for robust,
long-distance
data transmission.
There are a few types of MAX485 modules available in the market. One of them is a basic MAX485 circuit board with input and output pins for direct interfacing. In addition to the TX and RX signals, you also need to control the RE and DE pins.
Another type is one that comes along with a CD4069 hex-inverter IC. The CD4069 on the module automatically drives the DE and RE pins of the MAX485 IC when you transmit or receive messages. This simplifies the operation and programming.
This IC supports
half-duplex
communication, allowing data to be transmitted and received over a single pair of wires, though not simultaneously. It includes
driver enable
and
receiver enable
pins, which can be controlled to switch between transmitting and receiving modes. It is a
half-duplex driver
with a
Unit Load (UL)
rating of 1 and therefore you can have up to
32 MAX485s
on a single RS-485 bus.
You can learn more about RS-485 communication using the following tutorials:
RS-485 Simplex Communication
RS-485 Half Duplex Communication
RS-485 Full Duplex Communication
RS485 Temperature and Humidity Sensor
This is one of the temperature humidity sensors that work on the Standard Modbus RTU protocol.
We will be using this sensor in this guide. We will interface the temperature humidity sensor with Arduino & using the RS-485 we can use standard Modbus RTU to read the sensor data.
Product Specifications
Power Supply:
5-30V DC
Maximum Power Consumption:
≤ 5W
Humidity Range:
0-100% RH
Temperature Range:
-40° to 80° C
Accuracy:
Temperature ± 0.3 °C; Humidity ± 0.3% RH
Communication Protocol:
Standard Modbus RTU
Communication Mode:
485 communication
Baud Rate:
9600 (default), 1200, 2400, 4800, 9600, 19200 (can be set by software)
Default Address of Equipment:
Default 1 (1~254 can be modified by software)
Register:
Humidity address 0, temperature address 1 (software modifiable)
Wiring Mode:
Red+, black GND, Yellow A+, Green B-
Communication Protocol Details
Basic Communication Parameters
Parameter
Specification
Coding
8 bit binary
Data Bits
8bit
Parity Bit
None
Stop Bit
1bit
Error Checking
CRC (redundant cyclic code)
Baud Rate
1200bit/s, 2400bit/s, 4800bit/s, 9600 bit/s, 14400 bit/s, 19200 bit/s (Factory default is 9600bit/s)
Data Frame Format Definition
Component
Specification
Initial Structure
≥ 4 bytes of time
Address Code
1 byte
Function Code
1 byte
Data Area
N bytes
Error Check
16-bit CRC code
Time to End Structure
≥ 4 bytes
Address code:
the address of the transmitter, which is unique in the communication network (factory default 0x01).
Function code:
the function instruction of the command sent by the host, this transmitter only uses the function code 0x03 (read register data).
Data area:
The data area is the specific communication data, pay attention to the high byte of the 16 bits data first!
CRC code:
two-byte check code.
Host Query Frame Structure:
Component
Address Code
Functio
In this tutorial, we’ll dive into the Modbus RTU protocol and learn how to implement it with an ESP32 to read sensor data from a slave device using RS485. We’ll focus on using a humidity and temperature sensor that operates under the Modbus RTU protocol to simplify and enhance our understanding. The ESP32 will be programmed to retrieve sensor data using RS-485, providing a hands-on approach to understanding this communication method.
Modbus is a communication protocol initially developed in 1979 for use with programmable logic controllers (PLCs). Over the years, it has evolved into a standard protocol widely adopted in the industry for connecting a variety of industrial electronic devices.
Modbus RTU (Remote Terminal Unit) is a specific variant of the Modbus protocol that employs binary coding and serial communication for fast data transmission. It is particularly prevalent in industrial settings where it links sensors, instruments, and actuaries to controllers and computers, primarily through RS-485 serial interfaces. This makes it ideal for applications requiring robust and reliable data exchange over longer distances or in electrically noisy environments.
Components Required
To learn, how to implement Modbus RTU protocol with ESP32, we need the following components.
S.N.
Components Name
Quantity
Purchase Link
ESP32 Board
Amazon
AliExpress
MAX485 Modbus Module
Amazon
AliExpress
RS485 Temperature Humidity Sensor
AliExpress
Connecting Wires
Amazon
AliExpress
Breadboard
Amazon
AliExpress
What is Modbus?
Modbus is a serial communication protocol developed by Modicon in 1979, initially designed for Modicon programmable logic controllers (PLCs) for industrial applications. Over time, it has become a standard protocol used widely across various automation products to connect industrial electronic devices. Modbus is particularly useful for monitoring and communicating between intelligent devices, sensors, or instruments, and for managing field devices using computers and Human Machine Interfaces (HMIs). It is also well-suited for Remote Terminal Unit (RTU) applications that require wireless communication, making it ideal for gas and oil substation applications due to its openness, simplicity, low-cost development, and minimal hardware requirements.
In a typical Modbus network, there is one master device and up to 247 slave devices, each with a unique address. The master device orchestrates the data management and writes data to the slaves. Data transfer in Modbus is in the form of binary bits—zeroes and ones—which represent positive and negative voltages respectively. These bits are transmitted rapidly between devices due to their small size.
There are three main variations of the Modbus protocol:
Modbus ASCII
Modbus RTU
Modbus TCP/IP
These variations accommodate different types of networks and data transmission requirements.
What is Modbus RTU?
Modbus RTU (Remote Terminal Unit) is a widely utilized serial communication protocol in building management and industrial automation systems.
It facilitates the connection and communication between various devices, such as controllers, sensors, and actuators, often in environments where the devices may be spread over distances up to 15 meters. This protocol operates on a point-to-point basis, featuring a single master device that communicates with one or more slave devices in a network.
Technically, Modbus RTU employs binary data encoding for efficient data transmission and includes CRC (Cyclic Redundancy Check) error checking to ensure the integrity of data during transmission. This robust error-checking mechanism helps prevent data corruption across the physical network.
Modbus RTU is strictly confined to its specific communication standards, meaning devices configured for different modes (e.g., ASCII versus RTU) cannot interact. For instance, a device set up for ASCII communication will not be able to interpret data from a device sending data in RTU binary format.
The physical layer of Modbus RTU typically uses one of three types of electrical interfaces:
RS-232:
Suitable for short-distance communications and often used for connections between a device and a PC.
RS-485:
The most common interface for Modbus RTU, allowing for longer distances and the connection of multiple devices on a single bus network.
RS-422:
Similar to RS-485 but with greater resistance to electrical noise, suitable for industrial environments.
Additionally, the technical implementation of Modbus RTU includes defining slave IDs, function codes, and the structure of data packets, which consist of addresses, data values, and checksums to validate the communication. This meticulous organization enables precise control and monitoring of networked devices in complex systems.
MAX485 Module for RS-485 communication
The
MAX485 IC
is a low-power
transceiver
designed for RS-485 communication. It is widely used in
industrial
and commercial applications for robust,
long-distance
data transmission.
There are a few types of MAX485 modules available in the market. One of them is a basic MAX485 circuit board with input and output pins for direct interfacing. In addition to the TX and RX signals, you also need to control the RE and DE pins.
Another type is one that comes along with a CD4069 hex-inverter IC. The CD4069 on the module automatically drives the DE and RE pins of the MAX485 IC when you transmit or receive messages. This simplifies the operation and programming.
This IC supports
half-duplex
communication, allowing data to be transmitted and received over a single pair of wires, though not simultaneously. It includes
driver enable
and
receiver enable
pins, which can be controlled to switch between transmitting and receiving modes. It is a
half-duplex driver
with a
Unit Load (UL)
rating of 1 and therefore you can have up to
32 MAX485s
on a single RS-485 bus.
You can learn more about RS-485 communication using the following tutorials:
RS-485 Simplex Communication
RS-485 Half Duplex Communication
RS-485 Full Duplex Communication
RS485 Temperature and Humidity Sensor
This is one of the temperature humidity sensors that work on the Standard Modbus RTU protocol.
We will be using this sensor in this guide. We will interface the temperature humidity sensor with Arduino & using the RS-485 we can use standard Modbus RTU to read the sensor data.
Product Specifications
Power Supply:
5-30V DC
Maximum Power Consumption:
≤ 5W
Humidity Range:
0-100% RH
Temperature Range:
-40° to 80° C
Accuracy:
Temperature ± 0.3 °C; Humidity ± 0.3% RH
Communication Protocol:
Standard Modbus RTU
Communication Mode:
485 communication
Baud Rate:
9600 (default), 1200, 2400, 4800, 9600, 19200 (can be set by software)
Default Address of Equipment:
Default 1 (1~254 can be modified by software)
Register:
Humidity address 0, temperature address 1 (software modifiable)
Wiring Mode:
Red+, black GND, Yellow A+, Green B-
Communication Protocol Details
Basic Communication Parameters
Parameter
Specification
Coding
8 bit binary
Data Bits
8bit
Parity Bit
None
Stop Bit
1bit
Error Checking
CRC (redundant cyclic code)
Baud Rate
1200bit/s, 2400bit/s, 4800bit/s, 9600 bit/s, 14400 bit/s, 19200 bit/s (Factory default is 9600bit/s)
Data Frame Format Definition
Component
Specification
Initial Structure
≥ 4 bytes of time
Address Code
1 byte
Function Code
1 byte
Data Area
N bytes
Error Check
16-bit CRC code
Time to End Structure
≥ 4 bytes
Address code:
the address of the transmitter, which is unique in the communication network (factory default 0x01).
Function code:
the function instruction of the command sent by the host, this transmitter only uses the function code 0x03 (read register data).
Data area:
The data area is the specific communication data, pay attention to the high byte of the 16 bits data first!
CRC code:
two-byte check code.
Host Query Frame Structure:
Component
Address Code
Functio
Technologies used
Hardware components
- ESP32
- Arduino
Software & tools
- Arduino IDE
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.