Ternary RGB LED Clock With RP2040-Matrix and MicroPython

by arduinocelentano in Circuits > Clocks

111 Views, 0 Favorites, 0 Comments

Ternary RGB LED Clock With RP2040-Matrix and MicroPython

Ternary RGB LED Clock with RP2040-Matrix
logo.png

Have you ever marveled at the simplicity of a binary LED clock? It’s a fascinating concept where time is represented using just two states: on and off. But what if we could take that idea and elevate it to a whole new level? Introducing the 🌈Ternary RGB LED Clock⏰, where three colors are used to represent ternary digits!

💡I intentionally kept the project as simple as possible so that you can use it in Computer Science or STEM classes to playfully introduce the concepts of numeric systems and the basics of interfacing with the LED matrix. The MicroPython code is about 50 lines long.
🔗 You might also like another RP2040-Matrix project I published here.

Supplies

board.png
  1. RP2040-Matrix development board (alternatively, you may use any generic RP-2040 board with an external NeoPixel LED matrix)
  2. Thonny IDE to program it

The Concept

scheme.png

We'll use red, green, and blue pixels to represent the numbers 0, 1, and 2. The embedded matrix allows us to display five columns, enabling us to show the month, day, hours, minutes, and seconds. In the attached picture, you can see the layout. If you are not familiar with the ternary numeric system, there is an example of calculations provided.

The RP2040 features an embedded real-time clock. However, it does not have internet connectivity, so the time must be set manually.

Installing Thonny IDE

thonny1.png

Now that we've covered how it works, we're ready to get started!

Visit thonny.org to download Thonny. Simply follow the installation instructions for your operating system.

Installing MicroPython (Optionally)

thonny2.png

If you haven't used your board with MicroPython before, you'll need to install it. To do this, press and hold the BOOT button on your RP2040-Matrix board before connecting it to your computer.

Once it's plugged in, release the BOOT button and open Thonny. Navigate to the 'Tools → Options' menu. In the 'Interpreter' tab, select the appropriate port and click on the 'Install or update MicroPython' link.

Uploading the Code

view.jpg

Unplug your board and then plug it back in. Create a new file and paste the provided MicroPython code. Press the ▶️ 'Run' button and enjoy the animation!

import time
from machine import Pin
from neopixel import NeoPixel

# Attach 25 Neopixel LEDs to GPIO 16
np = NeoPixel(Pin(16), 25, bpp = 3)
rtc = machine.RTC()
rtc.datetime((2025, 8, 3, 21, 35, 32, 0, 0)) ##(year, month, day, hour, minute, second, microsecond, tzinfo)

def int_to_ternary(n):
"""Convert an integer to its ternary representation."""
if n == 0:
return "0"
ternary = ""
while n > 0:
ternary = str(n % 3) + ternary # Get the remainder and prepend it to the result
n //= 3 # Divide n by 3
return ternary

def setPixel(x,y,r,g,b,write=True):
"""Set pixel color at x,y to r,g,b. If write==True, flush the buffer"""
np[5 * x + y] = (r,g,b)
if(write):
np.write()

def displayTernaryNumber(number, col):
"""Display single number as a column of matrix"""
number = int_to_ternary(number)
row = 4
for digit in number:
if digit=="0":
setPixel(row,col,20,0,0,False)
elif digit=="1":
setPixel(row,col,0,20,0,False)
elif digit=="2":
setPixel(row,col,0,0,20,False)
row-=1

#Clear the matrix
np.fill((0,0,0))
np.write()

#Main loop
while(True):
current_time=rtc.datetime()
print(f"{current_time[0]}-{current_time[1]}-{current_time[2]} {current_time[4]}:{current_time[5]}:{current_time[6]} \r", end='')
np.fill((0,0,0))
displayTernaryNumber(current_time[6],4)#seconds
displayTernaryNumber(current_time[5],3)#minutes
displayTernaryNumber(current_time[4],2)#hours
displayTernaryNumber(current_time[2],1)#day
displayTernaryNumber(current_time[1],0)#month
np.write()
time.sleep(0.5)


💡 If the onboard files do not appear in the 'Files' tab, try pressing the 🛑 'Stop' button.
💡 To have the code start automatically, make sure to name the file main.py.