Arduino Nano - RS232
In this tutorial, we'll delve into RS232 communication with ESP8266. Key points include:
- Connecting ESP8266 to the TTL to RS232 module for improved communication.
- Programming ESP8266 to read data received from the TTL to RS232 module.
- Programming ESP8266 to send data to the TTL to RS232 module.
The tutorial covers both Hardware Serial and SoftwareSerial, providing a comprehensive guide to ESP8266 and RS232 interaction.
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 RS232 Module
When you're doing serial stuff with ESP8266 using Serial.print(), Serial.read(), Serial.write(), etc., the ESP8266 sends data through the TX pin and receives data on the RX pin. Now, those signals on TX and RX are TTL level, which doesn't travel very far. So, if you're looking to do serial things over a long distance, you've got to change that TTL signal into RS232, RS485, or RS422.
Enter the TTL to RS232 module. It takes the TTL signal and turns it into RS232, and it can do the reverse too. Easy-peasy.
Pinout
The RS232 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)
- GND pin: power pin, needs to be connected to GND (0V)
- RXD pin: data pin, needs to be connected a RX pin of Arduino Nano
- TXD pin: data pin, needs to be connected a TX pin of Arduino Nano
- The RS232 interface: DB9 female D-Sub connector, connect this to the serial device
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 RS232 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 RS232, you can use the following functions:
- To write data to RS232, you can use the following functions:
- And more functions to use with RS232 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 RS232 and vice versa. To do it, follow the below steps:
- Connect Arduino Nano to your PC via RS232-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.