Inexpensive RS485 interface board for ESP8266/ESP32/Arduino

RS485 interface providing a noise-resistant connection with the equipment at a distance of up to 1200 meters. The question is how to ensure a secure connection of the microcontroller to the equipment operating on the RS485 interface?

When lightning, electronic equipment connected by a cable can be burned. In addition, RS485 equipment often operates at voltages higher than 3.5 V or 5 V at which the microcontrollers operate, and, accordingly, can produce an overvoltage to the line, which will damage the RS485 chip.

Arduino RS485 Shield and cheap RS485 board don’t provide any protection of the interface converter chip (usually MAX485) from such misfortunes, and, accordingly, are unsuitable for industrial use. In previous articles, I considered some Arduino RS485 Shields. In particular, I reviewed in detail how the work with inexpensive Arduino Shield from Aliexpress.

In this article I will consider inexpensive modules providing an RS485 interface sufficiently protected for use in industrial equipment.

XY-485 и XY-017

I’ll start with a fairly expensive and high-quality module XY-485. Very well assembled, soldered connectors on it, well marked. In general, when you hold in your hands, you feel that the module is assembled in a decent factory.

RS485 converter module – XY-485

I note that in the module, the control of receiving and transmission is automatical. Those, it is not necessary, as in the previously reviewed Arduino 485 Shield, to manually set the RE / DE pins to HIGH / LOW level. In the article, I used the sketch to show how it works. I will use thesame sketch with small changes to work with XY-485.

Cheaper board XY-017, twin brother XY-485. The connectors are not soldered on it, the labeling is worse, but the boards are very similar to each other in terms of components and placement of elements. The layout is slightly different.

Pay attention to the presence of heat recovery fuses and Zener diodes in both versions of the modules that protect the outputs of the MAX485 chip from lightning strikes, overvoltages, and connection errors, when the RS485 equipment does not properly connected to A + and B-.

In addition, two Rx / Tx LEDs indicating data sending / receiving are soldered on the board. It’s conveniently.

Let’s check the work of the Arduino with this module. For the experiments, I will use the RX-485 module, the Arduino Uno and the previously used thermal sensor working via RS485 (Modbus protocol).

Here is the sketch from the previous article where I removed the piece of codes managing RE & DE pins.

#include "ModbusMaster.h"

#define Slave_ID       1 

// instantiate ModbusMaster object
ModbusMaster node;

void setup()
{
  // Modbus communication runs at 9600 baud
  Serial.begin(9600, SERIAL_8N1);

  // Modbus slave ID 1
  node.begin(Slave_ID, Serial);
}

void loop()
{
  // Read 2 registers starting at 0x01
  uint8_t result = node.readInputRegisters(0x01, 2);
  if (getResultMsg(result))
  {
    Serial.println();
    
    double res_dbl = node.getResponseBuffer(0)/10;
    String res = "Temperature: " + String(res_dbl) + " C\r\n";
    res_dbl = node.getResponseBuffer(1)/10;
    res += "Humidity: " + String(res_dbl) + " %";
    Serial.println(res);
 }
 delay(10000);
}

bool getResultMsg(uint8_t result)
{
  String tmpstr2;

  switch (result) {
  case node.ku8MBSuccess:
    return true;
    break;
  case node.ku8MBIllegalFunction:
    tmpstr2 = "Illegal Function";
    break;
  case node.ku8MBIllegalDataAddress:
    tmpstr2 = "Illegal Data Address";
    break;
  case node.ku8MBIllegalDataValue:
    tmpstr2 = "Illegal Data Value";
    break;
  case node.ku8MBSlaveDeviceFailure:
    tmpstr2 = "Slave Device Failure";
    break;
  case node.ku8MBInvalidSlaveID:
    tmpstr2 = "Invalid Slave ID";
    break;
  case node.ku8MBInvalidFunction:
    tmpstr2 = "Invalid Function";
    break;
  case node.ku8MBResponseTimedOut:
    tmpstr2 = "Response Timed Out";
    break;
  case node.ku8MBInvalidCRC:
    tmpstr2 = "Invalid CRC";
    break;
  default:
    tmpstr2 = "Unknown error: " + String(result);
    break;
  }
  Serial.println(tmpstr2);
  return false;
}

The connection schematic Arduino Uno with XY-485 is:

