ESP32 - RS232

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

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×TTL to RS232 Module
1×Jumper Wires
1×Breadboard
1×(Optional) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to TTL to RS232 Module

When you use the serial communication by using Serial.print(), Serial.read(), Serial.write() ... functions on ESP32, 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 ESP32
    • TXD pin: data pin, needs to be connected a TX pin of ESP32
  • The RS232 interface: DB9 female D-Sub connector, connect this to the serial device
RS232 Pinout

Wiring Diagram

  • Wiring diagram if using hardware serial
ESP32 TTL to RS232 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram if using breadboard
ESP32 RS232 to TTL Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How To Program ESP32 to use the RS232 module

Serial.begin(9600);

ESP32 Code for RS232

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rs232 */ void setup() { // start communication with baud rate 9600 Serial.begin(9600); // Serial Monitor Serial2.begin(9600); // RS232 // wait a moment to allow serial ports to initialize delay(100); } void loop() { // Check if there's data available on Serial if (Serial2.available()) { char data = Serial2.read(); // read the received character Serial.print(data); // print the recived data to Serial Monitor } }

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

Learn More

※ OUR MESSAGES