Arduino - RS232

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

The tutorial also provides the instruction for both Hardware Serial and SoftwareSerial.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×TTL to RS232 Module
1×Jumper Wires
1×(Optional) RS232 to USB Cable
1×(Optional) RS232 Gender Changer
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About TTL to RS232 Module

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

Wiring Diagram

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

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram if using software serial
Arduino RS232 to TTL Wiring Diagram

This image is created using Fritzing. Click to enlarge image

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

Arduino Code for Hardware Serial

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-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 } }

Arduino Code for Software Serial

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rs232 */ #include <SoftwareSerial.h> // define the SoftwareSerial object and their pins SoftwareSerial SoftSerial(7, 6); // RX: 7, TX: 6 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 Arduino via RS232 and vice versa. To do it, follow the below steps:

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

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES