Site icon Renzo Mischianti

How to interface Arduino, esp8266 or esp32 to RS-485

Spread the love

Hi all, I am doing a project for my inverter centreline, and the first problem to solve is to interface my microcontrollers to the inverter.

The inverter is ABB Aurora and has an interface RS-485. First, I try with Arduino, but I’m going to use an esp8266.

RS-485 work as a broadcast. When you send a message, all devices connected to the wire receive that, so there are a lot of protocols to manage point-to-point communication and broadcast. This article uses normal communication from two devices in a very simple way.
But as you can understand, with a specified protocol, you can control a lot of devices at the same time.

IC or module

Exists IC and module adapt for this type of connection, for Arduino, you can use IC MAX485 or a relative module, for esp8266 and esp32, you must use MAX3485 that works at 3.3v.

MAX485

You can find the IC on AliExpress

You can find the module on AliExpress

The MAX485 is a low-power transceiver for RS-485 communication. Contain one driver and one receiver.

Key Features

MAX485 diagrams

Wiring Arduino UNO

This IC is perfect for working with Arduino or other 5v logic devices, here is the connection schema:

MAX485 Arduino connection schema

The connection is quite simple, you must connect Pin 5 to DE and -RE so you can activate the transmission and reception of the data. In the schema, I use 10 and 11 pins, respectively as RX and TX of SoftwareSerial, and you must connect to RO DI pins of MAX485, then you must attach 5v and GND to the IC pins. The resistor Rt (from A and B) is 120ohm, but I usually use 104ohm.

Arduino UNO and rs458 max485 module connection

When you connect MAX485 to the device, remember that pin A is DI+ and B is DI-.

MAX3485

You can find the IC on AliExpress

You can find the module on AliExpressAliExpress

The MAX3485 is 3.3V, a low-power transceiver for RS-485 communication. It contains one driver and one receiver. The MAX3485 transmits at up to 10Mbps.

Drivers are short-circuited current limited and are protected against excessive power dissipation by thermal shutdown circuitry that places the driver outputs into a high-impedance state. The receiver input has a fail-safe feature that guarantees a logic-high output if both inputs are open circuits.

The MAX3485 is designed for half-duplex communication.

Key Features

MAX3485 diagram

Wiring esp8266

This IC is perfect for working with esp8266, esp32, or other 3.3v logic devices, here is the connection schema:

MAX3485 and esp8266 connection schema

The connection is quite simple, you must connect Pin D0 to DE and -RE so you can activate transmission and reception of the data, in the schema, I use D3 and D2 pins, respectively, as RX and TX of SoftwareSerial and you must connecto to RO DI pins of MAX3485, then you must attach 3.3v and GND to the IC pins. The resistor Rt (from A and B) is 120ohm, but I usually use 104ohm.

esp8266 nodemcu and rs458 max3485 module connection

When you connect MAX3485 to the device, remember that pin A is DI+ and B is DI-.

Wiring esp32

Similar connection for ESP32, but we are going to use Serial1 interface not software serial.

ESP32 DOIT DevKit V1 and rs458 max3485 module connection

And here is the module connection.

ESP32 DOIT DevKit v1 and rs458 max3485 module connection

How to

When you have a datasheet of the device to interconnect, the usage is quite simple, you use the serial communication interface of your microcontroller to send and receive data.

Communication from Arduino to WeMos D1 mini via RS-485 wiring
RS-485 Breadboard communication

And here 2 simple sketch.

Sketch

First Arduino send a message “Hello World!” and wait response.

#include "Arduino.h"

#include <SoftwareSerial.h>
#define RXPin        10  // Serial Receive pin
#define TXPin        11  // Serial Transmit pin

//RS485 control
#define SERIAL_COMMUNICATION_CONTROL_PIN 5 // Transmission set pin
#define RS485_TX_PIN_VALUE HIGH
#define RS485_RX_PIN_VALUE LOW

SoftwareSerial RS485Serial(RXPin, TXPin); // RX, TX

int byteSend;

void setup()  {
  Serial.begin(19200);

  pinMode(SERIAL_COMMUNICATION_CONTROL_PIN, OUTPUT);
  digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX_PIN_VALUE);
  RS485Serial.begin(19200);   // set the data rate

  delay(500);

  digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_TX_PIN_VALUE); // Now trasmit
  Serial.println("Send data!");
  RS485Serial.print("Hello world!"); // Send message
}

void loop() {
	  digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX_PIN_VALUE);  // Disable RS485 Transmit

		if (RS485Serial.available()){
			Serial.println("Response available!");
			Serial.println(RS485Serial.readString());
		}

	  delay(100);
}

Here WeMos D1 mini receive data and send an OK message.

#include "Arduino.h"

#include <SoftwareSerial.h>
#define RXPin        D2  // Serial Receive pin
#define TXPin        D3  // Serial Transmit pin

//RS485 control
#define SERIAL_COMMUNICATION_CONTROL_PIN D0 // Transmission set pin
#define RS485_TX_PIN_VALUE HIGH
#define RS485_RX_PIN_VALUE LOW

SoftwareSerial RS485Serial(RXPin, TXPin); // RX, TX

int byteSend;

void setup()  {
  Serial.begin(19200);

  pinMode(SERIAL_COMMUNICATION_CONTROL_PIN, OUTPUT);
  digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX_PIN_VALUE);
  RS485Serial.begin(19200);   // set the data rate
}
String dataReceived;
bool isDataReceived = false;
void loop() {
	digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_RX_PIN_VALUE);// Init receive

	if (RS485Serial.available()){
		dataReceived = RS485Serial.readString();
		Serial.print("Data received ");
		Serial.println(dataReceived);
		isDataReceived = true;
		delay(10);
	}

	if (isDataReceived){
		delay(111);
		digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, RS485_TX_PIN_VALUE); // Init transmit
		Serial.println("Send response!");
		RS485Serial.print("OK --> ");
		RS485Serial.println(dataReceived);
		isDataReceived = false;
	}

    delay(111);
}

For this sketch I use a SoftwareSerial, but you can use HardwareSerial without particular change, you must only wiring correct pins.

Communication diagram

What happend in communication? Here the diagram.

Communication from Arduino to WeMos D1 mini via RS-485

Video result

Here the video result:

Thanks


Spread the love
Exit mobile version