Skip to content

circuitpython

Updating SongMatrix

One of my favorite CircuitPython projects I've done is SongMatrix. Using a microphone on a Raspberry Pi, it records the song playing in the background, uploads a sample to shazamio, uploads the song and artist to AdafruitIO via MQTT, and an S3 MatrixPortal listens for the MQTT update and displays it on an LED matrix.

I don't run it all the time, but I do like to use it when I'm listening to a new album to learn the song names. I know a lot of music and can sometimes tell you the name of the artist or the album within seconds of a song starting, but not so much the song title.

I'm using a 2.5mm pitch LED Matrix and I have not found a lot of 3D printed cases for either one or two LED matrices. That is, until a few months ago when I came across the Transit Tracker project from EastsideUrbanism.

It uses two LED matrices in a beautiful 3D printed cases that hold both of them and the S3 MatrixPortal and screws together. I printed it out months ago but couldn't get SongMatrix to work with 2 matrices.

In theory, I should be able to update the MatrixPortal library by changing: matrix = Matrix(width=64, height=32, bit_depth=3) to matrix = Matrix(width=128, height=32, bit_depth=3) But no joy. I started over with some simple test programs, and I could get a 128x32 matrix to work without a problem, but as soon as I tried in my original program it did not work.

Next I tried to replace the MatrixPortal library by using the pins directly. Success! But now it doesn't scroll across the matrix, it's just a static display. It turns out that using ScrollingLabel only scrolls when the character count is larger than the max_character when setting the text:

title_scroll = ScrollingLabel(
    terminalio.FONT,
    text=song_title_scroll,
    max_characters=20,
    color=0xff0000,
    animate_time=0.3
)

To get it scrolling, I just did a len on the string returned from AdafruitIO and added a string of spaces to get over the 20 character limit to scroll it.

But the weirdest part? The original program scrolled the text with less than 10 characters. I tried to recreate it with a basic ScrollingLabel example, and of course I couldn't get it to work. Don't believe me? You can see the original scrolling the band Metric which shouldn't be scrolling on the project's GitHub page.

Introducing SongMatrix

I listen to music. A lot of music. If I’m in my home office I’m usually listening to a record, and if not, the radio. But I’ve always been an album person, not into playlist (or mixtapes to date myself). I’ve found by listening to albums front to back, I don’t always learn the song names.

Just a few weeks ago, I came across the shazamio Python library. I have a Raspberry Pi already sitting on my desk. I also have a couple extra 64x32 RGB Matrices and I recently picked up one of the new Adafruit S3 MatrixPortals, so I have the hardware and software to start a new project.

Enter SongMatrix (GitHub).

SongMatrix records a short audio sample on the Raspberry Pi and then sends it to shazamio to be identified. It then sends a MQTT message to Adafruit IO’s MQTT broker with the song title and artist. The MQTT message is received by the S3 MatrixPortal, which then scrolls the song and artist on separate lines, like so:

A 32x64 Matrix displaying the song Breathing Underwater on the top row and the artist, Metric, on the bottom row

I whipped up a proof of concept for the Python part of recording audio and sending it to shazamioin one Friday evening. The CircuitPython part took me a couple weeks and I’ll share some of the challenges in upcoming blog posts (no promises).

A special thank you to todbot for bootstrapping some asyncio code to get me started. And to anecdata for spending a good chunk of yesterday helping me get around the last issue and getting to done. (Well, it’s never done).

MatrixPortal Album Art Display

A year and a half ago I made some progress on displaying album art on a MatrixPortal and 2 32x64 RGB matrices using CircuitPython. I was never really happy with the results and using two 32x64 matrices instead of one 64x64 matrix was difficult. I moved on and re-created the project using a PyPortal Titano. It worked well: when I chose an album I wanted to listen to SilverSaucer.com, my FastAPI web app would convert the image and send a MQTT message. The PyPortal would listen for the message, and when a new message arrived, download and display the album art along with a Winamp skin that also showed the artist and album name. I should have blogged it and taken a picture!

