Arduino Nano ESP32 - RS232

In this tutorial, we are going to learn how to use RS232 communication with Arduino Nano ESP32. In detail, we will learn:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×TTL to RS232 Module
1×Jumper Wires
1×Breadboard
1×(Optional) RS232 to USB Cable
1×(Optional) RS232 Gender Changer
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 use the serial communication by using Serial.print(), Serial.read(), Serial.write() ... functions on ESP32, Arduino Nano ESP32 output data to TX pin or read data come from RX pin. The signals on TX and RX pins are TTL level. This signal cannot go far. Therefore, when you want to use the serial communication via long distance, you need to converts the TTL signal to RS232, RS485, or RS422 signal.

The TTL to RS232 module converts TTL signal to RS232 signal, and vice versa.

Pinout

The RS232 to TTL module has two interfaces:

  • The TTL interface (connnected to ESP32) 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 ESP32
    • TXD pin: data pin, needs to be connected a TX pin of Arduino Nano ESP32
  • The RS232 interface: DB9 female D-Sub connector, connect this to the serial device
RS232 Pinout

Wiring Diagram

  • When powering the Arduino Nano ESP32 board via USB port.
The wiring diagram between Arduino Nano ESP32 and TTL to RS232

This image is created using Fritzing. Click to enlarge image

  • When powering the Arduino Nano ESP32 board via Vin pin.
The wiring diagram between Arduino Nano ESP32 and RS232 to TTL

This image is created using Fritzing. Click to enlarge image

How To Program Arduino Nano ESP32 to use the RS232 module

#include <SoftwareSerial.h> SoftwareSerial SoftSerial(D3, D2); // RX: D3, TX: D2
  • Initializes the Serial interface:
SoftSerial.begin(9600);

Arduino Nano ESP32 Code for RS232

/* * 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-rs232 */ #include <SoftwareSerial.h> // define the SoftwareSerial object and their pins SoftwareSerial SoftSerial(D3, D2); // RX: D3, TX: D2 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 conduct a test by transmitting data between your PC and Arduino Nano ESP32 via RS232 in both directions. Follow the steps outlined below:

  • Connect the Arduino Nano ESP32 to your PC using an RS232-to-USB cable, as illustrated in the image below:
Arduino Nano ESP32 RS232 to PC communication
  • Install a Serial Terminal Program such as Tera Term or PuTTY.
  • Open the Serial Terminal Program and configure the Serial parameters (COM port, baud rate, etc.).
  • Enter some data in the Serial Terminal to transmit it to the Arduino Nano ESP32.
  • If the test is successful, you will observe the echoed data on the Serial Terminal.

Video Tutorial

Learn More