ESP8266 - RS485

In this tutorial, we'll explore the implementation of RS485 communication with ESP8266. Specifically, we'll cover the following aspects in detail:

The tutorial also provides the instruction for both Hardware Serial and SoftwareSerial.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×TTL to RS485 Module
1×Jumper Wires
1×(Optional) RS485 to USB Cable
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of TTL to RS485 Module

When utilizing serial communication on ESP8266 through functions like Serial.print(), Serial.read(), and Serial.write(), the ESP8266 transmits data via the TX pin and receives data through the RX pin. These pins operate at TTL level, meaning the signals they handle have limited range. Thus, for serial communication over longer distances, it becomes necessary to convert the TTL signal to RS232, RS485, or RS422 signal standards.

In this tutorial, we'll delve into the utilization of RS485 (also known as RS-485) with ESP8266 by employing a TTL to RS485 module. This module facilitates the conversion of TTL signals to RS485 signals and vice versa.

Pinout

The RS485 to TTL module has two interfaces:

  • The TTL interface (connnected to ESP8266) includes 4 pins
    • VCC pin: power pin, needs to be connected to VCC (5V, or 3.3V)
    • GND pin: power pin, needs to be connected to GND (0V)
    • RXD pin: data pin, needs to be connected a TX pin of ESP8266
    • TXD pin: data pin, needs to be connected a RX pin of ESP8266
  • The RS485 interface comprises the following pins:
    • D+ (A or TR+) pin: This pin facilitates data communication.
    • D- (B or TR-) pin: This pin serves for data transmission.
    • GND pin: While optional, it's strongly recommended to include this pin to mitigate noise interference, ensuring optimal performance.
    RS-485 module Pinout
    image source: diyables.io

Wiring Diagram

  • Wiring diagram if using hardware serial
The wiring diagram between ESP8266 NodeMCU and TTL to RS485

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

  • Wiring diagram if using software serial
The wiring diagram between ESP8266 NodeMCU and RS-485 to TTL

This image is created using Fritzing. Click to enlarge image

How To Program ESP8266 to use the RS485 module

To get started with ESP8266 on Arduino IDE, follow these steps:

Serial.begin(9600);
  • If you use SoftwareSerial, you need to include the library and declare a SoftwareSerial object:
#include <SoftwareSerial.h> #define RX_PIN D6 #define TX_PIN D7 // Define the SoftwareSerial objects and their pins SoftwareSerial rs485(RX_PIN, TX_PIN);

ESP8266 Code for Hardware Serial

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-rs485 */ void setup() { // start communication with baud rate 9600 Serial.begin(9600); // wait a moment to allow serial ports to initialize delay(100); } void loop() { // Check if there's data available on Serial if (Serial.available()) { char data = Serial.read(); // read the received character Serial.print(data); // echo back to data to the sender } }

ESP8266 Code for Software Serial

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-rs485 */ #include <SoftwareSerial.h> #define RX_PIN D6 #define TX_PIN D7 // define the SoftwareSerial object and their pins SoftwareSerial rs422(RX_PIN, TX_PIN); void setup() { // start communication with baud rate 9600 rs485.begin(9600); // wait a moment to allow serial ports to initialize delay(100); } void loop() { // Check if there's data available on rs485 if (rs485.available()) { char data = rs485.read(); // read the received character rs485.print(data); // echo back to data to the sender } }

Testing

You can do a test by sending data from your PC to ESP8266 via RS-485 and vice versa. To do it, follow the below steps:

  • Connect ESP8266 to your PC via RS485-to-USB cable as below:
ESP8266 NodeMCU RS485 to PC communication
  • Install a Serial Terminal Program like Tera Term or PuTTY
  • Open the Serial Terminal Program and configure the Serial parameters (COM port, baurate...)
  • Type some data from the Serial Termial to send it to ESP8266.
  • If successful, you will see the echo data on the Serial Terminal.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!