Bringing the bling 🌟 with MicroPython 🐍
Tags: micropython, esp32, neopixels, programming
Published: 2022-02-03 14:29:21

It all began, as many things do, with a Tweet.  [@GeekMomProjects](https://twitter.com/geekmomprojects) (aka Debra) posted this, late one evening in my timezone. 

{% embed https://twitter.com/geekmomprojects/status/1479210241807900676 %}

(sidenote, she has [a really good blog](https://www.geekmomprojects.com/) and Twitter feed, especially if you are into hardware projects)...

Let's break this down. This is an [Espressif ESP32-C3FH4](https://www.espressif.com/en/news/ESP32_C3) 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](https://banggood.com/ESP32-C3-Development-Board-RISC-V-WiFi-Bluetooth-IoT-Development-Board-Compatible-with-Python-p-1914005.html), 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.

{% embed https://www.youtube.com/watch?v=SQO4Dn0vE3k %}

The [manufacturer's example software for the board is on GitHub](https://github.com/01Space/ESP32-C3FH4-RGB), and works absolutely fine with the Arduino IDE. I'm using the new [Arduino IDE 2.0](https://docs.arduino.cc/software/ide-v2/tutorials/getting-started/ide-v2-downloading-and-installing). One thing to note here is that you also need the [Espressif development release core](https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html) 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](https://twitter.com/ciro)... with Twitter API, for bonus points!

{% embed https://twitter.com/ciro/status/1488259161066459142 %}

## MicroPython, ESP32-C3, and NeoPixels

We quickly tried the MicroPython [port for ESP32-C3 with USB](https://micropython.org/download/esp32c3-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.

```python
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)
```

As an aside here, I'm using the [Thonny editor](https://thonny.org/) to do the editing, but you could use [VS Code](https://code.visualstudio.com/), [mu](https://codewith.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.

```python
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()
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97cx43k745tji967rzdq.jpeg)

Working with the MicroPython developers via Tweets and GitHub [issues](https://github.com/micropython/micropython/issues/8109) and [commit comments](https://github.com/micropython/micropython/commit/1f04a9a1fcd4172f7c721e05a4515c9c8bb0e8c9), 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:

```python
esp32.RMT.bitstream_channel(0)
```

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](https://micropython.org/download/esp32c3-usb/) of MicroPython.

Since the manufacturer's content is limited to [a YouTube video](https://www.youtube.com/watch?v=m-cgaS6eHv4), and the Arduino code, I've documented various experiments and findings on GitHub in [a set of discovery notes](https://github.com/andypiper/fivebyfive/blob/main/notes.md). 

In my [FiveByFive repository](https://github.com/andypiper/fivebyfive/) 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 😀

{% embed https://github.com/andypiper/fivebyfive %}

_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5k8vljz11nbiiq3fupew.jpeg)

---

Finally, a _**huge Thank You**_ to Debra [@GeekMomProjects](https://twitter.com/GeekMomProjects) (check out the [cool necklace](https://twitter.com/GeekMomProjects/status/1483699676536655873), and the 🌧️ [raincloud](https://twitter.com/GeekMomProjects/status/1489168933709967366)), and also to [Matt Trentini](https://twitter.com/matt_trentini) and especially [Damien George](https://github.com/dpgeorge) 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!