Home

Awesome

micropython-magic

PyPI PyPI - Python Version PyPI - License PyPI - Downloads

<img src="docs/logo_S.jpg" width="100" align="right">

These Jupyter magic methods allow MicroPython to be used from within any Jupyter Notebook or JupyterLab (formerly IPython Notebook) The magics make use of the mpremote tool to enable communication with the MCUs

This allows:

A few of the possibilities

<table> <tr> <td> Live Plot of the cpu temperature <img src="docs/cpu_plot.gif" width="300" /> </td> <td> Visualize the memory map of the MCU <img src="docs/memory_map.gif" width="300" /> </td> </tr> <tr> <td> </td> <td> Memory allocation of the MCU over time <img src="docs/memory_map_sequence.gif" width="300" /> </td> </tr> </table>

For the source please refer to the samples folder

Installation

Recommended : install stubs for your MCU of choice

Usage

1) Create a notebook

2) Load the magic

%load_ext micropython_magic

This can also be configured once to always load automatically ( see below)

3) add a cell with some code to run on the MCU

# %%micropython  
from machine import Pin
led = Pin(25, Pin.OUT)
led.value(1)

The %%micropython cell magic will instruct Jupyter to run the code on the connected MCU

4) enable code highlighting for MicroPython

%pip install micropython-esp32-stubs==1.20.0.*
# installs the stubs for MicroPython syntax checking (one time install per environment) 
# %%micropython  
from machine import Pin
led = Pin(25, Pin.OUT)
led.value(1)

This allows for syntax highlighting and code completion of MicroPython code. Tested in VSCode with

More Examples

Please refer to the samples folder for more examples

  1. install - install the magic
  2. board_control - basic board control
  3. board_selection. - list connected boards and loop through them
  4. device_info - Get simple access to port, board and hardware and firmware information
  5. WOKWI - Use MicroPython magic with WOKWI as a simulator (no device needed)
  6. Plot rp2 CPU temp - create a plot of the CPU temperature of a rp2040 MCU(bqplot)
  7. Display Memory Map - Micropython memory map visualizer
  8. Plot Memory Usage - plot the memory usage of a Micropython script running on a MCU over time
<!-- 1. [](samples/mem_info_list.ipynb) - not currently working used to trace the m -->

Automatically load the magic on startup

In order to automatically load the magic on startup, you can add the following to your ipython_config.py file:

Configuration options

Configuration can be done via the %config magic

%config MicroPythonMagic

    MicroPythonMagic(Magics) options
    ------------------------------
    MicroPythonMagic.loglevel=<UseEnum>
        Choices: any of ['TRACE', 'DEBUG', 'INFO', 'WARNING', 'ERROR']
        Current: <LogLevel.WARNING: 'WARNING'>
    MicroPythonMagic.timeout=<Float>
        Current: 300.0

# example
%config MicroPythonMagic.loglevel = 'TRACE'

Development and contributions

The most welcome contributions are :

See current status and on Github