[{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eMake a highly accurate Circuit Python Matrix Panel clock quick and easy with a Matrix Portal S3 microcontroller and a 64x32 Matrix Panel.\u003c/p\u003e\n\u003cp\u003eSkill Level: Beginner\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/001/541/original/vlcsnap-2025-01-21-03h52m37s970.png?1737449586","metadata":{"caption":""}},{"element_type":"text","content":"\n  \n        \u003ch2\u003ePowering the Matrix Portal S3\u003c/h2\u003e\n\u003cp\u003eYou can power 1 panel with the the Matrix Portal S3 and included power cable.\u0026nbsp;\u003c/p\u003e\n      \n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/001/555/original/IMG_0155.JPG?1737879156","metadata":{"caption":""}},{"element_type":"text","content":"\n        \u003cp\u003eIf you want to add additional panels you will need to provide an external power supply such as a\u0026nbsp;\u003ca href=\"https://www.amazon.com/gp/product/B018TEAPRQ\" target=\"_blank\"\u003eMeanwell 5V 18Amp power supply\u003c/a\u003e. Each panel requires approximately 4 Amps of power.\u0026nbsp; There are many power supplies to choose from. It must be a 5V power supply but how many watts and amps will depend on how many panels you want to add. The power supply choice becomes simple if you allot 4 Amps per panel. I have a playground note on a \u003ca href=\"https://adafruit-playground.com/u/DJDevon3/pages/12-panel-matrix-portal-display\" target=\"_blank\"\u003e12-panel Matrix Portal S3 project\u003c/a\u003e.\u0026nbsp; If you want to add more panels that article is a very good resource.\u003c/p\u003e\n      ","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eInstalling Circuit Python\u003c/h2\u003e\n\u003cp\u003eFirst and foremost you'll want to install Circuit Python or update it.\u0026nbsp; You can find the \u003ca href=\"https://circuitpython.org/board/adafruit_matrixportal_s3/\" target=\"_blank\"\u003elatest UF2 for the Matrix Portal S3 here\u003c/a\u003e. You must use Google Chrome if you want to use the online web installer/updater.\u0026nbsp; Google Chrome is the only browser to support Web Serial. Firefox does not support it.\u003c/p\u003e\n\u003ch2\u003eGet Updated Library Bundle\u003c/h2\u003e\n\u003cp\u003eNow lets grab some necessary libraries for this project and transfer them to the /lib directory on your CIRCUITPY drive.\u0026nbsp; All libraries can be found in the \u003ca href=\"https://circuitpython.org/libraries\" target=\"_blank\"\u003eCircuit Python Library Bundle\u003c/a\u003e. The bundle is updated daily so when starting on a new project it's a good idea to get the latest bundle.\u0026nbsp; Libraries can be either a folder (containing multiple helper .mpy files) or a single .mpy file.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"alert","content":"\u003cp\u003e.mpy files are not editable, they are more efficient compressed files only for using on your device. If you ever have a need to edit a library file you should get the PY Bundle.  .py files are editable, .mpy files are not.\u003c/p\u003e","metadata":{"class":"element alert-element build-alert alert-info","markdown":".mpy files are not editable, they are more efficient compressed files only for using on your device. If you ever have a need to edit a library file you should get the PY Bundle.  .py files are editable, .mpy files are not.","alert_type":"info"}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eUsing Libraries\u003c/h2\u003e\n\u003cp\u003eDownload and unzip the bundle to your desktop. Then drag and drop libraries in the /lib directory to the /lib directory on your CIRCUITPY USB drive. Only transfer libraries that you will be actively using to save storage space on your CIRCUITPY drive.\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eYou use the libraries in your code.py script with `import`.\u0026nbsp; We will be turning the Matrix Panel into a 64x32 pixel display.\u0026nbsp; Displayio is the graphical library for Circuit Python. It is powerful and allows for layering of images and text.\u0026nbsp; Displayio will treat it almost the same as a 64x32 TFT display.\u0026nbsp; With any displayio project the first thing you want to do is set\u003c/p\u003e\n\u003cpre class=\"prettyprint language-python hljs\"\u003edisplayio.release_displays()\u003c/pre\u003e\n\u003cp\u003eThis re-initializes (wipes \u0026amp; makes ready) the display on every reload of the entire script. A common mistake is forgetting to put that at the top of the script and the display never reloads properly.\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eThen we setup the Wifi socket pool, connection manager, and adafruit_requests. All scripts that have an online feature to them in one way or another will always require adafruit_requests regardless of the protocol, including NTP.\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eThe libraries you'll need from the bundle are:\u003c/p\u003e\n\u003cul\u003e\n\u003cli\u003eadafruit_bitmap_font\u003c/li\u003e\n\u003cli\u003eadafruit_bus_device\u003c/li\u003e\n\u003cli\u003eadafruit_display_text\u003c/li\u003e\n\u003cli\u003eadafruit_connection_manager\u003c/li\u003e\n\u003cli\u003eadafruit_ntp\u003c/li\u003e\n\u003cli\u003eadafruit_requests\u003c/li\u003e\n\u003cli\u003eneopixel\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp\u003eThe rest of the imports such as os, time, board, displayio, etc..\u0026nbsp; are modules that are built into Circuit Python itself and can be imported at any time.\u0026nbsp;\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"import os\nimport time\nimport board\nimport microcontroller\nimport displayio\nimport terminalio\nimport rgbmatrix\nimport framebufferio\nimport wifi\nimport adafruit_connection_manager\nimport adafruit_requests\nimport rtc\nimport adafruit_ntp\nfrom adafruit_display_text import label\nfrom adafruit_bitmap_font import bitmap_font\ndisplayio.release_displays()\n\n# Initalize Wifi, Socket Pool, Request Session\npool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)\nrequests = adafruit_requests.Session(pool)","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch3\u003eRGBMatrix \u0026amp; Framebufferio\u003c/h3\u003e\n\u003cp\u003e\u003cstrong\u003eWe will not be using the MatrixPortal or Portalbase helper libraries here\u003c/strong\u003e. Instead we'll make the script more efficient by using only the libraries necessary for turning a Matrix Panel into a display.\u0026nbsp; If you have a Matrix Portal S3 please realize that the MatrixPortal \u0026amp; Portalbase libraries are not necessary to do basic tasks such as getting the time from an NTP server. In fact, those helper libraries rely on RGBMatrix, Framebufferio, Adafruit_NTP, and Adafruit_Requests. We're simply cutting out an unnecessary middle man from the equation.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eEnsure you fill out settings.toml with your wifi credentials.\u0026nbsp; This project requires the Matrix Portal S3 to go online for NTP updates.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"# Use settings.toml for credentials\nssid = os.getenv(\"CIRCUITPY_WIFI_SSID\")\npassword = os.getenv(\"CIRCUITPY_WIFI_PASSWORD\")","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eCustom Configuration Variables\u003c/h2\u003e\n\u003cp\u003eNext is setting some constants (or variables) to make life a bit easier.\u0026nbsp; It's a good idea to put all configuration constants and variables at the top of your script gathered in one place to make finding them easier when you're coding.\u003c/p\u003e\n\u003cp\u003eBit Depth is how many colors you want available.\u0026nbsp; For a clock with no background you only need a bit depth of 1 or 2. If you intend to add BMP images then you might want 4,5, or 6 bit color.\u003c/p\u003e\n\u003cp\u003eYou will see pictures later in this playground note that shows the difference between 12 hour and 24 hour time format.\u0026nbsp; Simply edit the variable to 12 or 24 here to change the format.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"TZ_OFFSET = -5  # time zone offset in hours from UTC\nTime_Format = \"24\"  # 12 hour (AM/PM) or 24 hour (military) clock\nsleep_time = 60  # NTP poll time interval in seconds\n\nDISPLAY_WIDTH = 64\nDISPLAY_HEIGHT = 32\nDISPLAY_ROTATION = 0\nBIT_DEPTH = 4\nAUTO_REFRESH = True\nDEBUG_TIME = False  # For manually testing times","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eInstantiating the Matrix\u0026nbsp;\u003c/h2\u003e\n\u003cp\u003eThese settings are standard for a 64x32 panel.\u0026nbsp; If you're using a 64x64 panel you might have to modify it to add the additional Address E pin.\u003c/p\u003e\n\u003cpre class=\"prettyprint language-python hljs\"\u003ematrix = rgbmatrix.RGBMatrix(\n    width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, bit_depth=BIT_DEPTH,\n    rgb_pins=[\n        board.MTX_R1,\n        board.MTX_G1,\n        board.MTX_B1,\n        board.MTX_R2,\n        board.MTX_G2,\n        board.MTX_B2],\n    addr_pins=[board.MTX_ADDRA, board.MTX_ADDRB, board.MTX_ADDRC, board.MTX_ADDRD],\n    clock_pin=board.MTX_CLK,\n    latch_pin=board.MTX_LAT,\n    output_enable_pin=board.MTX_OE,\n    doublebuffer=\u003cspan class=\"hljs-literal\"\u003eTrue\u003c/span\u003e)\u003c/pre\u003e\n\u003cp\u003eNext is where some magic happens as the Matrix Panel is associated with a displayio object using framebufferio. The frame buffer becomes the display, pixel colors are run as a buffer stream. You don't need to know how it works only that the matrix panel can now be used by displayio.\u0026nbsp;\u003c/p\u003e\n\u003cpre class=\"prettyprint language-python hljs\"\u003edisplay = framebufferio.FramebufferDisplay(matrix, auto_refresh=AUTO_REFRESH, rotation=DISPLAY_ROTATION)\u003c/pre\u003e\n\u003cp\u003e\u0026nbsp;\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eNTP \u0026amp; RTC\u003c/h2\u003e\n\u003cp\u003eNow we setup the NTP request.\u0026nbsp; NTP is an internet based time server.\u0026nbsp; All an NTP server does is respond to a request with the current time. The actual NTP server IP/URL and functions are pulled from the adafruit_ntp library. You can use a different NTP server but that is not covered in this playground note.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"# Publicly Open NTP Time Server\n# No AdafruitIO credentials required\nntp = adafruit_ntp.NTP(pool, tz_offset=TZ_OFFSET)","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eTimestamp Formatting\u003c/h2\u003e\n\u003cp\u003eThese are custom helper functions I've written to make time conversion and label formatting easier.\u0026nbsp;\u003c/p\u003e\n\u003cp\u003eTime formatting uses Struct Time. You can read more about \u003ca href=\"https://docs.circuitpython.org/en/latest/shared-bindings/time/index.html#time.struct_time\" target=\"_blank\"\u003eCircuit Python struct time formatting here\u003c/a\u003e. You can easily customize how you want date or time displayed using struct time formatting.\u0026nbsp;\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/001/539/original/Standard_Time.png?1737341012","metadata":{"caption":"12 Hour Time Format"}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eIgnore the phantom green lettering. It's due to a reflection on my camera lens.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/001/540/original/Military_Time.png?1737341046","metadata":{"caption":"24 Hour Time Format"}},{"element_type":"code","content":"def time_calc(input_time):\n    \"\"\"Converts seconds to minutes/hours/days\"\"\"\n    if input_time \u003c 60:\n        return f\"{input_time:.0f} seconds\"\n    if input_time \u003c 3600:\n        return f\"{input_time / 60:.0f} minutes\"\n    if input_time \u003c 86400:\n        return f\"{input_time / 60 / 60:.1f} hours\"\n    return f\"{input_time / 60 / 60 / 24:.2f} days\"\n\n# Custom timestamp functions use struct time format\n# You can easily change them to your preferred format\n# https://docs.circuitpython.org/en/latest/shared-bindings/time/index.html#time.struct_time\ndef _format_datetime(datetime):\n    \"\"\" F-String formatted struct time conversion\"\"\"\n    return (f\"{datetime.tm_mon:02}/\" +\n            f\"{datetime.tm_mday:02}/\" +\n            f\"{datetime.tm_year:02} \" +\n            f\"{datetime.tm_hour:02}:\" +\n            f\"{datetime.tm_min:02}:\" +\n            f\"{datetime.tm_sec:02}\")\n\ndef _format_min(datetime):\n    \"\"\" Get the current minute\"\"\"\n    return (f\"{datetime.tm_min:02}\")\n\ndef _format_sec(datetime):\n    \"\"\" Get the current second\"\"\"\n    return (f\"{datetime.tm_sec:02}\")\n    \ndef _format_time(datetime, format=\"12\"):\n    \"\"\" Time is 12 hour or 24 hour format\"\"\"\n    if format == \"12\":\n        hour = datetime.tm_hour % 12\n        min = datetime.tm_min\n        if hour == 0:\n            hour = 12\n        am_pm = \"AM\"\n        if datetime.tm_hour / 12 \u003e= 1:\n            am_pm = \"PM\"\n        if DEBUG_TIME:\n            # Set debug to True \u0026 change these to test different times\n            debug_hour = \"09\"\n            debug_min = \"09\"\n            return (f\"{debug_hour:01}:{debug_min:02} {am_pm}\")\n        else:\n            return (f\"{hour:01}:{min:02} {am_pm}\")\n    if format == \"24\":\n        return (f\"{datetime.tm_hour:02}:{datetime.tm_min:02}:{datetime.tm_sec:02}\")","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eQuick Colors\u003c/h2\u003e\n\u003cp\u003eI always add a small list of commonly used colors to every display related script.\u0026nbsp; It makes life much easier than looking up hex color codes.\u0026nbsp; You can add more colors using hex color codes.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"# Quick Colors for Labels\nTEXT_BLACK = 0x000000\nTEXT_BLUE = 0x0000FF\nTEXT_CYAN = 0x00FFFF\nTEXT_GRAY = 0x8B8B8B\nTEXT_GREEN = 0x00FF00\nTEXT_LIGHTBLUE = 0x90C7FF\nTEXT_MAGENTA = 0xFF00FF\nTEXT_ORANGE = 0xFFA500\nTEXT_PURPLE = 0x800080\nTEXT_RED = 0xFF0000\nTEXT_TEAL = 0xB2D8D8\nTEXT_WHITE = 0xFFFFFF\nTEXT_YELLOW = 0xFFFF00","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eCustom Fonts, Labels, and Groups\u003c/h2\u003e\n\u003cp\u003eIf you want to use custom fonts\u0026nbsp; they must be in .bdf or .pcf format. Upload the file to your CIRCUITPY device and then specifying the path to the font.\u0026nbsp; I usually create a directory in my CIRCUITPY drive named \"fonts\".\u003c/p\u003e\n\u003cp\u003eLabels are how text information is displayed on the matrix panel. If you want to learn more about labels, updating labels, and label x/y placement please read \u003ca href=\"https://learn.adafruit.com/circuitpython-display_text-library/label-placement\" target=\"_blank\"\u003eLearn Guide: Circuit Python Text Library\u003c/a\u003e.\u003c/p\u003e\n\u003cp\u003eGroups are how you create layers upon layers of elements on a display.\u0026nbsp; If you want to learn more about groups \u0026amp; layers \u003ca href=\"https://learn.adafruit.com/circuitpython-display-support-using-displayio/group\" target=\"_blank\"\u003eLearn Guide: Circuit Python Display Support Using Displayio\u003c/a\u003e\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"# To use custom font uncomment below and change terminalio.FONT\n#font_IBMPlex = bitmap_font.load_font(\"/fonts/IBMPlexMono-Medium-24_jep.bdf\")\n\nclock_label = label.Label(terminalio.FONT)\nclock_label.anchor_point = (0.5, 0.5)\nclock_label.anchored_position = (DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2)\nclock_label.scale = 1\nclock_label.color = TEXT_ORANGE\n\n# Groups Setup\nmain_group = displayio.Group()\nmain_group.append(clock_label)\ndisplay.root_group = main_group","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eNIST Time\u003c/h2\u003e\n\u003cp\u003eWhile developing this project it took a while before I figured out that my PC's clock was actually wrong and a few seconds ahead of NIST.\u0026nbsp; If you compare the time on the Matrix Panel clock to \u003ca href=\"https://www.time.gov/\" target=\"_blank\"\u003eNIST time\u003c/a\u003e you'll see that it is extremely accurate and in my case more accurate than the time on my PC.\u003c/p\u003e\n\u003cp\u003e\u0026nbsp;\u003c/p\u003e\n      \n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/001/554/original/NIST_TIME.PNG?1737876864","metadata":{"caption":""}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n        \u003ch2\u003eInitial Time Synchronization\u003c/h2\u003e\n\u003cp\u003eWhen the program is first run it will retrieve the NTP time and set the RTC.\u0026nbsp; This happens only once and before the infinite loop. When the time appears on the Matrix Panel it should be the correct time to the second.\u003c/p\u003e\n      \n\n\n\n\n\n","metadata":{}},{"element_type":"code","content":"now = time.localtime()\ncurrent_datestamp = \"{}\".format(_format_datetime(now))\ntry:\n    rtc.RTC().datetime = ntp.datetime\n    print(f\"Initial Synchronize: {current_datestamp}\")\n    time.sleep(1)\nexcept OSError as e:\n    print(f\"RTC or NTP Error: {e}\")","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n        \u003ch2\u003eInfinite Loop\u003c/h2\u003e\n\u003cp\u003eLastly we create a while True: loop that runs anything inside of it forever in a sequential loop.\u003c/p\u003e\n\u003ch4\u003eClock Label\u003c/h4\u003e\n\u003cp\u003eThe time from NTP is updated on the display using clock_label.text.\u003c/p\u003e\n\u003ch4\u003eMicrocontroller Reset Failsafe\u003c/h4\u003e\n\u003cp\u003etime.monotonic will drift by a few seconds every day.\u0026nbsp; To prevent that I reset the board once every 24 hours. This can help improve the accuracy by resetting the board once a day.\u003c/p\u003e\n\u003ch4\u003eSynchronize RTC with NTP\u003c/h4\u003e\n\u003cp\u003eWe want to keep the RTC updated by polling the NTP occasionally. I have it programmed to synchronize with the NTP server every hour on the hour. This is expressed when the clock reaches 00 minutes and 00 seconds.\u0026nbsp;\u003cspan class=\"hljs-comment\"\u003e\u003c/span\u003e\u003c/p\u003e\n      \n\n\n\n\n","metadata":{}},{"element_type":"code","content":"print(\"===============================\")\nwhile True:\n    now = time.localtime()\n    if DEBUG_TIME:\n        print(f\"Now: {now}\")\n        time.sleep(0.5)\n    board_uptime = time.monotonic()\n    current_datestamp = \"{}\".format(_format_datetime(now))\n    current_time = \"{}\".format(_format_time(now, format=Time_Format))\n    current_min = \"{}\".format(_format_min(now))\n    current_sec = \"{}\".format(_format_sec(now))\n    clock_label.text = f\"{current_time}\"\n    if DEBUG_TIME:\n        print(f\"Monotonic: {time.monotonic()}\")\n        print(f\"Current Time: {current_time}\")\n        print(f\"Current Min:Sec: {current_min}:{current_sec}\")\n        print(\"Board Uptime: \", time_calc(board_uptime))\n        print(\"===============================\")\n\n    if board_uptime \u003e 86400:\n        print(\"24 Hour Uptime Restart\")\n        microcontroller.reset()\n\n    # Synchronize RTC from NTP every 1 hour on the hour\n    if current_min == \"00\" and current_sec == \"00\":\n        try:\n            rtc.RTC().datetime = ntp.datetime\n            print(f\"Synchronized Time: {current_datestamp}\")\n            time.sleep(1)\n        except OSError as e:\n            print(f\"RTC or NTP Error: {e}\")","metadata":{"language":"python","linenums":false}},{"element_type":"alert","content":"\u003cp\u003eThat's all there is to it. Hope you've learned a bit about how Circuit Python works with NTP, RTC, requests, and displays.  Here is the full script. \u003c/p\u003e","metadata":{"class":"element alert-element build-alert alert-info","markdown":"That's all there is to it. Hope you've learned a bit about how Circuit Python works with NTP, RTC, requests, and displays.  Here is the full script. ","alert_type":"info"}},{"element_type":"code","content":"# SPDX-FileCopyrightText: 2025 DJDevon3\n# SPDX-License-Identifier: MIT\n# Coded for Circuit Python 9.2.3\n\"\"\"Matrix Portal S3 NTP Clock\"\"\"\n\nimport os\nimport time\nimport board\nimport microcontroller\nimport displayio\nimport terminalio\nimport rgbmatrix\nimport framebufferio\nimport wifi\nimport adafruit_connection_manager\nimport adafruit_requests\nimport rtc\nimport adafruit_ntp\nfrom adafruit_display_text import label\nfrom adafruit_bitmap_font import bitmap_font\ndisplayio.release_displays()\n\n# Initalize Wifi, Connection Manager, Request Session\npool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)\nrequests = adafruit_requests.Session(pool)\n\n# Use settings.toml for credentials\nssid = os.getenv(\"CIRCUITPY_WIFI_SSID\")\npassword = os.getenv(\"CIRCUITPY_WIFI_PASSWORD\")\n\nDISPLAY_WIDTH = 64\nDISPLAY_HEIGHT = 32\nDISPLAY_ROTATION = 0\nBIT_DEPTH = 4\nAUTO_REFRESH = True\nDEBUG_TIME = False  # For manually testing times\nTZ_OFFSET = -5  # time zone offset in hours from UTC\nTime_Format = \"24\"  # 12 hour (AM/PM) or 24 hour (military) clock\n\n# Instantiate 64x32 Matrix Panel\nmatrix = rgbmatrix.RGBMatrix(\n    width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, bit_depth=BIT_DEPTH,\n    rgb_pins=[\n        board.MTX_R1,\n        board.MTX_G1,\n        board.MTX_B1,\n        board.MTX_R2,\n        board.MTX_G2,\n        board.MTX_B2],\n    addr_pins=[board.MTX_ADDRA, board.MTX_ADDRB, board.MTX_ADDRC, board.MTX_ADDRD],\n    clock_pin=board.MTX_CLK,\n    latch_pin=board.MTX_LAT,\n    output_enable_pin=board.MTX_OE,\n    doublebuffer=True)\n\n# Associate RGB matrix as a Display so we can use displayio\ndisplay = framebufferio.FramebufferDisplay(\n        matrix, auto_refresh=AUTO_REFRESH,\n        rotation=DISPLAY_ROTATION\n)\n\n# Publicly Open NTP Time Server\n# No AdafruitIO credentials required\nntp = adafruit_ntp.NTP(pool, tz_offset=TZ_OFFSET)\n\ndef time_calc(input_time):\n    \"\"\"Converts seconds to minutes/hours/days\"\"\"\n    if input_time \u003c 60:\n        return f\"{input_time:.0f} seconds\"\n    if input_time \u003c 3600:\n        return f\"{input_time / 60:.0f} minutes\"\n    if input_time \u003c 86400:\n        return f\"{input_time / 60 / 60:.1f} hours\"\n    return f\"{input_time / 60 / 60 / 24:.2f} days\"\n\n# Custom timestamp functions use struct time format\n# You can easily change them to your preferred format\n# https://docs.circuitpython.org/en/latest/shared-bindings/time/index.html#time.struct_time\ndef _format_datetime(datetime):\n    \"\"\" F-String formatted struct time conversion\"\"\"\n    return (f\"{datetime.tm_mon:02}/\" +\n            f\"{datetime.tm_mday:02}/\" +\n            f\"{datetime.tm_year:02} \" +\n            f\"{datetime.tm_hour:02}:\" +\n            f\"{datetime.tm_min:02}:\" +\n            f\"{datetime.tm_sec:02}\")\n\ndef _format_min(datetime):\n    \"\"\" Get the current minute\"\"\"\n    return (f\"{datetime.tm_min:02}\")\n\ndef _format_sec(datetime):\n    \"\"\" Get the current second\"\"\"\n    return (f\"{datetime.tm_sec:02}\")\n\ndef _format_time(datetime, format=\"12\"):\n    \"\"\" Time is 12 hour or 24 hour format\"\"\"\n    if format == \"12\":\n        hour = datetime.tm_hour % 12\n        min = datetime.tm_min\n        if hour == 0:\n            hour = 12\n        am_pm = \"AM\"\n        if datetime.tm_hour / 12 \u003e= 1:\n            am_pm = \"PM\"\n        if DEBUG_TIME:\n            # Set debug to True \u0026 change these to test different times\n            debug_hour = \"09\"\n            debug_min = \"09\"\n            return (f\"{debug_hour:01}:{debug_min:02} {am_pm}\")\n        else:\n            return (f\"{hour:01}:{min:02} {am_pm}\")\n    if format == \"24\":\n        return (f\"{datetime.tm_hour:02}:{datetime.tm_min:02}:{datetime.tm_sec:02}\")\n\n\n# Quick Colors for Labels\nTEXT_BLACK = 0x000000\nTEXT_BLUE = 0x0000FF\nTEXT_CYAN = 0x00FFFF\nTEXT_GRAY = 0x8B8B8B\nTEXT_GREEN = 0x00FF00\nTEXT_LIGHTBLUE = 0x90C7FF\nTEXT_MAGENTA = 0xFF00FF\nTEXT_ORANGE = 0xFFA500\nTEXT_PURPLE = 0x800080\nTEXT_RED = 0xFF0000\nTEXT_TEAL = 0xB2D8D8\nTEXT_WHITE = 0xFFFFFF\nTEXT_YELLOW = 0xFFFF00\n\n# To use custom font uncomment below and change terminalio.FONT\n# font_IBMPlex = bitmap_font.load_font(\"/fonts/IBMPlexMono-Medium-24_jep.bdf\")\n\nclock_label = label.Label(terminalio.FONT)\nclock_label.anchor_point = (0.5, 0.5)\nclock_label.anchored_position = (DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2)\nclock_label.scale = 1\nclock_label.color = TEXT_RED\n\n# Groups Setup\nmain_group = displayio.Group()\nmain_group.append(clock_label)\ndisplay.root_group = main_group\n\nnow = time.localtime()\ncurrent_datestamp = \"{}\".format(_format_datetime(now))\ntry:\n    rtc.RTC().datetime = ntp.datetime\n    print(f\"Initial Synchronize: {current_datestamp}\")\n    time.sleep(1)\nexcept OSError as e:\n    print(f\"RTC or NTP Error: {e}\")\n    \nprint(\"===============================\")\nwhile True:\n    now = time.localtime()\n    if DEBUG_TIME:\n        print(f\"Now: {now}\")\n        time.sleep(0.5)\n    board_uptime = time.monotonic()\n    current_datestamp = \"{}\".format(_format_datetime(now))\n    current_time = \"{}\".format(_format_time(now, format=Time_Format))\n    current_min = \"{}\".format(_format_min(now))\n    current_sec = \"{}\".format(_format_sec(now))\n    clock_label.text = f\"{current_time}\"\n    if DEBUG_TIME:\n        print(f\"Monotonic: {time.monotonic()}\")\n        print(f\"Current Time: {current_time}\")\n        print(f\"Current Min:Sec: {current_min}:{current_sec}\")\n        print(\"Board Uptime: \", time_calc(board_uptime))\n        print(\"===============================\")\n\n    if board_uptime \u003e 86400:\n        print(\"24 Hour Uptime Restart\")\n        microcontroller.reset()\n\n    # Synchronize RTC from NTP every 1 hour on the hour\n    if current_min == \"00\" and current_sec == \"00\":\n        try:\n            rtc.RTC().datetime = ntp.datetime\n            print(f\"Synchronized Time: {current_datestamp}\")\n            time.sleep(1)\n        except OSError as e:\n            print(f\"RTC or NTP Error: {e}\")","metadata":{"language":"python","linenums":false}},{"element_type":"text","content":"\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n        \u003cp\u003eFonts and code updates can be found in \u003ca href=\"https://github.com/DJDevon3/My_Circuit_Python_Projects/tree/main/Boards/espressif/Adafruit%20MatrixPortal%20S3/64x32%20RGB%20Matrix/Simple%20NTP%20Matrix%20Clock\" target=\"_blank\"\u003emy Github Repository\u003c/a\u003e.\u003c/p\u003e\n      \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","metadata":{}},{"element_type":"product","content":"https://www.adafruit.com/product/5778","metadata":{}},{"element_type":"product","content":"https://www.adafruit.com/product/2277","metadata":{}}]