June 23, 2012

Raspberry Pi Serial Console in Linux

My Raspberry Pi arrived in the mail earlier this week. However, work, family, and other commitments meant that tinkering with it had to wait until the weekend. Until today, all that I managed to accomplish was to download a Debian-based OS image, flash it to an SD card, hook the board up to my TV, and squeal with joy as it booted.

Now I'm ready to dig into this diminutive computer and see what's going on inside, but I have a slight problem. The Pi has only HDMI and composite for video output and it turns out that I have nothing in my office which can display video from either of these. (You can supposedly buy an HDMI to DVI adapter, but I don't yet have a DVI-capable monitor either.) This basically leaves me with two choices: Log into the Pi via SSH over ethernet or connect a serial console.

SSH would work just fine in theory, but there are two major drawbacks:

  1. You don't get any feedback on the boot process. If the operating system doesn't come up far enough to configure the ethernet port, give it an IP address, and then run the SSH daemon, I'll have to go and hook it up to something else to debug it. Which is clearly obnoxious.
  2. The Debian image that I installed doesn't run an SSH daemon by default.

So that leaves us with the serial console. The Pi has two rows of headers for general-purpose IO (GPIO). Three of these pins double as a serial port: ground (GND), receive (RX), and transmit (TX). The protocol is the same one spoken by standard 9-pin RS-232 ports on PCs, but you cannot connect them directly to a PC's serial port because the voltage levels are different. You'll basically fry your shiny new uber-cheap Linux board. And it wouldn't even make a very serviceable doorstop.

You need a special cable with a bit of circuitry in it to do the level conversion. I happen to have one that I bought off eBay for a couple bucks to do wifi router hacking. It's just a cell phone data cable which I spliced a CD-ROM audio cable connector onto. Finding the right cable can be tricky since these aren't as common anymore. Just make sure you get one that converts to 3.3V, not 5V. The OpenWRT wiki has some suggestions. If you don't want to hunt around, just buy this one from Adafruit. Either way, you'll probably have to do some wire-splicing since there is no standard connector or pin arrangement for this.

This image shows the pinout for the Pi's GPIO header. The serial cable is connected to pins 6 (GND), 8 (TX), and 10 (RX). Remember that you have to connect the cable's TX wire to the Pi's RX pin, and the cable's RX wire to the Pi's TX pin.

Once the hardware is sorted, the rest is easy. Just plug the cable into your computer. Run the dmesg command to see how your system recognized the level converter. In my case, these were the relevant messages:

[125827.544373] usb 1-6.1: new full-speed USB device number 9 using ehci_hcd
[125827.663087] usbcore: registered new interface driver usbserial
[125827.663120] USB Serial support registered for generic
[125827.663202] usbcore: registered new interface driver usbserial_generic
[125827.663208] usbserial: USB Serial Driver core
[125827.665650] USB Serial support registered for pl2303
[125827.665717] pl2303 1-6.1:1.0: pl2303 converter detected
[125827.667643] usb 1-6.1: pl2303 converter now attached to ttyUSB0
[125827.667684] usbcore: registered new interface driver pl2303
[125827.667689] pl2303: Prolific PL2303 USB to serial adaptor driver

This is showing that the kernel recognized the device, set up the pl2303 driver, and then attached it to the character device /dev/ttyUSB0. In most cases, this will be the device you'll see too.

I previously used minicom to talk to serial ports, but recently found out that GNU screen works just as well. Just run this command as root, power up the Raspberry Pi, and away you go:

screen /dev/ttyUSB0 115200

(Depending on how your Linux distribution and account are set up, you may be able to use sudo or add your account to the dialout group.)

If all goes well, you'll see a flurry of kernel messages scroll by, followed by a login prompt. Success!

F.A.Q.

Q: Holy Moses, this doesn't work at all!

A: Probably the trickiest part in all of this is connecting the serial cable to the right pins on the Raspberry Pi. The good news is, you aren't likely to blow anything up by connecting them the wrong way. With only three pins, there aren't that many different combinations, so just try them all. The ground wire is probably going to be easiest to find, so try to connect that one first and then you have only two combinations to try.

Another strategy (one I have not tried) might be to plug the Raspberry Pi's power cable into your PC's USB port or a powered hub. (The docs warn against this but if you don't have any peripherals connected to the Pi and aren't running any CPU-intensive programs, you should be fine.) This connects the Pi's signal/power ground to the computer's signal ground so you then only have to worry about the RX and TX pins. Once you've figured them out, go back to powering your Pi with a cell phone or USB charger.

If you have any other questions or notes, please feel free to leave a comment.

Postscript: The Blogger WYSIWYG HTML editor was programmed by jerks and I totally regret using it to write this post.

14 comments:

Anonymous said...

Here is a site for cables.
Rs232 to TTL lvls. Probably what you were looking for. I found this handy myself not so long ago

http://www.ftdichip.com/Products/Cables/USBTTLSerial.htm

charles said...

@Anonymous:

Yep, that looks like a good source. I think one of those cables is the same thing that Adafruit sells. (And prices are similar.)

Anonymous said...

Thanks for posting this ! One question remains to me though:

Wouldn't it be easier to grab one of those USB to RS232 adapters (essentially a USB COM port) instead of using the GPIO connector plus a voltage converter ?

Those thingies cost below 6€.

I really wonder why they didn't use the standard symmetrical voltage for the RS232 board. Those RS232 Max ICs costed only a few cents back then and easily generated +/- 15V from a 5V power supply.

- Holger

cirrus minor said...

I power my pi from my TV's usb port with no problems , whilst hooked up to same 22" 1080p tv.if it powers a 2.5 usb hdd it will power a pi. good luck.

zoobab said...

Just order those cheap Nokia clones at Dealextreme:

http://www.zoobab.com/nokia-ca42-3-3v

Anonymous said...

AFAIK the raspberry draws around 700 mA max current. Be careful. This is above 500 mA USB port spec max and might easily fry your USB port.

Unknown said...

I tried with a usb to rs232 cable from radio shack that I already had. I took a cdrom audio cable, pulled the pins from inside the connector and put them on the 3 pins on the rs232...got it to output to the screen, but it was a bunch of funny characters. Not sure where to start troubleshooting to fix it.

charles said...

J Gol: You can't (or at least certainly shouldn't) use a USB to RS232 directly with the Pi. RS232 voltage levels are too high (5-12V typical). The Pi needs something that speaks the serial protocol at a much lower voltage (3.3V). You can possibly find a cable or adapter that converts the RS232 signals down to 3.3V, but it's probably cheaper and easier to buy a USB phone data cable like the one zoobab linked to and cut off the phone connector.

