By reading this tutorial you will learn to use your cute raspberry pi to do this :
We will learn how to print a simple “Hello World” using the Adafruit Mini Thermal Receipt Printer.
This is the result of a sunday night of hacking together with a friend. It represent another step forward in the realisation of the christmas project of this blog, an open source web kiosk. I want the kiosk to be able to print, don’t ask me why. Now the printer works.
The ingredients you need to complete this tutorial are :
- A raspberry Pi (we discussed in another post how we initially configured it) connected to a monitor
- An Adafruit Mini Thermal Receipt Printer (you can find the user manual here)
- A roll of 50’ long thermal receipt paper - the perfect amount for the thermal printer. BPA-free.
- 5V 2A power supply - an ideal supply for powering the thermal printer (and anything else that can use 5V power)
- 2.1mm DC jack adapter - makes it easy to attach the power supply to the printer
- Tape, red wine, a gospel playlist. (kidding, you don’t really need any tape now).
I suggest you to buy the starter kit that contains everything you need (but the rpi).
This tutorial is inspired to two articles written by Tobias Floery (1, 2), who we should all thank for his contribution. However his tutorials are not really “noob-proof”, so that’s why I’m writing this down. Lets divide and conquer the tutorial in three parts :
- Connect the printer to the power supply
- Connect the printer to the rpi
- Print Hello World
Before you start, make sure you have the two cables that comes with the printer.
Part 1: Connect the printer to the power supply
First of all don’t forget to turn off the raspberry pi, please: if you are connected to it via ssh you can type
sudo shutdown -h now
and unplug the power source. Then connect the cables to the back of the printer as shown in the picture below.
In this first phase we can ignore the green/yellow/red and focus on connecting the DC IN (black/red cable) to the power. For this purpose you need to plug the 5V 2A power supply to the 2.1 mm DC jack adapter, both provided with the kit. Afterwards you can connect the jack adapter to the red/black cable as shown in figure below:
Now you can plug it to the power adapter, and if everything went fine you can see how the green led on the printer starts to blink. Now open the printer, insert the roll of paper inside the printer (image below) and close it.
Press the only button on the printer and see if the paper is rolled out. Does it work? Do it again, and again, just for the thrill of it.
Part 2: Connect the printer to Raspi
Now plug the cables to the pin #3 and number #8 of the GPIO, as shown in the detail below.
If you didn’t do it before, now simply connect it to the TTL socket in the back of the printer : the black cables goes into GND, and the yellow into the RX. Green dude, rest in peace.
Done, we are now ready for the software part of this tutorial. I told you that it wasn’t that hard. Now lets try to make it work.
Part 3: Print Hello World
To interface the rpi with the printer we will use the driver class adapted from arduino and available on github. Its a c++ application based on the Qt framework, hence we need to install some dependencies to make it work. Let’s start installing Qt4 on the rpi by getting some prerequisites (you might already installed some of them):
sudo apt-get install gcc xterm git-core subversion
And finally the application framework :
sudo apt-get install qt4-dev-tools qtcreator
Now we need a component written for Qt that lets the framework communicate with the serial port: qextserialport. To install it statically in the system clone it and compile as following :
git clone https://code.google.com/p/qextserialport/
sudo make install
rm -r quextserialport
then download and compile the Qt project for the printer from github . (This will be your working directory from now on).
git clone https://github.com/thefloe1/Raspi-Thermal-Printer.git
Sweet! Now we have everything we need to make it work. For the next part, we need to open the Qt framework application user interface, therefore launch X (if you dint’ do it before) :
Now open the File Manager (under the menu Accessories) and navigate to the Raspi-Thermal-Printerfolder downloaded previously, and then to the examples/printertest/ subfolder, which contains the working example. Right click the printest.proTools and select QRcreator to open the project file with the Qt environment.
Then we need to configure the environment by selecting the menu Tools -> Options then selecting the Build & Run element on the left, and finally the Tools Chains tab. Press the Add button, and chose GCC from the dropdown menu. Select it in the option window and make sure to insert the following parameters:
compiler path -> /usr/bin/arm-linux-gnueabihf-gcc-4.6
Debugger : /usr/bin/gdb
mkspec : default
Now change tab and click the Qt versions tab, press add and select /usr/bin/qmake-qt4 . Apply changes by pressing the ok button.
Another configuration step is telling Qt to deploy locally and not to a remote linux machine. Select the menu item Help -> About plugins and uncheck Device support -> Remote linux.
Now we need to change some system settings: close Qt Creator and open the terminal. We need to free the tty port of the GPIO (pin #8). Type
sudo pico /boot/cmdline.txt
and replace the content with
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
Close the file and save ( Control + x then hit y). Last modification is to set the slot free on boot by editing inittab :
sudo pico /etc/inittab
and commenting (#) the line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Now reboot raspberry to make the changes effective.
Ok, I know, it was a little bit boring so far, but now you are ready to print! Let’s try something out.
Re-open the file Raspi-Thermal-Printer/examples/printertest/printetst.pro with Qt Creator (right click). Find the file printertest.cpp located under Sources in the left column and play with it. This is how you print using different styles. If you want you can edit the file by replacing the p->write part with a simple Hello World:
p->write("\n Hello, (physical) world!\n");
Save the file. Now press build the project by pressing the big hammer icon located in the bottom left cornet of Qt Creator.
This will create a new folder with the compiled application. Navigate to it (it depends on where the folder is located on your disk). In my case :
Now, call your mum and your best friends over skype, and prepare them to the big event. Open the terminal, and to finally print, type :
Clap your hands, congratulate yourself, and have huge fun. In the next posts we will learn how to make more complex stuff and we will write some more code to make printing a little more intuitive.
I hope I dind’t make too many mistakes. If you had any doubts or just a simple “thank you” feed back, please leave a comment full of love at the bottom of this page, so I know that someone actually is reading this stuff. It keeps me motivated to write ;)
Subscribe to this blog via rss.