Blog articles

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.

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.)


from tkinter import *
from tkinter import ttk
from tkinter import font
import time
import datetime

global endTime 

def quit(*args):
def show_time():
    # Get the time remaining until the event
    remainder = endTime -
    # remove the microseconds part
    remainder = remainder - datetime.timedelta(microseconds=remainder.microseconds)
    # Show the time left
    # Trigger the countdown after 1000ms
    root.after(1000, show_time)

# Use tkinter lib for showing the clock
root = Tk()
root.attributes("-fullscreen", True)
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"), rely=0.5, anchor=CENTER)


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/

where “/home/pi/” 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

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!

About the author