Arduino Nano ESP32 - RS485
This tutorial instructs you how to use RS485 communication with ESP32. In detail, we'll learn the following aspects:
- How to connnect the Arduino Nano ESP32 and the TTL to RS485 module.
- How to program the Arduino Nano ESP32 to retrieve data from the TTL to RS485 module.
- How to program the Arduino Nano ESP32 to transmit data to the TTL to RS485 module.
- How to send data from your PC to Arduino Nano ESP32 via RS485, and vice versa.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
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.
Wiring Diagram
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:
- Initializes the Serial interface:
- To read data come from RS485, you can use the following functions:
- To write data to RS485, you can use the following functions:
- And more functions to use with RS485 in Serial reference
Arduino Nano ESP32 Code
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:
- 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.