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

Spread the love
  •  
  •  
  •  
  • 4
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    4
    Shares

Hi all, I do a project for my inverter centraline and first problem to solve is to interface my microcontrollers to the inverter.

Inverter is ABB Aurora and have 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 sent a message all device connected to the wire receive that, so there are a lot of protocol to manage point to point communication and broadcast. In this article we use a normal communication from two device in very simple way.
But as you can understand with a specified protocol you can control a lot of device 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 relative module, for esp8266 and esp32 you must use MAX3485 that work at 3.3v.

MAX485

You can find the IC here  

You can find the module here  

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

Key Features

  • Low Power Consumption Minimizes Thermal Dissipation, Reducing System Cost
    • 120µA to 500µA Quiescent Current
    • Shutdown Current of 0.1µA
    • Single 5V Supply Voltage
  • Integrated Protection Enhances System Robustness
    • Short Circuit Current Limited Driver
    • Integrated Thermal Shutdown
    • Receiver Fail-Safe for Input Open Circuit
    • Guarantees Logic High
MAX485 diagrams

This IC is perfect to working with Arduino or other 5v logic device, here 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 transmission and reception of the data, in the schema I use 10 and 11 pins respectly as RX and TX of SoftwareSerial and you must connecto to RO DI pins of MAX485, than you must attach 5v and GND to the IC pins.

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

MAX3485

You can find the IC here  

You can find the module here  

The MAX3485 are 3.3V, low-power transceiver for RS-485 communication. It contain one driver and one receiver. The MAX3485 transmit at up to 10Mbps.

Drivers are short-circuit 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 circuit.

The MAX3485, is designed for half-duplex communication.

Key Features

  • Operate from a Single 3.3V Supply-No Charge Pump!
  • Interoperable with +5V Logic
  • 8ns Max Skew
  • 2nA Low-Current Shutdown Mode
  • -7V to +12V Common-Mode Input Voltage Range
  • Allows up to 32 Transceivers on the Bus
  • Full-Duplex and Half-Duplex Versions Available
  • Industry Standard 75176 Pinout
  • Current-Limiting and Thermal Shutdown for Driver Overload Protection
MAX3485 diagram

This IC is perfect to working with esp8266, esp32 or other 3.3v logic device, here 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 respectly as RX and TX of SoftwareSerial and you must connecto to RO DI pins of MAX3485, than you must attach 3.3v and GND to the IC pins.

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

Usage

When you have 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.

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.

What happend in communication? Here the diagram.

Communication from Arduino to WeMos D1 mini via RS-485

Here the video result:

Thanks


Spread the love
  •  
  •  
  •  
  • 4
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    4
    Shares

You may also like...

Leave a Reply

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