RPi + Thermal Printer
On a whim, I bought a tiny thermal receipt printer from adafruit. Why? I don’t know. Because it looked interesting? Don’t YOU want to print random things on paper with a tiny printer? You could even give people bills! I know you’re excited now.
Anyways, there are libraries and ample tutorials that talk about interfacing with an Arduino… but I wanted my printer to have a bit more functionality. The problem with directly interfacing with an Arduino is that it is a very static system. To print new information, I would need to reprogram the Arduino… or I would have to have it plugged into a computer at all times and use the serial port of the Arduino to control the printer. I have a Raspberry Pi, and since it costs $10 more than Arduino but has internet connectivity I decided to get it working with that.
Granted, I’m not the first person by a long shot to try this. I found a number of helpful resources that I’ll link to along the way. That said, like everything in linux, nothing is as simple as it seems. I started with a fully untouched 2012-10-28-wheezy-raspbian.zip image. Here is how you can get your printer working:
- Install the 2012-10-28-wheezy-raspbian.zip (or newer) distro onto a fresh SD card. If you don’t know how to do this, follow the instructions on the RPi Wiki.
- When you boot for the first time, you will be prompted with a configuration utility. Go ahead and expand the disk image (expand-rootfs), configure your keyboard to have the right layout (the default is GB which messes up your #, $, / keys among other things), change your root password, enable the ssh server, and enable the desktop on boot. You don’t really need to do any of this, but these options will help if you want to change things and make your RPi available via internet later.
- Boot into the OS!
- Install some extra python packages that you need. Specifically, you will need a serial package and a imaging package. You can install from the termal via the commands:
sudo apt-get install python-serial sudo apt-get install python-imaging-tk
- Tweak the useraccount’s permissions. The permissions need to be changed to allow the default user (pi) to connect to the GPIO serial port. Add the permissions with the following command: (source)
sudo usermod -a -G dialout pi
- Now the serial port is open… but you can’t use it in your code yet because it gets locked by the Linux FS. I’m not sure why this was left in the distro but it is quite annoying. If you don’t make this change, the GPIO serial device (/dev/ttyAMA0) will report being locked. To fix the erorr, make the following changes, then reboot. (source)
Change the file /boot/cmdline.txt with your favorite text editor (remember to sudo edit because its a normally locked file). You need to remove references to the /dev/ttyAMA0 device. For me, this changed from
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
Remove the following line from your /etc/inittab with your favorite text editor (remember to sudo edit because its a normally locked file). Again, you’re removing references to /dev/ttyAMA0.
#2:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
- Plug your printer into your RPi. Be very careful with the GPIO ports. You want to connect the Tx pin of the RPi to the Rx Pin of the printer. You also want to connect the GND of the printer to the GND of the RPi. Don’t connect the Rx of the RPi to the Tx of the printer- the levels don’t match and you really only need to send data to the printer anyways. Plug in the printer according to the diagrams below. (Source about RPi GPIO Plugs,
- Download the python library for the printer from github. (luopio/py-thermal-printer)
- Modify a couple lines in the printer.py file.
- Make the file executable (chmod 777 printer.py)
- Run the code! (./printer.py) Your printer should spit out a test page.
Feel free to modify the python file to print out custom text / images. I’ll post something in a bit to clarify image printing.