1 year ago

Build a binary clock

Solve real-world electronic and engineering problems with your Raspberry Pi and the help of renowned technology hacker
and author, Simon Monk

This is a very simple project to make. In fact, all you really need to do is plug the Sense or Unicorn HAT onto the Raspberry Pi’s GPIO header and install the software.

This project displays the current time in binary. The rows of the display are (from top to bottom) the last two digits of the year (e.g. 15), the month number, the day of the month, the hour (24-hour format), the minute, the second, and finally hundredths of a second.

The full article can be found in The MagPi 42 and was written by Simon Monk

You’ll Need

Raspberry Pi with 40-pin GPIO header (A+, B+, 2 and 3)

Sense HAT or Pimoroni Unicorn HAT

USB WiFi adaptor or Ethernet connection

Reading the binary clock

To read a binary clock, you either need to be good at recognising binary numbers or use a bit of arithmetic to calculate the time, by adding up the binary digit values for the LEDs that are lit on a particular row.

For example, the first row displays the year in two-digit format. So, if LEDs at positions 8, 4, 2, and 1 are all lit, then the year is 8+4+2+1 = 15. In 2016, just the digit position 16 will be lit.

The colours of the LEDs have no significance – they are different to make the display more interesting.

How to read the binary clock

How to read the binary clock

If you’re using the Sense HAT, you’ll need to install the software for the HAT using the following commands:

If you’re using a Unicorn HAT, then run the following commands:

There are two versions of the software for this project – one for the Sense HAT and one for the Pimoroni Unicorn HAT – so be sure to use the right version of the program for your hardware. The Sense HAT version is called binary_clock_sh.py, and the Unicorn version binary_clock_uni.py.

You can download the programs for this project from your Raspberry Pi command line using the command:

This command will actually bring down the code for all the projects in Simon’s The MagPi series, so if you’ve already issued this command for one of the earlier articles, change directory to pi_magazine and run the following command to update your directory with this project (12_binary_clock):

We’re also listing the code at the bottom of the page.

To run the program, change to the directory where the code for this project lives and then run the program using the commands below:

Or, if you are using the Unicorn HAT:

How the code works

The two versions are actually very similar to each other. Both start by importing the libraries that they need for their displays. They then define a number of colour variables that you can change, if you like, in order to set different colours for each row of the clock.

Most of the action takes place in the display_binary function. This takes three parameters. The first is the value to be displayed as binary, the second is the row on which it should be displayed (0 to 7), and the final parameter is the colour to be displayed.

The value is converted into a binary string representation using String’s format function. The for loop then iterates over each column (x value) and if that digit contains a 1, turns on the pixel in that row and column to the colour specified, otherwise the pixel is cleared.

The main loop fetches the current time and then splits it into its component parts, displaying each on a separate row. To display the year in two-digit format, we use modulo 100 (t.year % 100), which is the remainder after dividing it by 100.

Using your binary clock

Having a keyboard, mouse and monitor attached to your clock is fine while you are constructing it, but it would be better to have the program start automatically when the Raspberry Pi first boots up. To do this, run the following command to make the program executable:

Then, edit the file /etc/rc.local with the command:

Add the following line after the first block of comment lines that begin with #. Change the line to the _uni version if you are using a Unicorn HAT.

Restart your Raspberry Pi and this time the program should start up automatically.

Note that you will still need an internet connection for the Pi to pick up the correct time.

Code listing



  • Pratik kunkolienker

    Does the PI actively have to be connected to the internet?