Maker.io main logo

Build a Sense HAT Raspberry Pi Digital Clock

2022-12-14 | By Maker.io Staff

License: See Original Project

Digital clocks or watches use traditional optoelectronic display technologies like a ‎liquid crystal display (LCD) or a seven-segment light-emitting diode (LED) to ‎provide visual numbers for the time. The Sense HAT-based Raspberry Pi (RPi) ‎Digital Clock uses a matrix of programmable-addressable discrete LEDs that can ‎display various characters, scrolling text, and numbers. With an 8x8 matrix, the ‎Sense HAT provides 64 pixels for displaying visual information. ‎

hat_1‎ ‎

The RPi Sense HAT.‎

With an RPi Sense HAT digital clock device, the user will enter a basic python script ‎command to operate the clock. The current time will be displayed on the Sense ‎HAT LED matrix by tapping the up-arrow key, followed by the enter key. The ‎python script allows a simple Linux command to run the digital clock code on ‎either a Raspberry Pi 3 or 4 single-board computer (SBC) with an attached ‎keyboard. Another option is to secure-shell (SSH) or virtual network connection ‎‎(VNC) to remotely run the application headlessly, without an attached LCD ‎monitor. ‎

In this project, you will assemble and test an electronic timepiece of your very ‎own! ‎

Assembly of the Sense HAT RPi Digital Clock

The assembly process of the digital clock follows. You will take the Sense HAT and ‎place it on top of the RPi. The Sense HAT comes with a 40-pin dual-inline female ‎header connector.‎

place_2

The Sense HAT placement to the RPi.‎

Before assembling the two boards, place a sheet of red transparent plastic over ‎the Sense HAT. This will make it easier to see the info displayed on the LED matrix ‎without it being washed out by ambient light.‎

red_3

Red transparent plastic sheet over the Sense HAT.‎

Then place the Sense HAT onto the RPi. Ensure the Sense HAT is oriented on the Pi ‎as shown below.‎

final_4

The Sense HAT to the RPi final assembly.‎

Now let’s run a simple test to ensure the two boards are operating correctly. Turn ‎on your RPi, and a colorful visual pattern should be displayed on the Sense HAT. ‎This visual display is analogous to power up the RPi attached to an LCD monitor.‎

color_5

The Sense HAT power-up color pattern

Adding the Sense HAT Digital Clock Code

Before the Python code can be added to the RPi, a few preliminary steps ‎regarding the operating system (OS) for the SBC are required. The first step is to ‎ensure the RPi has the latest OS. Open a terminal and type the following Linux ‎commands to upgrade the OS to the latest version of Raspberry Pi OS. ‎

Copy Code
sudo apt update
sudo apt upgrade

The Sense HAT library is required to allow the RPi to work with the electronic PCB ‎LED matrix. The following command will install the Sense HAT library with the RPi.‎

Copy Code
sudo apt install sense-hat

To test the software library installation, let’s create a python file to display Hello ‎World on the Sense HAT. Type in the following Linux command to open the nano ‎editor.‎

Copy Code
sudo nano sensehat_test.py

With the nano editor opened, type the following python code into the terminal.‎

Copy Code
from sense_hat import SenseHat
sense = SenseHat()
sense.show_message(“Hello World”)

After the Hello World code has been saved, exit the nano editor. You will execute ‎the code with the following Linux command.‎

Copy Code
python3 sensehat_test.py

The Hello World message will scroll across the Sense HAT LED matrix, as shown ‎below.‎ ‎

text_6

Scrolling Hello World text.‎

You can observe the operation of this scrolling text message here:

 

If the LED matrix does not scroll the Hello World message, there are software and ‎hardware checks that might help. First, press down on the Sense HAT. This will ‎ensure the Sense HAT header is electrically connected to the RPi. You may ‎consider cycling the power of the RPi to properly reset the supporting ‎microcontroller and power supply circuits.‎

A software approach is to type “sudo reboot” into the terminal to reset the Pi. ‎Once the Sense HAT is working correctly, the final step of the project is the ‎installation and execution of the python digital clock software.‎

Installation of the Python Digital Clock Code

