Search code examples
visual-studio-codemicropythonbbc-microbit

How to use external modules in MicroPython on micro:bit from VSCode


I’m new to both micro:bit and MicroPython (or Python in general) - but I want to have it all running in VSCode. I grabbed this extension which was really smooth working with.

My problem now is that I want to leverage external modules, for things like the NeoPixels and also the bit:bot stuff, but I don’t know how to actually get that working. The NeoPixel tutorial is straight forward, but there is no mention on how to add the module.

I tried adding them with pip - but that won’t make them end up on the device. I’ve also tried this extension - hoping it would do some more magic in getting it onto the device.

Is this doable? Or would I have to revert do the online editors?


Solution

  • The micro:bit is a very constrained environment and will not run Python only MicroPYthon. MicroPython was designed to work under the constrained conditions of a microcontroller. As a result MicroPython does not come with the full Python standard library and only includes a small subset of the Python standard library.

    For MicroPython to run on the micro:bit there needs to be the MicroPython hex file and any Python code that you have written, with main.py being the entry point.

    The VS Code Extensions you linked to use uFlash to copy from your machine to the micro:bit the hex file and any Python files you have written.

    To use the neopixel module it should be as straight forward as import neopixel as it is part of the standard BBC micro:bit MicroPython.

    For BitBot, it only uses the standard micro:bit MicroPython library so I'm not sure what you are looking to import.

    You can create a module by putting the code in .py file and referencing it in your main.py file. You do this by using an import statement that calls the file or specific parts of it.

    MicroPython does have the concept of upip but I am not aware of that being available on micro:bit.