When Adafruit announced the new S3 MatrixPortal with so much memory - 8MB flash and 2 MB of SRAM, I decided to try again. My hope was that with that much memory, I could download and load the image into memory without having to save it.

I haven't figured out how to do it without saving the image to the MatrixPortal yet, but I was able to repurpose my original code and had it up and running in just a few minutes.

Now came the hard part: adding gamma correction to the image so it looks closer to normal on the MatrixPortal and not washed out. Adafruit has a great Learn Guide for Image Correction for RGB LED Matrices. The guide includes a CPython program that uses Python's PIL / Pillow library to manipulate the image and some logic to apply gamma correction.

I needed to re-create that program, which used command line arguments where you would pass the image name to the program, as one function within my FastAPI app. It took a few days of banging on it (and a few hours lost to a wrong indent(!)), but I got it. The get_discogs_image function downloads the given image and converts it from a 600px image to 320 x 320px (for the PyPortal) and a 64x64 image for the MatrixPortal. The process_image function then takes the 64x64 image and applies gamma correction and saves a new copy (which the MatrixPortal will download and display).

Some albums look a lot better than others. Considering it's a 64x64 image, it's practically pixel art at this point. It's too bad that it's difficult to photograph RGB matrices, but here is a picture showing Divine Fits' album, it being chosen on SilverSaucer.com, and the converted image being displayed on the MatrixPortal.

Divine Fits

Next CircuitPython Projects Up

I couldn’t resist the recent holiday sales and I bought a few parts for my next two CircuitPython projects.

First up, is re-creating my Pi-Dial project using CircuitPython on a microcontroller instead of a Raspberry Pi and Python. This project controls Zone 2 of my home theater receiver, which is my home office.

Parts used include: * ESP32-S3 microcontroller (because it has WiFI and I already had a couple on hand) * 4 key NeoKey (used to change the inputs, such as CD, Phono, or Tuner and mute) * 1 Rotary encoder (to change the volume) * 1 2” TFT screen

I already had a prototype working with the NeoKey and rotary encoder using the ESP32-S3. CircuitPython can send serial commands to my Denon receiver and I can do all of the functions I need, including changing the volume, inputs, and muting or unmuting the receiver.

For an enclosure, I’m going to use repurpose the WalkMP3rson project from Adafruit and 3D print a retro Walkman style player. I’ll need to modify it to permanently add the USB-C cable for power as it won’t be battery operated.

The biggest thing I have to learn is displayio. I don’t have any experience using displays in CircuitPython, but I’m excited because the WalkMP3rson project has a bunch of code I’ll be able to re-use.

Work in progress on the breadboard

My second project will be an IoT sensor to detect when I’m running low on water softener salt. I purchased a Time of Flight sensor that can measure the distance and connects to an ESP32-S2 Feather. It will then report back via MQTT the current measurement. I’m already running Home Assistant and I’ll set it up to report the measurement daily to my dashboard. I’ve used MQTT in one other project, but I’ve never integrated with Home Assistant. The biggest challenge so far is finding a 3D printed case that can hold both the Feather and a battery.

Those should keep me busy for a while. At least over holiday break!

Adafruit Macropad Awesome List

Inspired by the CircuitPython Awesome List, I've created an Awesome List for the Adafruit Macropad.

I finally found some time to set mine up after I received mine this past summer in the Adafruit AdaBox subscription. I've 3D printed a couple of cases for it already and also programmed mine to work with the Onshape CAD software.

It's pretty cool and I was curious to see what others had programmed for macros for their applications and I figured why not create a list for everyone!

If you have any suggestions for what to add, please let me know or feel free to fork and send a pull request. Contributing guidelines are in the list and if you have any questions, you can find me on Twitter or on the Adafruit Discord (link in the list).