Jimbo said...

I got this on ebay:

http://www.ebay.com/itm/190685792376

It's labeled with the different voltages right at the pins. Just got my Pi, trying serial tty in Raspbian next.

charles said...

@Jimbo: That looks like a great USB to TTL serial adapter for the price. I haven't heard of the CP2102 chip. Let me know how well it works.

Jimi James said...

@charles, Jimbo here, Yes the cp2102 adapter is working, logging in with screen. I thought I had a bad component, but I think I was just not waiting long enough to get the tty. I detached the screen session and a minute later I re-attached and there was the login prompt. I also ordered a similar, pl2303 based USB to TTL serial adapter but it hasn't arrived yet. Anyhow, woo hoo, just getting started.

charles said...

@Jimbo: awesome, glad it worked out. Good luck!

Anonymous said...

If you buy the cable from Adafruit, you might as well get their Raspberry Pi specific cable (much cheaper and easier to connect!):

https://www.adafruit.com/products/954

Chuan said...

"Remember that you have to connect the cable's TX wire to the Pi's RX pin, and the cable's RX wire to the Pi's TX pin."

THANK YOU!!! Such a noob mistake, but I was banging my head against the wall for 3 hours before seeing your post and realizing how dumb I was.

BTW I'm using a CP2012 USB adapter (couple bucks on eBay) and it's working great.