MicroPython or CircuitPython?

The Raspberry Pi Pico (and other RP2040 based boards) can be used with both MicroPython and CircuitPython. Let's look what they are and what is different between them.


MicroPython

MicroPython was announced in 2013, as a KickStarter campaing by Damien P. George. The aim was to create an optimized Python version that cuild be run directly in a microcontroller. The current version can be found in the official site and the source code is at github.

MicroPython implements most of the Python 3 language and standard libraries and adds features specific for microcontrollers. One key feature of MicroPython is the interactive operation over a serial connection (the REPL - Read-Eval-Print Loop). You type in a command, it is executed, the resulted is printed and a new command is awaited. Usually you interact with MicroPython using an IDE that supports REPL, like Thonny.

In MicroPython there is a machine module for microcontroller specific features. The Raspberry Pi Foundation has support MicroPython from the Pi Pico launch, including a machine module (with PIO support), examples and documentation.

Here is the traditional blink program fot the Pi Pico in MicroPython:

import machine
import utime

led_onboard = machine.Pin(25, machine.Pin.OUT)
while True:
    led_onboard.value(1)
    utime.sleep(5)
    led_onboard.value(0)
    utime.sleep(5)
CircuitPython

CircuitPython is a fork of MicroPython, sponsored by Adafruit. Most of the code is the same. Again, the current version is in the official site and source code is at github.

A list of the differences can be found at the README file, here is what I find important:
  • CircuitPython uses the native USB controller of the supported boards to implement a disk drive. To execute a program you write it into this drive with a special name.
  • Access to microcontroller features is different. Instead of the machine module there are many others, in a effort to create a consistent API (but pin names change according to the board).
  • There is no support to concurrent execution (interrupts and threading).
The blink program for the Pi Pico in CircuitPython is as follows:
import board
import digitalio
import time

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)
My Veredict

Both options are amazing.  They are great for quick tests and applications where you do not need to worry about low level details. The setup is really simple and fast: you load the interpreter on the Pi Pico, install the IDE, connect the board to the PC and the fun starts. I think these "embedded Pythons" are as important as BASIC was for the first microcomputers.

Looking at the official sites you will note that CircuitPython support more boards. It also has a greater number of libraries to support sensors and other modules, making it easy to start a new project.

On the other hand, there is the Raspberry Pi Foundation MicroPython support, particularly for the PIO. Also the concurrency support is great for some applications. Because of this, so far I have leaned to MicroPython for my Raspberry Pi Pico projects.

Comments

Popular posts from this blog