Make a unique clock out of your favorite LEGO set, a Pi Zero W and some LEDs


LEGO LED clockWith this easy to follow instruction you can turn every LEGO set into an awesome clock, that uses LEDs to show the time every 15 minutes. I used an old LEGO spaceship from the late 70s, because it was my first LEGO set, but you can use almost any set you like as long as you can fit a Pi Zero (or any other Raspberry Pi) inside. And even if there’s no space for the Pi, you can still install the LEDs and hide the Pi somewhere else.

How does it work?

This LEGO clock basically works like a church bell. As you probably know, some churches broadcast the time every 15 minutes by ringing their bells. Here’s how it works:

Usually the clock strikes once at 1:00 a.m., twice at 2:00 a.m., all the way up to twelve times at 12:00 p.m. After that it starts again, striking once at 1:00 p.m., twice at 2:00 p.m. and so on. By the way, this striking feature comes from a time before there were clock faces to show the time visually.

Some churches (like Big Ben) also strike every quarter by playing a little melody. So every quarter hour, you’re reminded of the time.

As mentioned before, the LEGO LED-clock works very similar. It lights the LEDs every quarter hour so you can tell what time it is: Every full hour, all five LEDs light up. When its 3 p.m., they flash 3 times, at 4 p.m. they flash 4 times and so on… When it comes to the quarters, our LEGO-clock works a little bit different: At 3.15 p.m., the green LEDs (indicating the hours) flash 3 times and the red ones (indicating the quarters) once. So at 3.30 p.m., the green LEDs blink 3 times and the red ones twice. And what happens at 3.45? Right! The green LEDs blink 3 times and the red ones also 3 times. Here`s an example:

This may sound a little difficult, but it really isn’t and you get used to it pretty quickly.

What do I need?

  • your favorite LEGO set
  • a Raspberry Pi Zero W (or any other Raspberry Pi, of it fits in your set. Just make sure that it has wireless lan, because it makes the setup much more easy and is also used to set the clock to the right time. The Raspberry Pi has no internal clock, so it forgets the time every time you turn it off.
  • a micro-SD-card (at least 8 GB) with a standard Raspbian image. You can download it here – there’s also an instruction how to copy the files to your card.
  • an LED Light kit for a LEGO model of your choice. These kits are available on eBay for a lot of different models. Please note that these kits are NOT sold by LEGO directly, but are custom made. They usually include several LEDs as well as a battery box, that’s connected via a standard USB cable. So just pick one that fits your project. You don’t have to worry about the colors, because you can separate the LEDs from the parts to which they’re attached to quite easily. After that, you can put them anywhere you like, for example under another translucent LEGO-part. The LEDs are very, very tiny so they fit almost everywhere. The cables are also very small and robust, so you can clamp them between two bricks without damaging them.
  • a wire cutter
  • a soldering iron (and of course some solder)
  • 3V-CR2032 button cell battery (to check the orientation of the LED)
  • You also need some way to connect the Pi Zero W to the internet and a computer to write the program. But I guess if you’re reading this you probably already have internet access as well as a computer, am I right?
  • Depending on where you want to put your LEGO model, you also need a USB cable or a power supply to power the Pi Zero W. When you have finished building your LEGO clock, you just need to put in the power cord and it boots automatically in „clock-mode“.

How do I start?

Let`s start by taking a closer look at a LEGO LED set, that is sold on eBay (this is the one for the LEGO beetle):

IMG_5475

As mentioned before, this isn’t an official LEGO product – in fact it`s hand-made and you can clearly see that, when you open up the USB connector:

OLYMPUS DIGITAL CAMERA

The cables are quite simple soldered to the 5V power output and the ground-connector. You can also see two resistors, which you won`t need anymore, because the Raspberry Pi Pins only have 3,3V voltage. Therefore we can connect the LEDs to the Pi without using any resistor.

You can see, that the LEDs are in fact very tiny. They’re so tiny, that they can fit inside this translucent LEGO piece.

OLYMPUS DIGITAL CAMERA

If you’re happy with the color, fine! Otherwise just take another translucent LEGO piece and put the LED underneath it. You can drill a little hole, but it also works fine if you just clamp the LED between two LEGO pieces.

