This project will pull the current local time from and then use it to calculate the percentage complete for the year.

A separate file on your MagTag,, is used to store the information you need to access WiFi and Adafruit IO. You will need a free account Adafruit IO account, your WiFi access information, and your location (for localized timezone management).

Steps to set up your file:

1. See the top part of this page on the MagTag for how to add the WiFi and timezone parts of the file.

2. Set up an Adafruit IO account, if you do not have one. It's free and not complex.

3. Update the file fields aio_username and aio_key with the values from your Adafruit IO account. Your file should look similar to this:

# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

secrets = {
    'ssid' : 'your-ssid-here',
    'password' : 'your-password-here',
    'aio_username' : "your-aio-username-here",
    'aio_key' : 'your-aio-key-here',
    'location' : 'New York, US'

Deep Sleep

This project uses the new deep sleep API to save power so it can run longer on a battery before needing a recharge. See this guide: to learn more!

Installing Project Code

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update with the example script.

Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the file in a zip file. Extract the contents of the zip file, open the directory MagTag_Progress_Displays/year_progress_percent/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

# SPDX-FileCopyrightText: 2020 Tim C, written for Adafruit Industries
# SPDX-License-Identifier: Unlicense
Show the progress through the year with text and a progress bar.

Copy epilogue18.bdf into fonts/ on your CIRCUITPY drive.

from adafruit_magtag.magtag import MagTag
from adafruit_progressbar.progressbar import ProgressBar
import rtc

def days_in_year(date_obj):
    # check for leap year
    if (date_obj.tm_year % 100 != 0 or date_obj.tm_year % 400 == 0) and date_obj.tm_year % 4 == 0:
        return 366
    return 365

magtag = MagTag()

        ( // 2) - 1,
    text_anchor_point=(0.5, 0.5),
magtag.set_text("Year Progress:", auto_refresh=False)

        ( // 2) - 1,
    text_anchor_point=(0.5, 0.5),

# set progress bar width and height relative to board's display
BAR_WIDTH = - 80

BAR_X = // 2 - BAR_WIDTH // 2
BAR_Y = 80

# Create a new progress_bar object at (x, y)
progress_bar = ProgressBar(
    BAR_X, BAR_Y, BAR_WIDTH, BAR_HEIGHT, 1.0, bar_color=0x999999, outline_color=0x000000

    now = rtc.RTC().datetime
    progress_bar.progress = now.tm_yday / days_in_year(now)
        "{:.2f}%".format(now.tm_yday / days_in_year(now) * 100.0), index=1

    magtag.exit_and_deep_sleep(24 * 60 * 60)  # one day

except (ValueError, RuntimeError, ConnectionError, OSError) as e:
    print("Some error occurred, retrying after 1 minute! -", e)
    magtag.exit_and_deep_sleep(60)  # one  minute

Code Overview

This code uses the adafruit_magtag library to fetch the current time, and to show the text output on the display. The library creates its own displayio.Group internally that is shown on the display. Add the progress bar to that Group like this

You can use to fetch the current time from Adafruit IO. Then you can access it with rtc.RTC().datetime. Use its field tm_yday, along with the total days in the year (accounting for leap days every 4 years) to divide out the year's progress. Set the progress on the progress bar before calling magtag.set_text() which is nice because it will handle the screen refresh.

To put the MagTag to sleep we use the function magtag.exit_and_deep_sleep()passing in the amount of seconds that it should sleep. This script sleeps for one day if everything works successfully, and one minute if anything failed so that it will try again shortly.

This guide was first published on Dec 02, 2020. It was last updated on Nov 28, 2020.

This page (Year Progress) was last updated on Oct 03, 2023.

Text editor powered by tinymce.