How to interface Arduino with RS232 communication protocol: Example Code Included

RS232 serial communication is a widely used protocol for transmitting data between devices. It allows devices to communicate over long distances using a simple interface. In this tutorial, we will explore how to use RS232 communication protocol with Arduino using MAX232 IC based RS232 to TTL module.  For demonstration purposes, we will create a project that will ON or OFF a LED connected to Arduino from a Computer (Arduino ide serial terminal) by sending ON or OFF command through RS232.

To know more about RS232 serial communication protocol, please read this article:

You may also like reading:

What is the MAX232?

MAX232 is an integrated circuit (IC) used for voltage level conversion in RS232 serial data communication. It allows devices operating at different voltage levels to communicate. It converts lower voltage signals to higher voltage levels required by RS-232 standard. Commonly used in interfaces between microcontrollers and PCs.

MAX232 Pinout

How Does the MAX232 Work?

The MAX232 IC utilizes internal circuitry to convert voltage levels between TTL and RS-232 standards. It incorporates charge pumps that generate the necessary positive and negative voltages required for reliable transmission and reception of RS-232 signals. This conversion process ensures effective communication between devices with different voltage requirements.

Pin Configuration of the MAX232 to TTL Converter Module

MAX232 module pinout

Interfacing Arduino with RS232 communication protocol: The project

Interfacing Arduino with the RS232 protocol enables seamless serial communication between devices operating at different voltage levels. The MAX232 module serves as a voltage level converter, bridging the gap between Arduino’s TTL logic levels and RS-232 levels used by many other devices. By establishing the connection and configuring the pins correctly, Arduino can transmit and receive data with RS-232 devices, expanding the possibilities for various electronic projects.

In this project, we will turn ON-OFF a LED by type some command in Arduino ide serial terminal. When we type “ON” to the serial terminal the LED will turn ON and when type “OFF” to serial terminal, LED will turn OFF. We will use RS232 to TTL converter and DB9 to USB converter for communication between Arduino and PC through RS232 communication protocol.

Interfacing Arduino with RS232 communication protocol

Component List for the project

Component NameQuantityPurchase Link
Arduino Uno1Amazon
MAX232 RS232 to TTL Serial Port Converter1Amazon
USB to DB9 Cable (for communicate with PC)1Amazon
LED1Amazon
220 Ohm Resistor1Amazon

Affiliate Disclosure: When you click on links to make a purchase, this can result in this website earning a commission. Affiliate programs and affiliations include, but are not limited to Amazon.com

Pin Connecctions

The connection between Arduino and MAX232 module is shown below:

Arduino UnoMAX232 Module
Pin 7 (software selected TX pin)TX pin
Pin 6 (software selected RX pin)RX Pin
5V pin of ArduinoVcc
Ground pin of ArduinoGround Pin

Circuit Diagram of This Project

ckt

Arduino code and its explanation

The provided code is an Arduino sketch that demonstrates the use of the MAX232 module for serial communication between an Arduino board and external devices (i.e. PC). Let’s go through the code step by step:

  1. The necessary libraries header files are included: SoftwareSerial.h, string.h, and stdio.h.
  2. Global variables are declared: recv_char to store the received characters, recv_str as a character array to store the received string, and i as a counter for the string array.
  3. The SoftwareSerial object mySerial is created, specifying the RX and TX pins (pin 6 and pin 7) respectively to establish communication with the MAX232 module.
  4. The LED pin (pin 8) is defined as an output pin.

In the `setup()` function:

  1. The baud rate for the Arduino’s hardware serial port is set to 2400 using Serial.begin().
  2. A message is printed to the Arduino’s serial port to indicate that it is ready.
  3. The baud rate for the mySerial SoftwareSerial port is set to 2400 using mySerial.begin().
  4. The LED pin is configured as an output and initially set to a LOW state.

The `loop()` function is the main execution loop of the program:

  1. It checks if there is incoming data available from the MAX232 module using mySerial.available().
  2. If data is available, the received character is read into recv_char.
  3. If the received character is a carriage return (‘\r’), it indicates the end of a command or message.
  4. The characters received before the carriage return are stored in recv_str, which is then null-terminated.
  5. If the received string matches “ON”, the LED pin is set to HIGH, and “high” is printed to the Arduino’s serial monitor.
  6. If the received string matches “OFF”, the LED pin is set to LOW, and “low” is printed to the Arduino’s serial monitor.
  7. The received string is also printed to the Arduino’s serial port.
  8. The variables are reset for the next incoming message by ‘memeset’.
  9. If the received character is not a carriage return, it is added to the recv_str array.

Additionally, the code checks for incoming data from the Arduino’s serial port (`Serial.available()`).

  1. If data is available, the received character is read into recv_char.
  2. If the received character is a carriage return (‘\r’), it indicates the end of a command or message.
  3. The characters received before the carriage return are stored in recv_str, which is then null-terminated.
  4. The received string is sent to the MAX232 module using mySerial.println().
  5. The variables are reset for the next incoming message.

Full code

Arduino Ide .ino file

#include <SoftwareSerial.h>
#include <string.h>
#include <stdio.h>
uint8_t recv_char;
char recv_str[20];
int i=0;

SoftwareSerial mySerial(6, 7); // RX, TX
#define LED 8
void setup()
{
  
  Serial.begin( 2400 );                   //Set the baund rate as 2400 for arduino serial 
  Serial.println("USB Serial is ready");
  mySerial.begin( 2400 );                 // set the baund rate for the SoftwareSerial port
  pinMode(LED, OUTPUT); 
  digitalWrite(LED,LOW);   
}

void loop()
{ 
  if ( mySerial.available() )
  {
    recv_char =  mySerial.read();
    if(recv_char == '\r'){
      recv_str[i] = '\0';
      if(!strcmp(recv_str, "ON")){
        digitalWrite(LED,HIGH);
        Serial.println("high");
      }
      if(!strcmp(recv_str, "OFF")){
        digitalWrite(LED,LOW);
        Serial.println("low");
      }
      Serial.println(recv_str);       // Write the serial from MAX3232 to Arduino serial 
      i=0;
      memset(recv_str, 0, sizeof(recv_str));
    }
    else{
		  if(recv_char == '\r' || recv_char == '\n'){
		} 
      else{
		    recv_str[i++] = recv_char;
		    }
    }
   
  }

  if ( Serial.available() )
  {
   recv_char =  Serial.read();
    if(recv_char == '\r'){
      recv_str[i] = '\0';
      mySerial.println(recv_str);       // Write the serial from MAX3232 to Arduino serial 
      i=0;
      memset(recv_str, 0, sizeof(recv_str));
    }
    else{
		  if(recv_char == '\r' || recv_char == '\n'){
		} 
      else{
		    recv_str[i++] = recv_char;
		    }
    }
     
  }
}

Video Tutorial

Rana Bhuiyan

I hold a B.Sc degree in Electrical & Electronic Engineering from Daffodil International University, Bangladesh. I am an Electronic circuit designer and Microcontroller programmer. I am interested in Robotics, Embedded System Design and IoT.

Recent Posts