As an Amazon Associate I earn from qualifying purchases.
The Covid-19 pandemic has been hard for everyone. Why not give yourself a break and create this unique Raspberry Pi Zero project. This motivational quote bot will display a new quote every hour. It sits on my desk and gets me through the day.
Parts you will need
For this project you will need the following items:
|Raspberry Pi Zero W
|I recommend getting the Raspberry Pi Zero WH (Zero W with Headers) @ Amazon as it already includes the header need for the Waveshare screen. No soldering required!
|Waveshare e-paper 2.13 inch e-Paper Display Hat
|I used the Waveshare 2.13 inch e-Paper Display Hat @ Amazon for this project.
|Micro SD card
|I used a Sandisk 16GB Micro SD card for this project.
|microUSB power supply.
|3D Printed case
|You can download the 3D files below or buy the case from me here Raspberry Pi Zero Case (With e-paper cutout) @ Etsy.
You will also need an installation of Raspberry Pi OS on your micro SD card. If you are unsure of how to do this, please read the post How to Install an OS on the Raspberry Pi.
Inserting the Raspberry Pi into the case
Start by pushing your Raspberry Pi Zero into the provided case. Note: Please make sure your SD card is removed before trying to fit the Raspberry Pi into this case. Failure to do this will damage the card
Your Raspberry Pi should fit snugly into the case. If you printed this case yourself and it does not fit, try increasing the print size by 1 or 2%.
If you have already have a Raspberry Pi Zero and you don’t have header pins, you will need to solder these pins to your board. Otherwise, the Waveshare e-Paper screen should fit right on top of your Raspberry Pi. Getting the screen into place inside the case can be a little tricky. Do not force the screen on the Pi as you will bend the pins
Installing the cover
Place the cover on top of the case and push firmly into place. After this, carefully insert the micro SD card back into the Raspberry Pi. The final assembly should look like below:
Writing the Python code
Cloning the Repository
Power up the Raspberry Pi. You will now need to clone my
Pi-Motivator repository. This repository includes the necessary Waveshare libraries, fonts and a file named
quotes.json which is used to store the quotes:
git clone https://github.com/ConorJOHanlon/Pi-Motivator.git
Now change directory to the
Your directory structure should look like the following:
pi@mypi:~/Pi-Motivator $ ls
fontawesome-webfont.tff Font.ttc lib quotes.json README.md
Importing the libraries
Create a new Python file named
motivator.py in the current folder:
You will also need to install the
pillow library for Python. This can be done using
pi@mypi:~/Pi-Motivator $ pip install pillow
motivator.py in the editor of your choice. I will be using
vim for this tutorial. Start by importing
textwrap. You will be using these imports to load in the
quotes.json file, picking a random quote and wrapping the text to make sure that it fits on the screen,
epd2in13_V2 from lib,
from lib import epd2in13_V2
from PIL import Image,ImageDraw,ImageFont
Now, you need to define the font you will be using to display the text. Make a reference to the font provided in the repository with the size as 16:
base_font = ImageFont.truetype('Font.ttc', 16)
Getting the quotes from quote.json
Create a method named
get_quotes with no arguments. This method will open the
quotes.json file, pick a random quote and return the
filename = "quotes.json"
with open(filename, encoding="utf8") as json_file:
data = json.load(json_file)
r = random.randint(0,len(data))
title = data[r]['from']
message = data[r]['text']
return title, message
Now that you have a way to get your quote, you need to print it to the screen. Initialize the module and print a white box which covers the entire screen. This will clear the frame:
epd = epd2in13_V2.EPD()
image = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
draw = ImageDraw.Draw(image)
The motivational quote bot displays a new quote every hour. To do this let’s go ahead and create a while loop. When a message gets printed, you will want to draw a white rectangle first to clear the screen:
draw.rectangle((0, 0, epd.height, epd.width), fill = 255)
Go ahead and get a random quote by using the method created earlier. You will also want to break this text into multiple lines using
textwrap.fill . In my case I chose to break after 30 characters which made the text fit the screen perfectly. Note: I am only using the
message variable here but if you had a bigger screen you could use both:
title, message = get_quote()
message = textwrap.fill(message,30)
Once you have your quote, print it to the screen using
epd.displayPartial. After this, sleep for 60 minutes before displaying a new quote:
draw.text((10,20), message, font = base_font, fill = 0)
Finally, run your code with the following command:
pi@mypi:~/Pi-Motivator $ python3 motivator.py
And that’s it for creating this unique Raspberry Pi Zero project! As always, if you have any questions or comments please feel free to post them below. Additionally, if you run into any issues please let me know