ESP8266 - RS232

In this tutorial, we'll delve into using RS232 communication with ESP8266, covering:

The tutorial includes guidance for both Hardware Serial and SoftwareSerial.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×TTL to RS232 Module
1×Jumper Wires
1×(Optional) RS232 to USB Cable
1×(Optional) RS232 Gender Changer
1×(Optional) ESP8266 Screw Terminal Adapter

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 working with serial stuff on ESP8266 and you're throwing around Serial.print(), Serial.read(), and Serial.write(), what's happening is your ESP8266 is sending data out through its TX pin and catching data coming in on its RX pin. Now, here's the deal - those signals on TX and RX are at TTL level, which is cool and all, but they don't travel too far. So, if you're planning on exchanging data over long distances, you've got to upgrade that signal.

Enter the TTL to RS232 module. This nifty gadget turns your TTL signal into RS232 and back again. It's like magic for making your communication reach far and wide.

Pinout

The RS232 to TTL module has two interfaces:

  • The TTL interface (connnected to ESP8266) 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 ESP8266
    • TXD pin: data pin, needs to be connected a TX pin of ESP8266
  • 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 ESP8266 NodeMCU and TTL to RS232

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

  • Wiring diagram if using software serial
The wiring diagram between ESP8266 NodeMCU and RS232 to TTL

This image is created using Fritzing. Click to enlarge image

How To Program ESP8266 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

ESP8266 Code for Hardware Serial

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

ESP8266 Code for Software Serial

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-rs232 */ #include <SoftwareSerial.h> // define the SoftwareSerial object and their pins SoftwareSerial SoftSerial(D5, D6); // RX: D5, TX: D6 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 ESP8266 via RS232 and vice versa. To do it, follow the below steps:

  • Connect ESP8266 to your PC via RS232-to-USB cable as below:
ESP8266 NodeMCU 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 ESP8266.
  • If successful, you will see the echo data on the Serial Terminal.

Video Tutorial

Learn More