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:
- How to connect Arduino Nano ESP32 to the TTL to RS232 module
- How to program Arduino Nano ESP32 to read data from the TTL to RS232 module
- How to program Arduino Nano ESP32 to send data to the TTL to RS232 module
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 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
Wiring Diagram
- When powering the Arduino Nano ESP32 board via USB port.
This image is created using Fritzing. Click to enlarge image
- When powering the Arduino Nano ESP32 board via Vin pin.
This image is created using Fritzing. Click to enlarge image
How To Program Arduino Nano ESP32 to use the RS232 module
- If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduini IDE.
- Define the serial pin pins:
- Initializes the Serial interface:
- 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 ESP32 Code for RS232
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:
- 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.