Building a countdown clock with Raspberry Pi and Python
By Lars Klint | August 24, 2017
To mark the days until Pluralsight LIVE 2017, I decided to build a dedicated clock. To embrace the maker and IoT spirit that Pluralsight LIVE celebrates, I realized a Raspberry Pi would be the perfect realization of my idea. The current model RP3 is readily available from online and physical stores, and it is inexpensive as well.
The clock counted down to the start of the Pluralsight LIVE event at 8am on September 19, 2017, but can be adjusted to any date and time you’d like.
To complete the steps in this guide, you will need the following hardware and software:
- Raspberry Pi 3 running Raspbian
- Raspberry Pi 7” Touchscreen
- A USB Keyboard and Mouse
Installing the Touchscreen
The first step, before we get to the code, is to install and configure the touchscreen. The official Raspberry Pi touchscreen can be mounted directly to a Raspberry Pi 2 or 3 with the included kit. This makes for a neat compact unit.
But before you mount the screen, you need to make sure the RP3 is updated with the right packages to use the screen.
Run the following commands in the Raspbian command prompt to get your RP up to date:
sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade sudo apt-get install raspberrypi-ui-mods sudo apt-get install raspberrypi-net-mods
Then run the following command to shut down safely:
You can now install the touchscreen using the included components. For more detailed information on the installation process, see this video.
Note: You can also power the screen separately with a second micro USB power source. If you want to use a single power source, you’ll need a 2A power supply.
Coding the Countdown
We will use the programming language Python which is common for the Raspberry Pi.
Open the Thonny Python IDE editor on the Raspbian desktop in Menu->Programming -> Thonny Python IDE.
You will see a new window open with a cursor ready to write your new Python script.
Copy the following code into the editor. (If you are reading this on a computer other than the RP, you can use a USB storage device to copy the code onto the PI.)
#!/usr/bin/python from tkinter import * from tkinter import ttk from tkinter import font import time import datetime global endTime def quit(*args): root.destroy() def show_time(): # Get the time remaining until the event remainder = endTime - datetime.datetime.now() # remove the microseconds part remainder = remainder - datetime.timedelta(microseconds=remainder.microseconds) # Show the time left txt.set(remainder) # Trigger the countdown after 1000ms root.after(1000, show_time) # Use tkinter lib for showing the clock root = Tk() root.attributes("-fullscreen", True) root.configure(background='black') root.bind("x", quit) root.after(1000, show_time) # Set the end date and time for the countdown endTime = datetime.datetime(2017, 9, 19, 9, 0, 0) fnt = font.Font(family='Helvetica', size=60, weight='bold') txt = StringVar() lbl = ttk.Label(root, textvariable=txt, font=fnt, foreground="green", background="black") lbl.place(relx=0.5, rely=0.5, anchor=CENTER) root.mainloop()
Press F5 to run the program and voila—you have now a countdown timer for the start of Pluralsight LIVE 2017.
Putting it all together, this is what you will see:
Auto-Start the Countdown (Optional)
Now we need to tell the operating system to run the script for the Pi user. In the command prompt or in a terminal window, enter the following command:
sudo nano /etc/profile
Scroll to the bottom and add the following line:
sudo python /home/pi/myscript.py
where “/home/pi/myscript.py” is the path to your script.
Type “Ctrl+X” to exit, then “Y” to save followed by “Enter”.
To test if this has worked, reboot your Pi using:
Now you are ready to countdown to the event of your choice with a Raspberry Pi countdown clock. Enjoy!
For more information about Python and helpful tips and advice for your next project, check out guide on How to Learn Python today!
5 keys to successful organizational design
How do you create an organization that is nimble, flexible and takes a fresh view of team structure? These are the keys to creating and maintaining a successful business that will last the test of time.Read more
8 ways to stand out in your stand-up meetings
Whether you call them stand-ups, scrums, or morning circles, here's some secrets to standing out and helping everyone get the most out of them.Read more
Technology in 2025: Prepare your workforce
The key to surviving this new industrial revolution is leading it. That requires two key elements of agile businesses: awareness of disruptive technology and a plan to develop talent that can make the most of it.Read more