Arduino Nano - RS485
In this guide, we'll delve into the implementation of RS485 communication with the Arduino Nano. We'll focus on the following key aspects:
- Establishing the connection between the Arduino Nano and the TTL to RS485 module.
- Programming the Arduino Nano to receive data from the TTL to RS485 module.
- Programming the Arduino Nano to transmit data to the TTL to RS485 module.
- Demonstrating how to send data between your PC and the Arduino Nano using RS485 bidirectionally.
The tutorial also provides the instruction for both Hardware Serial and SoftwareSerial.
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 utilizing serial communication on the Arduino Nano with functions like Serial.print(), Serial.read(), and Serial.write(), data transmission occurs via the TX pin, while data reception takes place through the RX pin. These pins operate at TTL level, handling signals with limited range. Therefore, for serial communication over longer distances, it becomes necessary to convert the TTL signal to standards such as RS232, RS485, or RS422.
In this tutorial, we'll explore the integration of RS485 (also referred to as RS-485) with the Arduino Nano by utilizing a TTL to RS485 module. This module facilitates the conversion of TTL signals to RS485 signals and vice versa, enabling robust communication over long distances.
Pinout
The RS485 to TTL module has two interfaces:
- The TTL interface (connnected to Arduino Nano) 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 Arduino Nano
- TXD pin: data pin, needs to be connected a RX pin of Arduino Nano
- 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.
Wiring Diagram
- Wiring diagram if using hardware serial
This image is created using Fritzing. Click to enlarge image
- Wiring diagram if using software serial
This image is created using Fritzing. Click to enlarge image
How To Program Arduino Nano to use the RS485 module
- Initializes the Serial interface:
- If you use SoftwareSerial, you need to include the library and declare a SoftwareSerial object:
- 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 Code for Hardware Serial
Arduino Nano Code for Software Serial
Testing
You can do a test by sending data from your PC to Arduino Nano via RS-485 and vice versa. To do it, follow the below steps:
- Connect Arduino Nano 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 Arduino Nano.
- If successful, you will see the echo data on the Serial Terminal.