Arduino Nano - RS232

In this tutorial, we'll delve into RS232 communication with ESP8266. Key points include:

The tutorial covers both Hardware Serial and SoftwareSerial, providing a comprehensive guide to ESP8266 and RS232 interaction.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×TTL to RS232 Module
1×Jumper Wires
1×(Optional) RS232 to USB Cable
1×(Optional) RS232 Gender Changer
1×(Optional) 9V Power Adapter for Arduino Nano
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 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
RS232 Pinout

Wiring Diagram

  • Wiring diagram if using hardware serial
The wiring diagram between Arduino Nano and TTL to RS232

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram if using software serial
The wiring diagram between Arduino Nano and RS232 to TTL

This image is created using Fritzing. Click to enlarge image

How To Program Arduino Nano to use the RS232 module

  • Initializes the Serial interface:
Serial.begin(9600);
  • If you use SoftwareSerial, you need to include the library and declare a SoftwareSerial object:
#include <SoftwareSerial.h> // Define the SoftwareSerial objects and their pins SoftwareSerial SoftSerial(7, 6); // RX: 7, TX: 6

Arduino Nano Code for Hardware Serial

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-rs232 */ 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 } }

Arduino Nano Code for Software Serial

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

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:
Arduino Nano RS232 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 Arduino Nano.
  • If successful, you will see the echo data on the Serial Terminal.

Video Tutorial

Learn More