Translate python to micropython for Pico?

I recently bought the ICM20948 9DoF Motion Sensor to use with pico but it turns there is no micropython library for it. What’s involved with translating the python library to make it pico compatible? Is there anything out there that can autotranslate? Are there any guides for this process? What are some common issues when translating something for a raspberrypi to run on a Pico?

You can try multiple things:

  • use CircuitPython (it has a driver for the ICM20948)
  • use MicroPython with Blinka, the CircuitPython compatibility layer
  • port the Pimoroni Python-driver to MicroPython

The Python-drivers usually use smbus for the I2C interface, and there is a MicroPython compat smbus library which might help here.
If the Python-driver uses Rpi.GPIO, you must translate this to normal MP gpio usage, but that should be straightforward.

My experience is that Pimoroni Python-drivers tend to mix up the layers, so you might find additional Python modules in use. But usually you can strip this down to your needs.

This thread might be useful for ICM20948, it contains a pure MicroPython driver that @Tonygo2 found. I think there’s also a CircuitPython driver for ICM20938 that it might be possible to convert?

Brilliant. Just the sort of answers I’m looking for. Will start there. Many thanks.

This is v useful. To see a driver written in pure micropython gives me a decent idea of what needs to happen to take the raw output from a sensor and get the data into a state were I can make use of it. Thanks!

I just signed up for chatgpt plus so thought I’d see how it performed with a Python to Micropython library conversion. It seems to want to make me do some of the work but has laid out exactly what’s involved… I think.

I gave it the Pimoroni Python library and asked it the following:

Can you convert this Python library for the Pimoroni ICM20948 9DoF Motion Sensor Breakout to micropython so that it can run on the raspberry pi Pico?

Sure, I can help you get started on converting the Python library for the Pimoroni ICM20948 9DoF Motion Sensor Breakout to MicroPython so that it can run on the Raspberry Pi Pico. Note that I can’t provide the entire conversion, but I’ll give you a basic framework that you can build upon.

First, let’s make some general changes to make the code compatible with MicroPython:

  1. Replace smbus2 with machine.I2C.
  2. Replace time.sleep with utime.sleep.
  3. Replace the print statements with appropriate MicroPython functions.

Here’s a basic MicroPython framework for the ICM20948 library:

import utime
from machine import Pin, I2C
from micropython import const

# Constants and register addresses
# ... (Copy the original constants and register addresses here)

class ICM20948:
    def __init__(self, i2c_bus, address=ICM20948_I2C_ADDR):
        self._i2c = i2c_bus
        self._address = address
        self._buffer = bytearray(2)

    def init(self):
        # ... (Copy the original initialization code here, making necessary changes)

    def _write_byte(self, reg, value):
        self._buffer[0] = reg & 0xFF
        self._buffer[1] = value & 0xFF
        self._i2c.writeto(self._address, self._buffer)

    def _read_byte(self, reg):
        self._buffer[0] = reg & 0xFF
        self._i2c.writeto(self._address, self._buffer, end=1)
        return int.from_bytes(self._i2c.readfrom(self._address, 1), 'little')

    def _read_bytes(self, reg, length):
        self._buffer[0] = reg & 0xFF
        self._i2c.writeto(self._address, self._buffer, end=1)
        return self._i2c.readfrom(self._address, length)

    # Other methods for reading and processing data
    # ... (Copy the original methods here, making necessary changes)

def main():
    # Define SDA and SCL pins
    SDA_PIN = 0
    SCL_PIN = 1

    i2c = I2C(0, sda=Pin(SDA_PIN), scl=Pin(SCL_PIN), freq=400000)
    sensor = ICM20948(i2c)

    while True:
        # Read and process data from the sensor
        # ... (Use the appropriate methods from the ICM20948 class)


if __name__ == '__main__':

This framework gives you a starting point to convert the Pimoroni ICM20948 Python library to MicroPython. You’ll need to go through the original library and copy the constants, register addresses, and other methods into this framework, making necessary changes to make it compatible with MicroPython.

Once you’ve done that, you should be able to run the code on a Raspberry Pi Pico. Make sure to also include any dependencies and make necessary changes to those as well, if required.

Sadly I’m not able to test on a Pico for another week or so. Does it look like ChatGPT is on the right lines here?

Amazing. A perfect template for an I2C-device. It does not take the easy route of using a shim-library (e.g. translating smbus → i2c), but that will result in better code anyhow.