https://www.hardill.me.uk/wordpress/2021/09/18/the-linear-clock-ticks-again/ Skip to content Ben's Place Somewhere to keep notes on my projects Menu and widgets * Android Apps * About + Curriculum Vitae Search for: [ ] [Search] Recent Posts * The Linear Clock Ticks Again * Router swap * King Alfred's Way * IKEA VINDRIKTNING PM2.5 Sensor * Quick and Dirty Finger Daemon Recent Comments * The Linear Clock Ticks Again - Ben's Place on Back to Building the Linear Clock * The Linear Clock Ticks Again - Ben's Place on Lightbar clock * Mercedes Pinder on 64bit Raspberry Pi OS build with USB-C Ethernet enabled * hardillb on 64bit Raspberry Pi OS build with USB-C Ethernet enabled * Mercedes Pinder on 64bit Raspberry Pi OS build with USB-C Ethernet enabled Archives * September 2021 * July 2021 * May 2021 * April 2021 * March 2021 * February 2021 * January 2021 * December 2020 * October 2020 * September 2020 * August 2020 * July 2020 * June 2020 * May 2020 * April 2020 * March 2020 * February 2020 * December 2019 * November 2019 * October 2019 * September 2019 * August 2019 * July 2019 * June 2019 * May 2019 * April 2019 * March 2019 * January 2019 * November 2018 * October 2018 * September 2018 * June 2018 * April 2018 * March 2018 * December 2017 * November 2017 * October 2017 * September 2017 * August 2017 * July 2017 * May 2017 * April 2017 * March 2017 * February 2017 * January 2017 * November 2016 * October 2016 * September 2016 * June 2016 * April 2016 * March 2016 * February 2016 * January 2016 * December 2015 * November 2015 * October 2015 * August 2015 * July 2015 * May 2015 * March 2015 * February 2015 * January 2015 * December 2014 * November 2014 * September 2014 * August 2014 * July 2014 * May 2014 * April 2014 * March 2014 * December 2013 * November 2013 * October 2013 * September 2013 * June 2013 * March 2013 * February 2013 * December 2012 * November 2012 * October 2012 * September 2012 * August 2012 * July 2012 * June 2012 * May 2012 * February 2012 * January 2012 * December 2011 * November 2011 * October 2011 * September 2011 * August 2011 * July 2011 * June 2011 * May 2011 * April 2011 * January 2011 * December 2010 * October 2010 * September 2010 * July 2010 * May 2010 * April 2010 * February 2010 Categories * Android * Clock * Cycling * Exercise * Food * Hacks * MythTV * Node-RED * Projects * SMS/MMS * Tech * Uncategorised * Work Meta * Log in * Entries feed * Comments feed * WordPress.org [Close and accept] Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. To find out more, including how to control cookies, see here: Cookie Policy The Linear Clock Ticks Again I've had a background project ticking over slowly in the background for a number of years. Last year I designed and had built a number of PCBs to be used as HATs for a Raspberry Pi Zero. They included a RTC and a terminal block to attach the LED strip. I did say that I would write another post when the boards where delivered and I had assembled the first prototype. Unfortunately I had made a small, but critical mistake when designing the boards, I slightly messed up the package package size for the RTC so it wasn't possible to get assemble the boards correctly. I didn't get round to re-doing the PCB layout with the correct sized parts so the whole thing just sat for a while. In the meantime the Raspberry Pi Foundation went and released a new product, the Raspberry Pi Pico, which is based on the RP2040 chip. As well as the Pico they are also making the RP2040 chip available to other folk to include it directly in their own projects. Pimoroni have created a number of different boards but their latest is the Plasma 2040 which is specifically designed to drive LED strips. B.O.M. * Pimoroni Plasma 2040 * Pimoroni LED strip cable adapter * Pimoroni RV3028 Real-Time Clock Breakout * Flexible RGB 60 LED per meter strip * USB-C power supply * USB-C cable Assembly * Solder the RTC on to the breakout section of the Plasma 2040, the terminals are labelled so just make sure you match up the pins, I used the headers that came with the RTC and arranged it so the breakout was over the top of the Plasma2040 * Loosen the screw terminals for the connections marked 5V, DA and -. Insert the Red wire of the adapter in the 5V, Green wire in DA and White wire in - * Clip the LED strip to the end of the adapter. Plasma 2040Plasma 2040 Code When you first attach the Plasma2040 to your computer it will show up as a USB flash drive. This is so you can install the runtime. In this case we'll be using the Pimoroni Micropython build that comes with support for the board. You can grab a version from the release page on GitHub here. Once downloaded copy it into the root of the drive. When the copy has finished the board will reboot and be ready to run Python code. You can use the Thonny IDE to both write and push code to the device. You will need at least version 3.3.3 to support the Plasma2040. The fist version of the code was as follows: import plasma from plasma import plasma2040 from pimoroni import RGBLED, Button import time NUM_LEDS = 60 LOW = 32 MED = 64 HIGH = 128 BRIGHTNESS = [LOW,MED,HIGH] BRIGHTNESS_LEVEL = 0 button_brightness = Button(plasma2040.BUTTON_A) led = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B) led.set_rgb(0, 0, 0) led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT) led_strip.start() while True: RED = [0]*NUM_LEDS GREEN = [0]*NUM_LEDS BLUE = [0]*NUM_LEDS t = time.localtime() hour = (t[3] % 12) * 5 #Hours RED[hour] = BRIGHTNESS[BRIGHTNESS_LEVEL] RED[hour + 1] = BRIGHTNESS[BRIGHTNESS_LEVEL] RED[hour + 2] = BRIGHTNESS[BRIGHTNESS_LEVEL] RED[hour + 3] = BRIGHTNESS[BRIGHTNESS_LEVEL] RED[hour + 4] = BRIGHTNESS[BRIGHTNESS_LEVEL] #Mins GREEN[t[4]] = BRIGHTNESS[BRIGHTNESS_LEVEL] #Secs BLUE[t[5]] = BRIGHTNESS[BRIGHTNESS_LEVEL] #set the LEDS for i in range (NUM_LEDS): led_strip.set_rgb(i, RED[i], GREEN[i], BLUE[i]) #change brightness if button_brightness.read(): BRIGHTNESS_LEVEL += 1 BRIGHTNESS_LEVEL %= 3 time.sleep(1) This works well when triggered from Thonny as it syncs the laptop's time to the RP2040 each time it connects. But when the clock is powered by a USB power supply or a battery, the clock starts at 00:00:01 Jan 1st 2021 and has no way to be updated to match now. This is why we need the RTC module, it keeps track of the time while the clock is powered down. It also has a way to change the brightness, by pressing the A button it will cycle through 3 different brightness levels. Setting the RTC Time With a little bit of playing I worked out how to sync the RTC to the current time in the Thonny console >>> from pimoroni_i2c import PimoroniI2C >>> from breakout_rtc import BreakoutRTC >>> import time >>> PINS_PLASMA = {"sda": 20, "scl": 21} >>> i2c = PimoroniI2C(**PINS_PLASMA) >>> rtc = BreakoutRTC(i2c) >>> rtc.set_unix(time.time()) >>> rtc.set_time(54,18,17,6,18,9,2021) True >>> rtc.update_time() True >>> print(rtc.string_time()) 17:18:54 >>> rtc.set_backup_switchover_mode(3) The most important line is the last one, which enables the battery backup for the RTC so it remembers the time you just set. I was going to use the rtc.set_unix() function and pass in time.time () but it appears that the unix timestamp is maintained independently of the "Real" time on the RTC. The set_time() function takes values in the order * seconds (0-60) * minutes (0-60) * hours (0-23) * day of the week (1-7 -> mon-sun) * day of month (1-31) * monthe (1-12) * year (2000-2099) With the RTC set correctly a small update to the code to read from the RTC rather than from the time object and we are good to go. import plasma from plasma import plasma2040 from pimoroni import RGBLED, Button from pimoroni_i2c import PimoroniI2C from breakout_rtc import BreakoutRTC import time PINS_PLASMA = {"sda": 20, "scl": 21} i2c = PimoroniI2C(**PINS_PLASMA) rtc = BreakoutRTC(i2c) if rtc.is_12_hour(): rtc.set_24_hour() if rtc.update_time(): print(rtc.string_time()) print(rtc.string_date()) NUM_LEDS = 60 LOW = 32 MED = 64 HIGH = 128 BRIGHTNESS = [LOW,MED,HIGH] BRIGHTNESS_LEVEL = 0 button_brightness = Button(plasma2040.BUTTON_A) led = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B) led.set_rgb(0, 0, 0) led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT) led_strip.start() rtc.enable_periodic_update_interrupt(True) while True: RED = [0]*NUM_LEDS GREEN = [0]*NUM_LEDS BLUE = [0]*NUM_LEDS t = time.localtime() if rtc.read_periodic_update_interrupt_flag(): rtc.clear_periodic_update_interrupt_flag() rtc.update_time() hour = (rtc.get_hours() % 12) * 5 RED[hour] = BRIGHTNESS[BRIGHTNESS_LEVEL] RED[hour + 1] = BRIGHTNESS[BRIGHTNESS_LEVEL] RED[hour + 2] = BRIGHTNESS[BRIGHTNESS_LEVEL] RED[hour + 3] = BRIGHTNESS[BRIGHTNESS_LEVEL] RED[hour + 4] = BRIGHTNESS[BRIGHTNESS_LEVEL] GREEN[rtc.get_minutes()] = BRIGHTNESS[BRIGHTNESS_LEVEL] BLUE[rtc.get_seconds()] = BRIGHTNESS[BRIGHTNESS_LEVEL] for i in range (NUM_LEDS): led_strip.set_rgb(i, RED[i], GREEN[i], BLUE[i]) if button_brightness.read(): BRIGHTNESS_LEVEL += 1 BRIGHTNESS_LEVEL %= 3 time.sleep(1) 2021 Edition Next Steps There are a few things that need doing next. The first is to build a case for the clock, I'm thinking about something made up of layers of thin plywood with a channel for the LED strip and maybe a layer of smoked/mat acrylic to act as a diffuser. The second part is to work out a way to work with DST, Micropython doesn't support timezones as the database needed to keep track of all the different timezones takes up a huge amount of space. I could hard code in the dates for my location, but I'll probably just make use of the B button to toggle an hours difference on/off. Optionally I might add another 31 LED strip (probably at 30/meter) to be used as a calendar showing the current month with markers for weekends and the current day. Another option is to use 4 of these to build a 60 LED ring for something a little more conventionally shaped. And the final extra hack is to daisy chain the Light level sensor (e.g. one of these) on top of the RTC and dynamically adjust the brightness based on ambient light levels. I'll also probably keep tinkering with the Raspberry Pi Zero W version as that will allow oAuth to link to things like Google Calendar to show meetings in the clock view and add Holidays to the Calendar view. It will also have access to the full timezone database and NTP for time syncing over the network. Share this: * Click to print (Opens in new window) * Click to email this to a friend (Opens in new window) * Click to share on Twitter (Opens in new window) * Click to share on Reddit (Opens in new window) * Click to share on Pinterest (Opens in new window) * More * * Click to share on Facebook (Opens in new window) * Click to share on LinkedIn (Opens in new window) * * Related Posted on 18th September 202118th September 2021Author Ben Hardill Categories Clock, Hacks, Projects, TechTags clock, led, rpi2040 Leave a Reply Cancel reply Your email address will not be published. Required fields are marked * [ ] [ ] [ ] [ ] [ ] [ ] [ ] Comment [ ] Name * [ ] Email * [ ] Website [ ] [ ] Notify me of follow-up comments by email. [ ] Notify me of new posts by email. [Post Comment] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] This site uses Akismet to reduce spam. Learn how your comment data is processed. Post navigation Previous Previous post: Router swap Privacy Policy Proudly powered by WordPress Loading Comments... Write a Comment... [ ] Email (Required) [ ] Name (Required) [ ] Website [ ] [Post Comment] Send to Email Address [ ] Your Name [ ] Your Email Address [ ] [ ] loading [Send Email] Cancel Post was not sent - check your email addresses! Email check failed, please try again Sorry, your blog cannot share posts by email.