personal_site/_posts/2024-07-20-micropython-1.md
2024-07-29 10:49:20 +01:00

4.0 KiB

title, layout, excerpt, image, thumbnail, assets, alt, head
title layout excerpt image thumbnail assets alt head
MicroPython post Embedded Programming is fun again! /assets/blog/micropython/xkcd.png /assets/blog/micropython/thumbnail.png /assets/blog/micropython A crudely edited version of XKCD 353, the one about python, but with a greek letter mu stuck in front of "python". <script src="/assets/blog/micropython/micropython.min.mjs" type="module"></script>
Original

My first exposures to programming as a kid were through processing and arduino.

With Arduino, I understood that it's really just C with some libraries and a toolchain and started to read the datasheets of things like the ATmega328. This is super fun because the atmega328 is a relatively simple computer and you can just about read through the datasheeet.

The atmega328 has all sorts of hardware that you can configure, like the timers that you can set up to count up/down, to trigger interupts, toggle pins etc. I had loads of fun with this as a nerdy kid.

However the compile and upload time is kinda long, C that hits registers directly is both ugly and hard to debug and when you start to work with bigger systems like the ESP32 and RP2040 that have WiFi and multiple cores and stuff this all starts to get a bit less fun, at least for me.

But because the likes of the ESP32 and the RP2040 are so much more powerful they can run a python interpreter and it's surprisingly fast and really fun!

Everyone loves to hate on python for being slow, and obviously don't write your tight loops in it (or do, I can't tell you what to do). But even on a resource constrained microprocessor you can have a fun time!

So anyway, here is a compendium of things I've being doing with micropython. Some of this is so that I don't forget how to do it later so there's a little more detail than might be warranted.

Get yourself a dev board

The Raspberry Pi Pico is really nice, if you went to EMFcamp you can use the badge, and ESP32 boards work really well too! The easiest way to start is to flash a prebuilt firmware (for RP2040 boards that means putting the board in boot mode and then dragging and dropping a .uf2 file onto a virtual file system that appears.)

Run some code!

mpremote is a really handy little tool for interacting with a micropython board. My first scripts to play with this looked like this

mpremote cp main.py :main.py
mpremote run main.py

You can also just run code

{% flexible_include assets/blog/micropython/example_micropython.py %}

Webassembly port

People are staring to use webassembly to create simulators for physical hardware that run in the browser, this can make the dev loop super fast. More details here

For me this looks like:

cd /ports/webassembly
make min FROZEN_MANIFEST=/path/to/font_sources/manifest.py
cp ...wasm and ....min.mjs to your webserver directory

Then in the js console you can do:

const mp = await loadMicroPython();
mp.runPython("import fonts; print(fonts.gunship30)")

Note that I've got access to my compiled in code here.

Run