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 Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

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

#define RX1PIN D3 #define TX1PIN D2
  • Initializes the Serial interface:
Serial1.begin(9600, SERIAL_8N1, RX1PIN, TX1PIN); // RS232

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 */ #define RX1PIN D3 #define TX1PIN D2 void setup() { // start communication with baud rate 9600 Serial.begin(9600); // Serial Monitor Serial1.begin(9600, SERIAL_8N1, RX1PIN, TX1PIN); // RS232 // wait a moment to allow serial ports to initialize delay(100); } void loop() { // Check if there's data available on Serial1 if (Serial1.available()) { char data = Serial1.read(); // read the received character Serial1.print(data); // echo back to data to the sender Serial.print(data); // print the recived data to Serial Monitor } }

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

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!