Bringing the bling 🌟 with MicroPython 🐍

Andy Piper - Feb 3 '22 - - Dev Community

It all began, as many things do, with a Tweet. @GeekMomProjects (aka Debra) posted this, late one evening in my timezone.

(sidenote, she has a really good blog and Twitter feed, especially if you are into hardware projects)...

Let's break this down. This is an Espressif ESP32-C3FH4 board with USB-C, covered in 25 multicoloured LEDs (WS2812b / NeoPixels) in a 5x5 grid. The ESP32-C3FH4 is equipped with wifi and Bluetooth LE, 400Kb of SRAM and 4Mb of Flash memory. All in all, it is almost exactly 2cm square. Basically, it's a very capable little microcontroller board, for about $10.

I had to have one of these, for no good reason, apart from...

πŸŽ† ✨ BLINKY LIGHTS!!! ✨ πŸŽ†

(quite a few other folks in the community also saw the original Tweet, and jumped in on the excitement!)

Getting started

The board arrived within about 10 days (if you order right now delivery is likely to vary, with the Lunar New Year). Plugging it in to a USB port immediately resulted in a fun and colourful cycling light display.

The manufacturer's example software for the board is on GitHub, and works absolutely fine with the Arduino IDE. I'm using the new Arduino IDE 2.0. One thing to note here is that you also need the Espressif development release core in order to build for the ESP32-C3 board - these are relatively new within the Espressif family of chips.

That repo is fine, and folks quickly got building things in the Arduino environment - see this excellent Wordle example from @ciro... with Twitter API, for bonus points!

MicroPython, ESP32-C3, and NeoPixels

We quickly tried the MicroPython port for ESP32-C3 with USB, but both @GeekMomProjects and I were finding a few issues - in my case, no matter what I tried, as soon as I did anything with the NeoPixels, the first one in the group lit up in bright 🟩 green, and could not be switched off, unless I power cycled the board.

I was able to make a start, though.

To flash MicroPython to the board, I needed to hold the button marked B (top left, looking at the back of the board with USB-C on the top edge) when connecting it to my machine. The blue light on the back of the board lit up, and a new USB device showed up. On my M1 Pro MacBook, depending on which side I plugged in to, it was either /dev/tty.usbmodem11301 or /dev/tty.usbmodem21301 (I resorted to ls -lrt /dev to figure this out). Then, I used esptool.py to wipe the board, and reflash with the MicroPython build.

Looking at the Arduino code, the NeoPixels are controlled via pin 8, and there are 25 of them; so in the REPL, this sequence let me initialize the board to talk to the NeoPixels.

import esp32
from machine import Pin
from neopixel import NeoPixel

pin = Pin(8, Pin.OUT)  # NP control on Pin 8
pixels = 25  # we have 25 pixels, set here to use for loops

np = NeoPixel(pin, pixels)
Enter fullscreen mode Exit fullscreen mode

As an aside here, I'm using the Thonny editor to do the editing, but you could use VS Code, mu, or an alternative of your choice. Thonny lets me access the REPL easily, but I could do the same using one of several terminal tools.

The next thing I wanted to do was to understand the board layout. How are the LEDs arranged? By lighting each one in sequence, it was easy to figure this out. Here's the answer.

np[0] = (0,255,0) # top left green
np.write()
np[2] = (0,255,0) # top middle green
np.write()
np[4] = (255,0,0) # top right red
np.write()
np[24] = (255,0,255) # bottom right magenta
np.write()
Enter fullscreen mode Exit fullscreen mode

If the board is aligned with the USB-C / power in along the top edge, the LEDs run from 0 (top left), left to right in rows, down to the last one 24 (bottom right). Here's a visual guide.

The board with an overlay showing the LEDs numbered from top left 0-24 in left-to-right sequence

Working with the MicroPython developers via Tweets and GitHub issues and commit comments, we eventually figured out the problem with the first pixel staying green. To fix this, I needed to add a specific call at the start of the MicroPython code:

esp32.RMT.bitstream_channel(0)
Enter fullscreen mode Exit fullscreen mode

The ESP32 C3 seems to require the RMT channel to be set this way (this is associated with a change in the implementation of accessing the NeoPixels in MicroPython). In my case, this code is currently working, on the most recent nightly builds of MicroPython.

Since the manufacturer's content is limited to a YouTube video, and the Arduino code, I've documented various experiments and findings on GitHub in a set of discovery notes.

In my FiveByFive repository you can find an example which will run a simple test - blinking each pixel in turn in a randomised RGB colour tuple, and then displaying a happy smiley face πŸ˜€

GitHub logo andypiper / fivebyfive

Polyglot examples for the 01Space ESP32-C3FH4-RGB board

"Five by Five"

Multilingual code for a 01Space 5x5 Neopixel RISC V board (ESP32-C3FH4 with USB-C) - sometimes called a "bedazzled" or "bejazzled" ESP32-C3!

fivebyfive

Background

It all started with this Tweet from @GeekMomProjects...

This repo started as a set of MicroPython experiments. The goal of this project now, is to aggregate information, and different examples, to enable the community to learn about and use the ESP32-C3FH4-RGB board.

Requirements

  • the board, available to purchase via Banggood
    • a USB-A to USB-C data cable will not work with USB-C to USB-C

Contents

  • arduino/ updated version of the original Arduino code for the board (may need to populate via git pull --recurse-submodules). This pulls in my fork of the upstream original project so that I can send updates as needed.
  • micropython/ MicroPython code examples, and board definition for building your own MicroPython firmware.
  • tinygo/ TinyGo code examples.

Each language subdirectory contains…




here's a super high-tech prototype of the smiley face pattern

a blue sticky note with a 5x5 grid showing squares shaded to make a face


Finally, a huge Thank You to Debra @GeekMomProjects (check out the cool necklace, and the 🌧️ raincloud), and also to Matt Trentini and especially Damien George from the MicroPython project, for the patience and support in figuring these boards out.

I've got more ideas in mind than a simple example like this, the board is capable of a lot more. I'm still learning the MicroPython tooling though (I've used CircuitPython before, but rarely MicroPython itself). Stay tuned for more experiments and discoveries, and let me know in the comments if you have any questions or suggestions!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .