You will need to enable the UART serial interface on the Raspberry Pi. Like most interfaces, this is disabled on the Pi by default. Open the Raspberry Pi configuration menu with the following command:
Under Interfacing Options choose Serial.
You will be asked, "Would you like a login shell to be accessible over serial?" Select No.
When you see the question, "Would you like the serial port hardware to be enabled?" Choose Yes.
Finally, reboot the Raspberry Pi:
You can find more information on enabling the UART serial interface on Adafruit's website.
For our fortune teller script to work, we'll need to use Python 3 as well as a few libraries.
To install Python 3, use the following command:
sudo pip3 install --upgrade setuptools
Next up we’ll need blinka by Adafruit:
pip3 install adafruit-blinka
Then, install the RPI.GPIO library:
pip3 install RPI.GPIO
And lastly, we’ll need the thermal printer library from Adafruit:
sudo pip3 install adafruit-circuitpython-thermal-printer
You can find more details about this process on Adafruit's website.
The printer will need its own source of power, it cannot run off USB. I opted to use the 5V 2A power supply and 2.1mm jack adapter sold by Adafruit. You can find more details about powering the mini thermal printer here.
To set up the jack adapter, you will need to cut the wires provided with the printer. Strip away the wire sleeve and attach the inner wires to the jack adapter by screwing them into place. When the printer is receiving power, the LED on top will illuminate and often flash.
|Raspberry Pi||Adafruit Mini Thermal Printer|
Wiring the printer to the Pi isn't too difficult. I used the original wires provided by Adafruit and connected them to the Pi using a couple of FTM jumper wires. The printer's ground wire should be wired to any GND GPIO pin on the Pi. The RX wire needs to connect to the Pi's TX pin.
If you need help identifying your Pi's pins, Pinout.xyz is a great resource.
Once everything is connected, restart the Pi. We’ll need to run a test script. This is our opportunity to see if we can print a custom receipt. I created a short test script you can download from Github.
Open a terminal window and run the script using Python 3 like this:
The button will need to be wired to the Pi with a resistor. I soldered jumper wires to my button and wrapped the connection in heat shrink tubing. You can read more about creating a proper button circuit on the Raspberry Pi here.
I’ve wired my arcade button to pins 1 and 18 (3.3V and GPIO 5). My fortune teller script uses the physical GPIO pin numbers. If you use a different pin, be sure to update the script accordingly.
Once you have a working button and printing thermal printer, it’s time to test the fortune teller script!
Download the fortune teller script from Github. Transfer the python script to your Raspberry Pi using your preferred method -- such as remotely downloading the script from the Pi itself. I have a Windows machine and use WinSCP for my file transfers.
You can also log into your Pi, create a new file, and then paste the script in place:
sudo nano fortuneteller.py
Once the script is in place, execute it using Python 3 like so:
fortuneteller.py in your favorite text editor.
Formatting your receipt
def getfortune(): you’ll find the default receipt I built. It uses the Adafruit Thermal Printer library to add several lines of custom formatted text. I highly suggest reviewing the official Adafruit documentation on customizing your receipt.
Add your own fortunes
fortunes variable is a list. Edit the existing fortunes and add your own to the list. Be sure to keep the text within the apostrophes and end each line with a comma.
There are several ways to make a python script initiate on bootup. I’ll use crontab for this guide, but it’s definitely worth familiarizing yourself with other methods.
Using a terminal, open crontab with the following string:
Choose option 2. At the bottom of the document that opens, add the path to the
fortuneteller.py file. You may need to change this string if you put yours in a different location.
@reboot sudo python3 /home/pi/fortuneteller.py
Save the document and close crontab. When you restart the Pi, give it a test and try printing a fortune with the button.
Finishing up this project is fun. I created a custom case shaped like a takeout box to house the printer and Raspberry Pi. To fit with the "to-go" theme, the fortunes are labeled with "Fortunes 2 Go" at the top. ;)
You can print this case yourself by downloading the STL on Thingiverse. After printing it, use a bit of rigid wire (like a coat hanger) to make a handle.