Arduino Nano ESP32 - RS485

This tutorial instructs you how to use RS485 communication with ESP32. In detail, we'll learn the following aspects:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×TTL to RS485 Module
1×Jumper Wires
1×(Optional) RS485 to USB Cable
1×(Recommended) Screw Terminal Adapter for Arduino Nano

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 employing serial communication on the Arduino Nano ESP32 using functions such as Serial.print(), Serial.read(), and Serial.write(), data transmission takes place via the TX pin, while data reception occurs through the RX pin. These pins operate at TTL level, handling signals with a limited range. Therefore, for serial communication over extended distances, it becomes necessary to convert the TTL signal to standards such as RS232, RS485, or RS422.

In this tutorial, we'll delve into the utilization of RS485 (also known as RS-485) with the Arduino Nano ESP32 by employing a TTL to RS485 module. This module facilitates the conversion of TTL signals to RS485 signals and vice versa, enabling reliable communication over extended distances.

Pinout

The RS485 to TTL module features two interfaces:

  • TTL Interface (connected to Arduino Nano ESP32):
    • VCC Pin: This power pin should be connected to VCC (5V or 3.3V).
    • GND Pin: This power pin should be connected to GND (0V).
    • RXD Pin: This data pin should be connected to a TX pin of the Arduino Nano ESP32.
    • TXD Pin: This data pin should be connected to an RX pin of the Arduino Nano ESP32.
  • RS485 Interface:
    • D+ (A or TR+) Pin: This pin facilitates data communication.
    • D- (B or TR-) Pin: This pin is utilized for data transmission.
    • GND Pin: While optional, including this pin is strongly recommended to mitigate noise interference, ensuring optimal performance.
    RS-485 module Pinout
    image source: diyables.io

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and TTL to RS485

This image is created using Fritzing. Click to enlarge image

How To Program Arduino Nano ESP32 to use the RS485 module

  • Define the serial pin pins:
#define RX1PIN D2 #define TX1PIN D3
  • Initializes the Serial interface:
Serial1.begin(9600, SERIAL_8N1, RX1PIN, TX1PIN); // RS232

Arduino Nano ESP32 Code

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-rs485 */ #define RX1PIN D2 #define TX1PIN D3 void setup() { // start communication with baud rate 9600 Serial.begin(9600); // Serial Monitor Serial1.begin(9600, SERIAL_8N1, RX1PIN, TX1PIN); // RS485 // wait a moment to allow serial ports to initialize delay(100); } void loop() { // Check if there's data available on Serial if (Serial1.available()) { char data = Serial1.read(); // read the received character Serial1.print(data); // echo back to data to the sender Serial.print(data); // print the recived data to Serial Monitor } }

Testing

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

  • Connect Arduino Nano ESP32 to your PC via RS485-to-USB cable as below:
Arduino Nano ESP32 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 ESP32.
  • 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!