Create a filename for the python digital clock code. This python code will be ‎executed from the home/pi directory of the RPi. To create the digitalclock file, ‎type “nano digitalclock.py” into the terminal. With the editor open, type in the ‎digital clock python code as shown here:‎

Copy Code
#!/usr/bin/env python

from sense_hat import SenseHat
import time

sense = SenseHat()

number = [
[[0,1,1,1], # Zero
[0,1,0,1],
[0,1,0,1],
[0,1,1,1]],
[[0,0,1,0], # One
[0,1,1,0],
[0,0,1,0],
[0,1,1,1]],
[[0,1,1,1], # Two
[0,0,1,1],
[0,1,1,0],
[0,1,1,1]],
[[0,1,1,1], # Three
[0,0,1,1],
[0,0,1,1],
[0,1,1,1]],
[[0,1,0,1], # Four
[0,1,1,1],
[0,0,0,1],
[0,0,0,1]],
[[0,1,1,1], # Five
[0,1,1,0],
[0,0,1,1],
[0,1,1,1]],
[[0,1,0,0], # Six
[0,1,1,1],
[0,1,0,1],
[0,1,1,1]],
[[0,1,1,1], # Seven
[0,0,0,1],
[0,0,1,0],
[0,1,0,0]],
[[0,1,1,1], # Eight
[0,1,1,1],
[0,1,1,1],
[0,1,1,1]],
[[0,1,1,1], # Nine
[0,1,0,1],
[0,1,1,1],
[0,0,0,1]]
]

noNumber = [0,0,0,0]

hourColor = [255,0,0] # Red
minuteColor = [0,255,255] # Cyan
empty = [0,0,0] # Black/Off

clockImage = []

hour = time.localtime().tm_hour
minute = time.localtime().tm_min

for index in range(0, 4):
if (hour >= 10):
clockImage.extend(number[int(hour/10)][index])
else:
clockImage.extend(noNumber)
clockImage.extend(number[int(hour)][index])

for index in range(0, 4):
clockImage.extend(number[int(minute/10)][index])
clockImage.extend(number[int(minute)][index])
for index in range(0, 64):
if (clockImage[index]):
if index < 32:
clockImage[index] = hourColor
else:
clockImage[index] = minuteColor
else:
clockImage[index] = empty

sense.set_rotation(90) # Optional
sense.low_light = True # Optional
sense.set_pixels(clockImage)

With the python code typed into the nano editor, save it by exiting out (^Exit) and ‎typing y for yes. To execute the code, enter the following command:‎

Copy Code
 python3 digitalclock.py

Hit the enter key on the keyboard, and you should see the current time displayed. ‎‎(Note, the time will be shown in a 24-hour format.) ‎

To see an update of the time, click the up arrow on the keyboard. The “python3 ‎digitalclock.py” command will be visible in the terminal - hit the enter key to see ‎the updated time. The below picture illustrates the Sense HAT RPi digital clock in ‎operation. ‎

clock_7

A functional Sense HAT -RPi digital clock

A YouTube video clip shows the digital clock working as well:

 

This project was inspired by Steve Amor’s Sense HAT Raspberry Pi development ‎work: https://github.com/SteveAmor/Raspberry-Pi-Sense-Hat-Clock

制造商零件编号 SC0329
SENSE HAT 8X8 RGB LED 5 BUT
Raspberry Pi
¥244.20
Details
制造商零件编号 SC0442
RASPBERRY PI MOUSE RED
Raspberry Pi
¥65.12
Details
制造商零件编号 SC0059
1M HDMI CABLE WHITE (CPRP010-W)
Raspberry Pi
¥54.95
Details
制造商零件编号 SC0445
AC/DC WALL MNT ADAPTER 5.1V 15W
Raspberry Pi
¥65.12
Details
制造商零件编号 SC0193(9)
RASPBERRY PI 4 B 2GB
Raspberry Pi
¥366.30
Details
制造商零件编号 SC0167
RASPBERRY PI KEYBOARD (US) RED
Raspberry Pi
¥138.38
Details
制造商零件编号 SC0252L
16GB SD CARD NOOBS SOFTWARE
Raspberry Pi
¥75.54
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum