One of the reasons I was excited to play with the Pi was the rank of pins on one of its corners, the ‘low-level peripheral‘ pin-out. GPIO pins, SPI, I2C and even an onboard 3.3v serial connection (UART)! In fact, in the stock debian image, it is configured to output the bootup messages and the kernel errors to this port!
But it’s not just for that. Oh no. You can play with it too!
The serial device is at ‘/dev/ttyAMA0’ and by default, outputs kernel messages at 115200 baud. This is configured in /boot/cmdline.txt and you can remove the option, or alter the connection speed it outputs at. If you were going to use this port for your own purposes, I would recommend you disable the kernel logging for obvious reasons!
In /boot/cmdline.txt: (for example)
dwc_otg.lpm_enable=0 rpitestmode=1 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 rootwait
The “console” and “kgdboc” options that include “ttyAMA0” are the ones that are important here.
Connecting to the GPIO pins
Look at the pin-out (the board should be turned so you can read the text under the logo. Then top-left, etc should match the pinout below)
You see the pins marked 5v and the 3v3? You should never let them connect or you might do something nasty to your Pi! And the one below the 5v? That is marked as ‘Do Not Connect’ so you shouldn’t really connect anything to that either.
This isn’t really an interface that you should be poking wires at by hand, or even using crocodile clips. You need a connector that can clip onto this and lets you pick and choose your connections easily. There are some breakout boards already, such as the Slice Of Pi, or the Gertboard.
I used a floppy disc drive connector. It has the same pitch as the pins and you’ll find that you can slip most of the connector onto it nicely. There is a bit of overhang, but don’t worry about that!
I used a multimeter to make sure that the wires I was stripping and preparing at one end of the ribbon cable, were the ones I wanted to connect to. In this case the 5v, the 0v (Ground) and the two yellow pins, Tx and Rx.
If you have got this far, then this is it! If you have a serial port connector, like an USB FTDI cable, you can connect that to these and start to communicate!
See http://www.irrational.net/2012/04/19/using-the-raspberry-pis-serial-port/ for an excellent write-up on how to use this connection to interface with your Pi.
Let’s make it physical!
A few years back, I followed in the footsteps of others and hacked about with a receipt printer – a ‘microprinter‘ as they are sometimes referred to. In fact, I even got to the point of being able to push image data to it via an adapted serial connection, the details of which you can find here.
I pulled the bag with all the printer bits and pieces off the shelf and begin reconstructing what I had done, rebuilding the circuit that allowed this printer’s TTL serial to chat with the normal RS232 serial using a MAX232N chip. I was saved by my own post, as I couldn’t remember how to do it to save my life!
The printer also uses a much slower baud rate (9600) so I lowered that in the cmdline.txt and rebooted with everything connected.
The Result?
And once I got my old python libraries for chatting to the printer on the Pi:
UPDATE
Serial logging speed
Even though the printer was capturing the boot at the correct serial speed, kernel logging was still being emitted at 115200baud. It turns out that the serial connection for the logger is a virtual tty, and that to get the correct speed out for kernel panics, you have to set the tty’s speed to 9600 as well. This is from ‘Grey’ via popcornmix(Dom) on an unrelated github issue:
From Gray (not directly in response to you, but this question has been asked before):
An awful lot of what is printed during the boot sequence is output by the kernel during initialization – i.e. during the set-up of devices that are later used to support the operating system implemented on the root filie system.
One of the classes of devices that need to be set-up are terminal (tty) devices – so it kind-of follows that the thing being output to during this kernel initialization process isn’t really a tty device. The kernel calls it (well them actually) a ‘console’. The kernel command line allows you to identify and set the baud rate for these consoles and kernel output goes to them all (e.g. to the HDMI framebuffer console and to the UART console).
Each console normally ends up being presented as a separate tty device in /dev.
Once the operating system gets hold of the devices the kernel has left it, it configures them and uses them as it sees fit. In our case we do the standard thing of running a shell on just about any tty we can find. This is implemented in the file that controls what we do when control is first passed to the operating system – /etc/inittab.
In /etc/inittab each tty is read by a program ‘getty’ in its own process. This explains why, once you get to a log-on prompt, [1] the baud rate might change; and [2] the output is no longer the same as it is on other console/ttys. (You may have noticed that you can log on separately to a shell over the UART and a different one over the HDMI/keyboard.)
So, in short, edit /etc/inittab and change
/sbin/getty -L ttyAMA0 115200 vt100
to
/sbin/getty -L ttyAMA0 9600 vt100
if you want the operating system to run at 9600 baud.
Slice Of Pi board
I also picked up a Slice of Pi PCB and headers from Ciseco, as my hacky floppy drive connector was not up to scratch for prototyping. It’s meant to be a breakout board and allow for easy addition of an XBee module but I am pretty sure all I will use it for is as a breakout board. Once soldered together, it sat quite nicely over the Pi, but I couldn’t help feel that a supporting leg on one side would be helpful, to avoid stressing the Pi’s GPIO header when plugging in wires to the breakout board. The current supply (5v,3v3, 0v) is collected to one side, but the kit doesn’t include anything to solder in there for ease of use. I cut down a stackable header to size and soldered that in as you will see from the picture, but it would’ve been nice if this was part of the kit too.
Clayton Smith
April 24, 2012
Great hack! I love the receipt full of kernel output! 🙂
Steve Anderson (@IrregularShed)
April 24, 2012
Excellent work – this is precisely what I plan on doing once my Pi (finally) arrives…
Michael Cook
April 28, 2012
Cool, I’d love it if you emailed me how you get on with making something like this. I want to try something like this but worry it might be a bit beyond my abilities. I wonder if it could be done with a usb receipt printer?
benosteen
May 31, 2012
If you can push bytes to the USB receipt printer from a linux environment, then you will be able to from the Pi. A cheap way to make a printer work over USB, would be to stick a USB->serial chip in there and get it to show up as a USB serial device. If that is what the manufacturer has done, then you might be in with a chance 🙂
Terrence Andrew Davis
April 24, 2012
Depressing
benosteen
April 24, 2012
Ouch, not sure how to take that!
Alex
April 24, 2012
He’s just depressed that you beat him to it. Awesome work by the way!
Fredrik Lindroth (@atarian88)
April 24, 2012
Nicely done! It’s great to have these inspirational posts for when people are getting their boards.
Brian Geniesse
April 24, 2012
indeed, the possibilities might just be endless
Alex Gibson
May 28, 2012
Hi there, great write-up that is accessible to linux n00bs like me. I wonder if you can help me at all further please?
I want to attach a serial mouse systems mouse to my Pi.
I have a RS232 breakout board, and have swapped a MAX3232CPE chip in to make sure it’s 3V3 friendly.
I followed all of the above to help me detach the console from ttyAMA0
I learned about apt-get and installed inputattach and ldattach.
Set ldattach -s 1200 MOUSE /dev/ttyAMA0
Set inputattach –mousesystems /dev/ttyAMA0 &
sadly this does not seem to have resulted in, as I hoped, a working mouse
Out of ideas – all of the above was new to me!!! Can you offer any pointers please?
Cheers, Alex
benosteen
May 31, 2012
Firstly, nice idea on the serial mouse 🙂 Old ideas are new again! It would be a handy way to open up a USB port (if you have a serial mouse of course!)
I think that a number of years ago, ‘inputattach’ would’ve been what you needed to do to get the mouse to show up. However, I think the “new” device detection/etc will not pick that up.
If you edit /etc/X11/xorg.conf, and change/add the mouse section to use /dev/ttyAMA0 as a device, (with protocol set to auto IIRC), that should do it:
You might want to check that as that is from memory… but that alone – I think – would be a way to get the mouse to show up in X11. You will have to restart X if it is already running.
David K-M
August 9, 2012
A simple question… how are you powering your mouse?
Does it expect to get power from DTR/RTS etc at a “reasonable” current?
You may need to supply the mouse with somewhere between -5 and -12 Volts on these pins (I think that the logic is inverted on RS232 so “1” is -12V and “0” is 12V, you might need to check)
Alvaro
June 29, 2012
hi!! great work, i just have a question, could i use a regular MAX232 to shift the levels, i mean, the RPi CPU uses 3V3, and a regular/common MAX232 uses 5V of i’m not mistaken, so, can i use any MAX232 or do i have to use the MAX3232CPE that some recommend???
benosteen
June 29, 2012
I had some MAX232’s lying around and the datasheet led me to believe that they were somewhat compatible (ie they worked and I wasn’t too scared of killing the Pi) but yes, the MAX3232 are the better line to use 🙂 It’s pin compatible too, so exactly the same circuit will work for both chips.
Mikael Johansson
July 12, 2012
I have a prototype card for driving RC servos from the PRI. Right now it has an easy ascii commandinterface via ttyAMA0…
Dissy
February 2, 2013
A suggested correction – You quote the serial device name as “/dev/ttyAMAo” ending with a lower-case letter O (oh), while the correct character is the number zero (0)
I only mention this seeing a comment also repeating the incorrect device name while trying to setup an Xorg config file, and that one character is all that stands between it working, and haven’t seen a correction or a “I got it working” post since.
Hope that helps clear up any confusion.
Dissy
February 2, 2013
Or perhaps it’s just the font/css used here. Very strangely, when I copy/paste, I get a letter. But in my posting, it shows the same character as elsewhere.
If this is just a wordpress issue, feel free to delete both of these posts. Thanks!