Arduino UnoXY-485
Rx (PIN 0)TXD
Tx (PIN 1)RXD
5V VCC
GNDGND
Термодатчик XY-485
A+A+
B-B-
GNDPower Supply –
VCCPower Supply +5 V

The sketch was successfully uploaded to the Arduino, but there are “Response time-out” errors on the serial port monitors. No shamanism helps: some changes with RS485 library, connecting to the “ground”, power supply and XY-485 sensor from USB port etc. Nothing helped. Then, by chance on youtube I saw a short review of the XY-017 module.

And in the video I heared the decision in one short phrases:

“On XY-017 you need to swap Tx and Rx”.

Honestly, I would never have thought that the XY-485, so good looking board, was produced with so serious a mistake.

I swap Rx and Tx, trying to load a sketch into Arduino Uno and get new error:

avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00
An error occurred while uploading the sketch

I temporary pull out Rx pin to “Upload” the sketch again. After a successful upload I return Rx back and “oh, a miracle”, everything starts working, I receive data from the temperature and humidity sensor. So, the correct connection is unusual 🙂

Arduino UnoXY-485
Rx (PIN 0)RXD
Tx (PIN 1)TXD

Another important point. If all connections with Arduino Uno are correct and the module is normally power supplied, then

On the XY-485 board, the RXD LED should not be constantly on!

If it is constantly lit, most likely the data from the sensor will not be read.

I powered the XY-485 module from both 3.3 V and 5 V directly from the Arduino Uno board powered from the USB port and from a separate power supply unit. In both cases, everything is working fine. Data from the sensor comes.

XY-485 with ESP8266

When you connect XY-485/XY-017 module to ESP8266 developer board, use 3,3 V pin or separate power supply with 3,3V. Otherwise you can burn out GPIO on the ESP8266. Or use TTL logic level converter.

It would seem that when moving RX / TX pins from Arduino Uno to the RX / TX ESP8266 there should be no problems. But it is in theory. 🙁 In practice, the data from the sensor stops coming. There is an “Invalid Slave ID” error, although if there were no connection to the sensor, the error would be “Response Timed Out”.

With the hardware serial port sketch doesn’t work, since RX/TX used for USB converter chip of the developer board. So, I use RX / TX emulation using the ESP SoftwareSerial library from Peter Lerup (@plerup). Since this is a software emulation of a serial port, so better to use lower speeds.

With the software emulation of the serial port, everything works stably, the parameters from the sensor are read successfully. When grounding all circuit elements (Wemos D1, XY-485, RS485 thermal sensor) and powering the XY-485 and thermal sensor from a separate power supply, but not from Wemos D1 connected via USB to a PC, the “Invalid Slave ID” error has not appeared.

#include "ModbusMaster.h"
#include "SoftwareSerial.h" //https://github.com/plerup/espsoftwareserial  

#define Slave_ID    1 
//#define RX_PIN      14  //D5
#define RX_PIN      15  //D8
//#define TX_PIN      12  //D6    
#define TX_PIN      13  //D7
   
SoftwareSerial swSer(RX_PIN, TX_PIN, false, 128);

// instantiate ModbusMaster object
ModbusMaster node;

void setup()
{
  //********** CHANGE PIN FUNCTION  TO TX/RX ********** 
  //GPIO 1 (TX) swap the pin to a TX.
  //GPIO 7 (TXD) swap pint to a TX.  
  //pinMode(7, FUNCTION_4); 
  //GPIO 3 (RX) swap the pin to a RX.
  //GPIO 8 (RX) swap the pin to a RX.
  //pinMode(8, FUNCTION_4); 
  //***************************************************

  // Modbus communication runs at 9600 baud
  Serial.begin(9600, SERIAL_8N1);
  //Serial.setRxBufferSize(128); //Change default 256 to 128
  
  swSer.begin(9600);

  // Modbus slave ID
  //node.begin(Slave_ID, Serial);
  node.begin(Slave_ID, swSer);
}

This code is stable. The loop () and getResultMsg () functions have not changed from the original sketch. I did not clean up the non-working pieces of code that were used in the experiment but just commented out. Maybe will be useful.

Upd. When the sensor is disconnected from the Wemos D1, sometimes the libriary SoftwareSerial crashed an I see a stack in the serial monitor. In this case, even reset button on Wemos D1 doesn’t help. Only turning off the power supply helps.

Spread the love
This entry was posted in IT Recipies and tagged . Bookmark the permalink.

One Response to Inexpensive RS485 interface board for ESP8266/ESP32/Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *