Raspberry Pi Fortune Teller

Make a Raspberry Pi fortune teller that prints your future

Your future will be filled with Raspberry Pis.
  • by Ash (152)
  • Time to complete: 45 minutes

Now you can shape your own destiny with this Raspberry Pi fortune teller!

The goal of this project is to print a random fortune at the push of a button using a Raspberry Pi 3 B+ and this mini thermal printer from Adafruit. In this guide, I'll walk you through everything you need to get started. We'll need some specific software, the printer needs to be set up, and then we'll need to trigger our fortune-telling script with the push of a button.

1

I made a video of the final product so you can see it in action!

2

We’ll begin with a fresh installation of Raspbian. Make sure it’s up to date by visiting our guide on updating Raspbian.

3

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:

sudo raspi-config

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:

sudo reboot

You can find more information on enabling the UART serial interface on Adafruit's website.

4

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.

5

2.1mm jack adapter adafruit

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.

6

Raspberry Pi wired to adafruit thermal printer
Raspberry PiAdafruit Mini Thermal Printer
GNDGround
TXRX

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.

7

Adafruit printer custom receipt

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:

python3 fortunetest.py
8

button wired to raspberry pi

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.

9

python 3 script raspberry pi

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:

python3 fortuneteller.py
10

Fortune Teller python script

Open fortuneteller.py in your favorite text editor.

Formatting your receipt

Under 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

Under the 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.

11

edit crontab file

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:

crontab -e

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.

12

Raspberry Pi Fortune Teller

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.

Have fun!