Skip to content

Instantly share code, notes, and snippets.

@stonehippo
Last active April 29, 2021 16:33
Show Gist options
  • Save stonehippo/301fbd2bf1ec6bf522077e382b006cec to your computer and use it in GitHub Desktop.
Save stonehippo/301fbd2bf1ec6bf522077e382b006cec to your computer and use it in GitHub Desktop.
Snaking around on tiny computers with Python, CircuitPython, and MicroPython

Python on microcontrollers

Python is a great language for development. Microcontrollers are wonderful for physical computing. What's better than either one alone? Both together!

The good news is there's growing support for using Python on microcontrollers, both as programming language off-board and directly interpreted on embedded hardware.

CircuitPython

Based on MicroPython, CircuitPython was created by Adafruit for its boards, and is intended to provide a easy to use, consistent interface across multiple microcontrollers. It also has an extensive library/driver bundle.

CircuitPython makes use if the MicroPython VM, which is designed to run directly on embedded microcontrollers. It's a reimplementation of Python 3, and has all sorts of pythonic goodness, including a REPL.

CircuitPython supports a growing number of dev boards, and Adafruit maintains many, many libraries to support common hardware and peripherals. Adafruit has intentionally focused on devices that have native USB capability, so that adding and editing application code can be done on-device with a standard editor. Firmware updates and adding libraries are drag-and-drop operations. This makes for a great beginner experience, and is pretty great even after you get to know your way around.

MicroPython

An implemenation core features of CPython 3.4, tailored for use on microcontrollers.

MicroPython runs a true Python interpreter on embedded hardware. It has been ported to several boards, including the popular ESP8266 and ESP32 boards. It's VM has been adopted as the core of CircuitPython, and much of the core API is there, too (though there are some very notable differences, too).

MicroPython is easy to get up and running, thought not as easy as CircuitPython. You'll typically need a tool like ampy to send files up to the board, and a board-specific tool to flash updates (for example, for the ESP boards, you'll need esptool).

There are few boards the ship with MicroPython installed, including Pyboard and the various dev boards from Pycom.

ArduPy

ArduPy is a blend of MicroPython and Arduino, from Seeed Studio. It uses a command line tool, aip, to work with Arduino libraries using the Arduino API, and bundles them up into firmware that runs on ArduPy-compatible devices (which are currently the Wio Terminal and Seeeduino XIAO).

BBC micro:bit

The BBC micro:bit has it's own port of MicroPython.

An alternative way to write in Python on the micro:bit is to use Microsoft Makecode. This version of Python on micro:bit has a different API than MicroPython, and functions a little differently. It's a third high-level language on top of the Makecode compiler (along with Blocks and Javascript), and the compiled .hex files it produces will be identical to those produced with the other langauges. Well, it's really a Python-ish language subset called Static Python, which in turn gets converted to Static TypeScript, but it's close enough for learning the rudiments of Python.

In contrast, the micro:bit's MicroPython .hex files will contain both user application code and the MicroPython intepreter, and is therefore somewhat resource constrained compared to compiled code.

For more detail, take a look at this explanation of differences between MakeCode Python and MicroPython on the micro:bit.

Makecode Arcade

Similar to the Makecode for BBC micro:bit, Microsoft's Makecode Arcade can be programmed using Static Python. Games developed with Makecode Arcade can be deployed on Adafruit's PyBadge and PyGamer boards.

This isn't true Python on a microcontroller, as the resulting game doesn't run on a Python interpreter, but I've included it here for completeness. If you're looking to write games for small hardware and want to use Python syntax, this is one way to do it!

Pycopy

Pycopy was originally a fork of MicroPython. I'm not 100% certain what happened, but it seems that Pycopy's main contributor had a falling out with the MicroPython team, and so maintains this version. Still brings in some code from MicroPython on occasion. I haven't used Pycopy, but noting it here in case I do want to try it.

Raspberry Pi

The Raspberry Pi is a Single Board Computer, and not really a microcontroller. When running Raspbian, it has a multitasking OS, so it's not always great for working with buttons or sensors. One the other hand, it can still do it, and even share some of the code from microcontrollers directly.

And in case I need to find it, here's a handy pin reference for the RPi header: https://pinout.xyz/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment