Member-only story
Integrating an RS485 Sensor with ESP32
4 min readMay 3, 2024
In this guide, we will explore how to connect an RS485 sensor, the XY-MD02, to an ESP32 microcontroller using an MAX485 module. We’ll go through the necessary components, wiring setup, and the code required to get temperature readings from the sensor.
What You Need
- XY-MD02 Sensor (Amazon): This is an RS485 Modbus RTU protocol sensor, commonly used for measuring temperature and humidity.
- ESP32 Microcontroller (Amazon)
- MAX485 Module (Amazon): This module is used to convert TTL signal levels to RS485.
- 12V Power Supply 10A (Amazon): To power the XY-MD02 Sensor.
Wiring
Please follow the following wiring diagram,
Code
Here is the complete code I used.
#include <ModbusMaster.h>
#define MAX485_DE 32
#define MAX485_RE_NEG 33
#define RXD2 16
#define TXD2 17
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void…