In order to get the LED out, just cut the wires. Make them as long as possible, you can always shorten them afterwards. Now take out the LED, but be careful not to rip apart any of the cables (have I mentioned that the LED is very tiny?).

OLYMPUS DIGITAL CAMERA

For my project, I use four of the tiny LEDs and this 3 LED piece, which was included in the kit to illuminate the interior of the beetle:

OLYMPUS DIGITAL CAMERA

How do I connect the LEDs to the Raspberry Pi?

Simply by soldering them to the GPIO-port. The Pi Zero W has 40 small holes – this is the GPIO-port. Now let’s have a look at the layout of these Pins:

Pi Zero Pins

Each LED has to be connected to a ground-pin (which are black in the picture above) and one of the GPIO-pins (blue). I used the following pins:

  • 3 LEDs-brick (interior): GPIO21
  • red LED on the left wing: GPIO 16
  • green led on the left side: GPIO 12
  • red LED on the right wing: GPIO 25
  • green LED on the right side: GPIO 4

In order to check, which of the two wires coming from the LED is the ground-wire, just remove the insulation at the end of the two cables (be careful not to cut the wire) and hold the ends at the two sides of a 3V-CR2032 button cell battery. If the LED lights up, remember which cable was connected to the minus pole, because that cable has to be connected to a ground pin on the Raspberry Pi. If the LED doesn’t light up, change the orientation of the cables. Don’t worry – this won’t damage the LED.

Now you can solder the cables to the Raspberry Pi. Just stick the cables through the hole and use some solder to keep it in place. Here’s how my Pi looked after I soldered all the cables to the board:

IMG_5516

Now you just need to fit in all inside your LEGO-set. Here are some pictures of my build (with the LEGO-set 978 from 1979).

IMG_5511

IMG_5515

IMG_5514

IMG_5525

IMG_5523

LEGO LED clock

How can I tell, if I connected the LEDs the right way?

To see if you’ve done everything the right way, power up the Pi (with a standard Raspbian image) and open the command line interface. Then start python by simply typing:

python

Now you should see this:

>>>

Then type in the following commands (without the three Greater-than-signs at the beginning):

>>> from gpiozero import LED

>>> a = LED(21)

>>> a.on()

>>> a.off()

If the command „a.on()“ switched the LED on and „a.off()“ turned it off, everything`s fine! You can also try the other LEDs by typing in the same code again (except the first line) and changing the GPIO-port. For example if you want to check, if the LED connected to GPIO-port 16 is working, type in:

>>> a = LED(16)

>>> a.on()

>>> a.off()

Easy, wasn’t it? Now there’s another really nice feature in the gpiozero-package that let`s you pulse the LED, so it slowly turns on and off. You can try it out with the following code:

>>> from gpiozero import PWMLED

>>> a = PWMLED(16)

>>> a.pulse()

>>> a.off()

Looks cool, right? That’s pretty much all we need for our clock.

So far, so good – but how do I tell my Raspberry Pi to light the LEDs at the right time?

Therefore we need another python-package called „datetime“. Let me tell you how to do use it by starting writing our clock-program. If you’re not interested in the programming-part, you can skip this paragraph and just download the program. But I`​d like to encourage you to read it, because it makes you smarter and let`s you adjust the clock in any way you need.

OK, let`s go: If you’re still in the python-console, press „CTRL-D“ to exit.  Then type the following:

nano clock.py

This opens the nano-editor with a new file called „clock.py“. Now type in the following (and make sure you indent the lines exactly like it`s shown or the program won’t work):

from gpiozero import PWMLED
from time import sleep
import datetime

interior = PWMLED(21)
red_left = PWMLED(16)
green_left = PWMLED(12)
red_right = PWMLED(25)
green_right = PWMLED(4)

while True:

time = datetime.datetime.now().strftime(„%H:%M“)
if (time == „01:00“) or (time == „13:00“):

interior.pulse()
red_left.pulse()
green_left.pulse()
red_right.pulse()
green_right.pulse()
sleep(2.04)
interior.off()
red_left.off()
green_left.off()
red_right.off()
green_right.off()
sleep(60)

sleep(0.5)

First thing we did was to change the name of the variables for the LEDs, so that we can see in the code, which LED is turned on or off. My spaceship has two green and two red LEDs and one piece with three LEDs for the interior.

