Arduino & RS-232 Protocol

Learn how to communicate with a computer using a MAX3323 single channel RS-232 driver/receiver and a software serial connection on the Arduino.

In this tutorial you will learn how to communicate with a computer using a MAX3323 single channel RS-232 driver/receiver and a software serial connection on the Arduino. A general purpose software serial tutorial can be found here.

Materials needed:

  • Computer with a terminal program installed (ie. HyperTerminal or RealTerm on the PC, Zterm on Mac)
  • Serial-Breadboard cable
  • MAX3323 chip (or similar)
  • 4 1uf capacitors
  • Solderless breadboard
  • Hookup wire
  • Arduino Microcontroller Module
  • Light emitting Diode (LED) - optional, for debugging

Prepare the Breadboard

max3323 pins

Insert the MAX3323 chip in the breadboard. Connect 5V power and ground from the breadboard to 5V power and ground from the microcontroller. Connect pin 15 on the MAX233 chip to ground and pins 16 and 14 - 11 to 5V. If you are using an LED connect it between pin 13 and ground.

+5v wires are red, GND wires are black
+5v wires are red, GND wires are black

Connect a 1uF capacitor across pins 1 and 3, another across pins 4 and 5, another between pin 2 and ground, and the last between pin 6 and ground. If you are using polarized capacitors make sure the negative pins connect to the negative sides (pins 3 and 5 and ground).

+5v wires are red, GND wires are black
+5v wires are red, GND wires are black

Determine which Arduino pins you want to use for your transmit (TX) and receive (RX) lines. In this tutorial we will be using Arduino pin 6 for receiving and pin 7 for transmitting. Connect your TX pin (7) to MAX3323 pin 10 (T1IN). Connect your RX pin (6) to MAX3323 pin 9 (R1OUT).

TX wire Green, RX wire Blue, +5v wires are red, GND wires are black
TX wire Green, RX wire Blue, +5v wires are red, GND wires are black

Cables

(DB9 Serial Connector Pin Diagram)

If you do not have one already, you need to make a cable to connect from the serial port (or USB-serial adapter) on your computer and the breadboard. To do this, pick up a female DB9 connector from radioshack. Pick three different colors of wire, one for TX, one for RX, and one for ground. Solder your TX wire to pin 2 of the DB9 connector, RX wire to pin 3 and Ground to pin 5.

ser con top

Connect pins 1 and 6 to pin 4 and pin 7 to pin 8. Heatshrink the wire connections to avoid accidental shorts.

ser con bottom

Enclose the connector in a backshell to further protect the signal and enable easy unplugging from your serial port.

ser con shell

Connect the TX line from your computer to pin 8 (R1IN) on the MAX233 and the RX line to pin 7 (T1OUT). Connect the ground line from your computer to ground on the breadboard.

MAX3323 cable2

TX wires Green, RX wires Blue, +5v wires are red, GND wires are black

Program the Arduino

Now we will write the code to enable serial data communication. This program will simply wait for a character to arrive in the serial receiving port and then spit it back out in uppercase out the transmit port. This is a good general purpose serial debugging program and you should be able to extrapolate from this example to cover all your basic serial needs. Upload the following code into the Arduino microcontroller module:

1//Created August 23 2006
2//Heather Dewey-Hagborg
3//http://www.arduino.cc
4
5#include <ctype.h>
6
7#define bit9600Delay 100
8#define halfBit9600Delay 50
9#define bit4800Delay 188
10#define halfBit4800Delay 94
11
12byte rx = 6;
13byte tx = 7;
14byte SWval;
15
16void setup() {
17 pinMode(rx,INPUT);
18 pinMode(tx,OUTPUT);
19 digitalWrite(tx,HIGH);
20 delay(2);
21 digitalWrite(13,HIGH); //turn on debugging LED
22 SWprint('h'); //debugging hello
23 SWprint('i');
24 SWprint(10); //carriage return
25}
26
27void SWprint(int data)
28{
29 byte mask;
30 //startbit
31 digitalWrite(tx,LOW);
32 delayMicroseconds(bit9600Delay);
33 for (mask = 0x01; mask>0; mask <<= 1) {
34 if (data & mask){ // choose bit
35 digitalWrite(tx,HIGH); // send 1
36 }
37 else{
38 digitalWrite(tx,LOW); // send 0
39 }
40 delayMicroseconds(bit9600Delay);
41 }
42 //stop bit
43 digitalWrite(tx, HIGH);
44 delayMicroseconds(bit9600Delay);
45}
46
47int SWread()
48{
49 byte val = 0;
50 while (digitalRead(rx));
51 //wait for start bit
52 if (digitalRead(rx) == LOW) {
53 delayMicroseconds(halfBit9600Delay);
54 for (int offset = 0; offset < 8; offset++) {
55 delayMicroseconds(bit9600Delay);
56 val |= digitalRead(rx) << offset;
57 }
58 //wait for stop bit + extra
59 delayMicroseconds(bit9600Delay);
60 delayMicroseconds(bit9600Delay);
61 return val;
62 }
63}
64
65void loop()
66{
67 SWval = SWread();
68 SWprint(toupper(SWval));
69}

Open up your serial terminal program and set it to 9600 baud, 8 data bits, 1 stop bit, no parity, no hardware flow control. Press the reset button on the arduino board. The word "hi" should appear in the terminal window followed by an advancement to the next line. Here is a shot of what it should look like in Hyperterminal, the free pre-installed windows terminal application.

hyperterm hi

Now, try typing a lowercase character into the terminal window. You should see the letter you typed return to you in uppercase.

hyperterm abc

If this works, congratulations! Your serial connection is working as planned. You can now use your new serial/computer connection to print debugging statements from your code, and to send commands to your microcontroller.

code and tutorial by Heather Dewey-Hagborg, photos by Thomas Dexter

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.