The Raspberry Pi’s General-Purpose Input/Output (GPIO) connector consists of 26 pins that are a hacker’s delight, allowing for some serious analogue and digital tomfoolery. When drooling over the specs, one thing that surprised me was the inclusion of a basic serial port. This was a lower-powered version of our old 20th century friend, RS232. For those of you who have no idea what I’m talking about, don’t panic, although this post may not be for you. RS232 would seem to many to be redundant old-school tech but in reality it’s still all around you as it is still probably the simplest way you can get two computers chatting digitally.
As a bonus, the bright folks of Pi have configured their stock Raspbian OS to use the serial port as a console. That is, you can use it to log in and get a command line prompt. My day job involves a number of servers at a data centre. Despite being the latest and greatest kit, each one has an RS232 port configured like this. In the event of serious problem where not even the graphics card is working correctly, you may still be able to get into the system using this interface.
The serial interface provided on the Pi is about as simple as you can make it. Only four lines are used; power, transmit, receive and ground. Couldn’t be easier, just wire this to to a COM port on a PC and get talking. Right?
Err, no. There’s a problem: The Pi runs at a meagre 3.3V. RS232 requires +12V for the high (or ‘1’) signal and -12V for low (or ‘0’). My friend the Pi can’t do that out of the box, but a bit of Googling revealed some enterprising types who had constructed a solution. Luckily, there’s a readily-available chip that can convert the signals in both directions, allowing us to create a ‘real’ RS232 interface from the Pi’s low-voltage version. This gizmo costs about £5, is readily available and has the catchy title of the MAX3232CPE. All it needs is a few capacitors to help it along at you’re good to go.
Time to get to work. I got everything from my local Maplin (links and order codes provided).
- 1 x MAX3232CPE Integrated Circuit (DT26D)
- A 16-pin DIL IC socket for the above (BL19V) (not required if breadboarding)
- 5 x 0.1uF Capacitors (I used simple ceramic discs in the final circuit) (N43CJ)
- 9-Pin D-type socket (RK61R)
- Wires with connectors for the GPIO port (A39GF)
- Some veroboard (FL17T) (although I would strongly recommend using a breadboard (AG09K) first to prototype)
- Some soldering experience (unless breadboarding)
- A COM/Serial Port
I had a problem with the last item there. As a Mac user, I knew that Steve Jobs had banished the RS232 port to the parts bin many years ago in favour of the over-complicated-but-a-bit-more-useful USB interface. Fortunately, Maplin sell a USB to serial adaptor based on the PL2303 chipset that works great on recent OS X flavours, including Mountain Lion – although I had to dig around for some drivers and instructions. (PL2303 drivers for Mac)
To Work. Here’s what we’re going to create:
First of all I built this up on a breadboard, following Joonas Pihlajamaa’s instructions (link at foot of page). If you’re new to mounting ICs on a breadboard, note that you’ll need to carefully bend the slightly splayed pins in to fit. Don’t use the socket at this point. Next up, I soldered up the 9-pin connector. From the reverse/solder-side I connected RXD to Pin 2, TXD to Pin 3 and Ground to Pin 5 (top-right).
Now with your Pi switched OFF, connect the four lines for the Pi from the circuit to pins 1, 6, 8 and 10. Pin 1 is the top-left pin marked ‘P1’ on the board. The pin on it’s right is Pin 2, then the pin below P1 is Pin 3, on it’s right Pin 4 and so on.
The four lines you are connecting are:
- Pin 1 – +3.3V
- Pin 6 – Ground
- Pin 8 – TXD (Transmit data)
- Pin 10 – RXD (Receive data)
With the 9-pin RS232 connector hooked up to your COM port, switch your Pi on.
Your Pi should start up normally and seem blissfully unaware of it’s new connection. Now turn your attention to your computer.
You need some software to establish a connection to your Pi. On Windows, hunt down PuTTY, a tool built just for this purpose. On Mac, try out MacWise (£) or, and this applies to Linux as well, go with screen, the command-line utility.
If you’re going with screen, see if it is installed:
$ screen -h
Not found? Then…
$ apt-get install screen
$ screen /dev/ttyUSB0 115200
Or if you’re on a Mac and have MacPorts installed:
$ port install screen
$ screen /dev/tty.usbserial 115200
NB: The /dev/ device many vary depending on OS and drivers etc. Have a look around your /dev/ directory if the above doesn’t work. You’re generally trying to locate something mentioning ‘tty’ and ‘usb’.
The screen should go blank and a tap on the enter key will show a login prompt to the Pi. Congratulations.
If you’re using a more advanced package, note that RS232 requires configuration before it will work. The Pi’s settings are 115,200bps (bits-per-second), 8 data-bits, no parity bit and 1 stop-bit. This is typically written as 115200 8N1 and you should match your settings to this. Once configured, open or connect (depending on how your software terms it) and press the Enter key. All being well, a Raspberry Pi login prompt should greet you.
Not working? Nine out of ten times this is down to the RXD and TXD lines being the wrong way around on the PC side. After disconnecting and switching your Pi off, try swapping the lines around on the breadboard and giving it another go. If that doesn’t do the trick, re-check your settings in your terminal emulator.
I decided to bring this to it’s logical conclusion (in my mind) by hooking up the Pi to a perfect screen and keyboard combination; the Psion 5. However, it didn’t work at first as both the Psion and the Pi expect to be the ‘client’ talking to the computer. I needed to add a crossover cable, which crosses over RXD and TXD, to get it working. But work it did!
Once happy with the breadboard, it was easy enough to transfer the circuit over to veroboard and create something a little more permanent.
You may be wondering what the point of all this is, but the fact is that many, many devices out there still use RS232 for communication. You can find RS232 ports on projectors, set-top boxes, even cars. Heck, my old washing machine had one (a Dyson Contra-rotator). So, having a fully-working RS232 interface on something as versatile as the Pi might be fun. A bit of Googling will also reveal how you can change the operation of the port from a console login to, well, whatever you want. What am I going to use it for? Well, that’s another post.
Sources for this article: