Skip to content

Commit 93d905f

Browse files
authored
Merge pull request #240 from arduino/karlsoderby/rs232-tutorial
[MKC-464] RS-232 tutorial migration
2 parents fdb9dd3 + 18d7dd3 commit 93d905f

File tree

12 files changed

+147
-0
lines changed

12 files changed

+147
-0
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
title: Arduino & RS-232 Protocol
3+
description: 'Learn how to communicate with a computer using a MAX3323 single channel RS-232 driver/receiver and a software serial connection on the Arduino.'
4+
author: 'Heather Dewey-Hagborg'
5+
tags: [RS-232, Serial]
6+
---
7+
8+
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](http://www.arduino.cc/en/Tutorial/SoftwareSerial).
9+
10+
Materials needed:
11+
12+
* Computer with a terminal program installed (ie. HyperTerminal or RealTerm on the PC, Zterm on Mac)
13+
* Serial-Breadboard cable
14+
* MAX3323 chip (or similar)
15+
* 4 1uf capacitors
16+
* Solderless breadboard
17+
* Hookup wire
18+
* Arduino Microcontroller Module
19+
* Light emitting Diode (LED) - optional, for debugging
20+
21+
## Prepare the Breadboard
22+
23+
![](/rs232/assets/max3323_pins.jpg)
24+
25+
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.
26+
27+
![+5v wires are red, GND wires are black](assets/MAX3323_pwr2.jpg)
28+
29+
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).
30+
31+
![+5v wires are red, GND wires are black](assets/MAX3323_caps2.jpg)
32+
33+
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).
34+
35+
![TX wire Green, RX wire Blue, +5v wires are red, GND wires are black](assets/MAX3323_txrx2.jpg)
36+
37+
## Cables
38+
39+
![(DB9 Serial Connector Pin Diagram)](assets/DB9-pinout.gif)
40+
41+
42+
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.
43+
44+
![](assets/ser_con_top.jpg)
45+
46+
Connect pins 1 and 6 to pin 4 and pin 7 to pin 8. Heatshrink the wire connections to avoid accidental shorts.
47+
48+
![](assets/ser_con_bottom.jpg)
49+
50+
Enclose the connector in a backshell to further protect the signal and enable easy unplugging from your serial port.
51+
52+
![](assets/ser_con_shell.jpg)
53+
54+
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.
55+
56+
![](assets/MAX3323_cable2.jpg)
57+
58+
TX wires Green, RX wires Blue, +5v wires are red, GND wires are black
59+
60+
## Program the Arduino
61+
62+
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:
63+
64+
```arduino
65+
66+
//Created August 23 2006
67+
//Heather Dewey-Hagborg
68+
//http://www.arduino.cc
69+
70+
#include <ctype.h>
71+
72+
#define bit9600Delay 100
73+
#define halfBit9600Delay 50
74+
#define bit4800Delay 188
75+
#define halfBit4800Delay 94
76+
77+
byte rx = 6;
78+
byte tx = 7;
79+
byte SWval;
80+
81+
void setup() {
82+
pinMode(rx,INPUT);
83+
pinMode(tx,OUTPUT);
84+
digitalWrite(tx,HIGH);
85+
delay(2);
86+
digitalWrite(13,HIGH); //turn on debugging LED
87+
SWprint('h'); //debugging hello
88+
SWprint('i');
89+
SWprint(10); //carriage return
90+
}
91+
92+
void SWprint(int data)
93+
{
94+
byte mask;
95+
//startbit
96+
digitalWrite(tx,LOW);
97+
delayMicroseconds(bit9600Delay);
98+
for (mask = 0x01; mask>0; mask <<= 1) {
99+
if (data & mask){ // choose bit
100+
digitalWrite(tx,HIGH); // send 1
101+
}
102+
else{
103+
digitalWrite(tx,LOW); // send 0
104+
}
105+
delayMicroseconds(bit9600Delay);
106+
}
107+
//stop bit
108+
digitalWrite(tx, HIGH);
109+
delayMicroseconds(bit9600Delay);
110+
}
111+
112+
int SWread()
113+
{
114+
byte val = 0;
115+
while (digitalRead(rx));
116+
//wait for start bit
117+
if (digitalRead(rx) == LOW) {
118+
delayMicroseconds(halfBit9600Delay);
119+
for (int offset = 0; offset < 8; offset++) {
120+
delayMicroseconds(bit9600Delay);
121+
val |= digitalRead(rx) << offset;
122+
}
123+
//wait for stop bit + extra
124+
delayMicroseconds(bit9600Delay);
125+
delayMicroseconds(bit9600Delay);
126+
return val;
127+
}
128+
}
129+
130+
void loop()
131+
{
132+
SWval = SWread();
133+
SWprint(toupper(SWval));
134+
}
135+
```
136+
137+
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.
138+
139+
![](assets/hyperterm_hi.jpg)
140+
141+
Now, try typing a lowercase character into the terminal window. You should see the letter you typed return to you in uppercase.
142+
143+
![](assets/hyperterm_abc.jpg)
144+
145+
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.
146+
147+
*code and tutorial by Heather Dewey-Hagborg, photos by Thomas Dexter*

0 commit comments

Comments
 (0)