The Joy of Running (Micro) Python on a RP2040 Microcontroller
I know, I'm late, but I've finally joined the party!
I did play with microcontrollers during my final years at university. Back then I was part of a robotics team and I developed the firmware for a color sensor.
As part of my bachelor thesis, I even wrote the code for a bat-meter. You might wonder what that is. Well... that's a story for another time.
Time has passed and I never truly got to play with microcontrollers again. Mainly because I've felt intimidated with:
- having to design my own board
- getting proficient in a low-level language that allows you to write firmware for a microcontroller
- having an exciting project to do.
Eventually, the stars aligned, and I finally got to try running Python on an RP2040.
You can read here about my first project developed in MicroPython.
Backstory
It all started with me wanting for a long time to replace the PCB inside my IBM Model M keyboard with a board capable of running QMK firmware and offering the unrenounceable USB-C port.
A while back the QMK firmware added support for the RP2040 microcontroller and I've also discovered that some knockoff clone boards are available for cheap with USB C ports. I bought one and put together a little micropad to test if everything worked. It did.
Fast forward to a few days ago, I remembered my original plan and purchased two RP2040-based boards that break out enough pins to connect the Model M matrix because the one I already had didn't.
One of the two boards, known as YD-RP2040, has a couple of nice features that make it a great candidate for anyone wanting to experiment with writing software for microcontrollers.
Pins 23, 24, and 25 are connected to an RGB LED, a blue LED, and a button.
That's plenty of IO options to start writing the classic hello world of the microcontroller world. No more annoying breadboards, wires and clutter.
With the first intimidating obstacle removed, I still had to figure out how to write the firmware. Despite having recently tried to learn Rust again, I'm not really fluent in any low-level language. Fortunately, I remembered my ex-colleague and friend Mattia mentioning MicroPython.
The knowledge of MicroPython's existence rested for years in the back of my mind, but it floated back to the surface at the right time!
So... yeah! That's pretty cool and easy stuff!
What is using MicroPython like?
In short, you flash the Micropython firmware on the microcontroller (which, in the case of the RP2040, consists of transferring a file over USB).
No need for a hardware programmer and fiddly cables, just a USB-C cable.
Then you write your code in Python and use the mpremote (MicroPython remote) command to transfer the Python file to your board's memory, and you are good to go!
If you can write Python code, you can already program a microcontroller!
Debugging is also quite easy.
You once again use mpremote to check the output of your program (via serial over USB), much like you would with a standard Python program.
When something crashes, you can simply connect and look at the stack trace or everything you print.
TL;DR
If you would like to experiment with microcontrollers and you already know Python, just grab a board with an RP2040, possibly the YD-RP2040 (which is very cheap and has some nice onboard features), and start coding.
My only regret is not having tried this earlier.