Then the program checks the time, formats it in the way we want and writes it in a variable called „time“. Then there is an if-statement that checks, if the time is either 01:00 or 13:00. If that`s the case, then all the LEDs blink once and are then turned off. They blink only once, because the program waits exactly 2.04 seconds, which is the time for one pulse of the LED. Then the program waits another 60 seconds, otherwise the LED would start blinking again and we want the clock to display the time only once.

Then the program wait half a second, writes the actual time in the variable and then checks again, if the if-statement is true.

If you now save the program by pressing „CTRL-X“ (and then confirm to save the program by pressing „Y“ and enter), you can run it by typing:

python clock.py

Most likely, nothing will happen now, because the LEDs only blink, if its either exactly 1 a.m. or exactly 1 p.m. Now you can either wait or just change the time by opening another terminal-window and typing:

sudo timedatectl set-ntp 0

and then:

sudo date -s ‚2019-01-01 01:00:00‘

The first command turns off a daemon that syncs the time over the internet, the second sets the clock to 01:00. Now the LEDs should start blinking. If you want to test another time, cancel the python-program (running in the other command-window) by pressing „CTRL-C“, then run it again and switch to the other window to change the time again.

Don’t forget to turn on the syncing of the clock if you’re done with testing by typing:

sudo timedatectl set-ntp 1

Next thing we need to do is to tell the clock what it should do when its either 01:15 a.m. or 01:15 p.m. Why don’t you just take a look at the code and try to figure out for yourself, what will happen (the first half of the program stays the same):

from gpiozero import PWMLED
from time import sleep
import datetime

interior_led = PWMLED(21)
red_left = PWMLED(16)
green_left = PWMLED(12)
red_right = PWMLED(25)
green_right = PWMLED(4)

while True:

time = datetime.datetime.now().strftime(„%H:%M“)
if (time == „01:00“) or (time == „13:00“):

interior_led.pulse()
red_left.pulse()
green_left.pulse()
red_right.pulse()
green_right.pulse()
sleep(2.04)
interior_led.off()
red_left.off()
green_left.off()
red_right.off()
green_right.off()
sleep(60)

elif (time == „01:15“) or (time == „13:15“):

green_left.pulse()
green_right.pulse()
sleep(2.04)
green_left.off()
green_right.off()
red_left.pulse()
red_right.pulse()
sleep(2.04)
red_left.off()
red_right.off()
sleep(60)

sleep(0.5)

So this program turns on the green LEDs to tell the hours and then the red LEDs to show the quarters. Both LEDs blink once so it`s either 01:15 a.m. or 01.15 p.m.

This is the way, I want my clock to work, but of course you’re free to change the code and make the clock blink in any way you like.

Now you just need to copy, paste and change the code for every time. Yes – that’s a bit of work, but fortunately you can just download the program I wrote and use it the way it is or as a template to make the changes you want.

Once you’re done programming, just start the program and the clock should be ready.

How do I make the program run on startup?

Simple! Just type this in the command line:

sudo crontab -e

Then choose your favorite editor (I use nano) and use the arrow key to navigate to the bottom and add the following line (suggesting you saved your file in the home-directory, otherwise just change the path):

@reboot sudo python /home/pi/clock.py &

The clock is awesome! What else can I do with a Raspberry Pi and some LEGO bricks?

Well here are a few examples of things I made:

  • A Pi-powered Arcade Machine:

  • A vacation-reminder:

There’s a lot more you can do with LEGO and a Raspberry Pi – I wrote an entire article in my blog about this topic – feel free to have a look, but be warned that it is written in german.

I like it, but what do I actually do with my LEGO-clock?

I`ve put my LEGO-clock on top of my Apple II so I can see how long I’ve been lost in Britannia playing Ultima. What do you do with your LEGO-clock? Please leave a comment or send me a picture on twitter (@medienistik).

Now I look at thinks in a different way wondering if I can fit a Raspberry Pi inside – should I have to worry?

Well, you’re probably suffering from the Raspberry Pi syndrom. I had a discussion about it with the guys from the Pi Foundation and we discovered that more and more people are affected. I’m not sure about the long-term effect, maybe just go ask your doctor.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden /  Ändern )

Google Foto

Du kommentierst mit Deinem Google-Konto. Abmelden /  Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.