Article

Building a countdown clock with Raspberry Pi and Python

August 24, 2017  |  Lars Klint
Learn something new. Take control of your career.
Sign up

The inaugural Pluralsight LIVE is fast approaching (September 19-21), and to mark the remaining time, 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 will count 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.

Equipment Needed

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:

sudo shutdown

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:

sudo reboot

With my countdown clock is ready to go, I’m curious, how are you counting down to Pluralsight LIVE? With insightful breakout sessions, incredible mainstage speakers, and the opportunity to meet an incredible community face-to-face, Pluralsight’s first-ever user conference is sure to be one to remember.

Learn something new. Take control of your career.
Sign up

Lars Klint

Lars is an author, trainer, Microsoft MVP, community leader, authority on all things Windows Platform and part-time crocodile wrangler. He... See more