Home Code A Raspberry Pi digital clock using the Sense HAT

A Raspberry Pi digital clock using the Sense HAT

by shedboy71

The Sense HAT is an add-on board for Raspberry Pi, made especially for the Astro Pi mission.

The Sense HAT has an 8×8 RGB LED matrix, a five-button joystick and includes the following sensors:

  • Gyroscope
  • Accelerometer
  • Magnetometer
  • Temperature
  • Barometric pressure
  • Humidity

In this example we create a digital clock example

Parts Required

Name Link
Raspberry Pi 4 Aliexpress product link

Amazon. com link

Ebay search

Sense hat AliExpress Product link

Amazon link

ebay link

Getting Started

Lets get started, attach the Sense Hat to your Raspberry PI and power it on

Make sure that Raspbian is up to date, open a terminal window and enter the following

sudo apt-get update
sudo apt-get upgrade

The Sense HAT library is already installed on the later versions of Raspbian, if you type in the following it will either tell you that the library is installed or it will install it for you.

sudo apt-get install sense-hat
sense hat installed

sense hat installed

I started Mu editor ,you may have to use an alternative depending on the version of Raspbian you have installed. Create a new file and enter the code below

Code Example

#!/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%10)][index])

for index in range(0, 4):
    clockImage.extend(number[int(minute/10)][index])
    clockImage.extend(number[int(minute%10)][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)

Now run your code – in Mu you simply need to click on the run button

You can change the colors with these lines

hourColor = [255,0,0] # Red
minuteColor = [0,255,255] # Cyan

Green would be 0,255,0
Blue would be 0,0,255

 

Download

Code is available at https://github.com/getelectronics/PIBits/blob/master/python/senseclock.py

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More