0

empowering creative people

MicroPython Primer for Makers - What is MicroPython?

This Tutorial is all about the MicroPython Programming Language. We will discuss what MicroPython is, why you will want to use it in your projects and we will share some resources to help you learn further!

What is MicroPython?

micropython-logo

MicroPython is an open source Python programming language interpreter that’s built for microcontrollers. This is Python simplified down to fit on a microcontroller and was designed to be as close to Python 3 as possible! Using MicroPython you can write Python code to control your microcontroller projects simply and easily as possible. Python is a language that’s designed to be as easy as possible for the programmer (that’s YOU!). With MicroPython you can program any Pycom board, Pyboard, Micro:bitESP2866 and more!

MicroPython is a great choice for the beginner programmer because it allows for a faster program development, with fewer opportunities for errors. Those of you that have programmed in Arduino know all about curly brackets {} and semicolons at the end of lines. Well forget all about them because they aren’t used in MicroPython. Instead, we use indentation to delimit our code.

All this makes MicroPython fast, easy, and dare we say FUN to program with! It also has some great features that make it stand out in the programming world and make developing with it even easier. These features are the REPL, more libraries then you can shake a stick at, and extensibility.

Interactive REPL

repl-micropython-screenshot

One of the biggest advantages to programming with MicroPython is the REPL. REPL stands for Read, Evaluate, Print, Loop. With the REPL you can run code directly off the microcontroller without any need for uploading or compiling. This allows you to lookup functions on your board, or code in real time. This is perfect for troubleshooting or learning how things work line by line.

Huge Libraries

actual-micropython-library

This makes programming with MicroPython a breeze. Just like Python there are many libraries already made and supported. This takes the load of programming common or repetitive operations yourself. On a MicroPython board, you would find libraries tailored for that specific hardware to make programming even easier!

Extensibility

The MicroPython interpreter allows you to create functions in C/C++ to allow you to handle high demand, low-level tasks. This way you can make a hybrid program where you use C/C++ where you need the extra speed, and still have the ease of organization and program manipulation that Python allows.


What’s So Great About MicroPython?

MicroPython sounds nice, but what’s it really good for? Just about everything! You can use MicroPython for anything you might want to do on a microcontroller! We can control LEDs, read sensors, create algorithms and much, much more. Everything that you use Arduino for, you can use MicroPython for. There are some great boards out there that you can use MicroPython with and start programming your microcontrollers with Python!

What’s Not So Great About MicroPython?

For most users, there aren’t any real downsides to programming with MicroPython, but there are a couple of limitations that we need to be aware of. The first limitation is speed. Because MicroPython is an interpreter, it takes a bit longer for the code to be interpreted than precompiled code. In Arduino, your code is compiled down into machine language that’s super-efficient for the microcontroller, and in MicroPython the code is interpreted as we go through it. This makes MicroPython slightly slower and take more RAM. In practice, this speed difference won’t be noticeable by most users, as MicroPython microcontrollers have much faster processors and higher RAM than the typical Arduino board. If you are an advanced user that really needs those extra microseconds, there is always the option to program in conjunction with C/C++ to circumvent this issue.

For those Python experts trying out MicroPython for the first time. You will find that the libraries aren’t as extensive for MicroPython. This is due to the natural limitations of size that we encounter on a microcontroller. There is only so much room to store all those libraries on a tiny microcontroller, but there is more than enough to store the most commonly used ones!


MicroPython Structure

The structure of MicroPython and Python are similar to Arduino but have some key differences. Indentation is critical in Python. The indentation takes the place of brackets in Python as the way that the structure of the code is maintained. If you have a background in Arduino you may notice that some things are missing from a MicroPython program. Because MicroPython is interpreted, we don’t need to declare our variables or have a setup section of code. All this is done on the fly. The MicroPython looks at each variable and assigns it a type based on what value you assign to it. It’s easy to get confused about what your variable types are, so choose your variable names wisely.

Another difference you will encounter is the order of operations with regards to functions. In Arduino code is typically arranged in this order:

  • Import libraries
  • Define variables/constants
  • Setup
  • Loop
  • Functions

A MicroPython sketch runs top to bottom, and because its interpreted rather than compiled, it needs to identify the functions before they can be called. This means that the functions need to be higher in the code than any callouts. If you put your functions after the loop the interpreter will never reach them and your callouts won't work. This is what it looks like in practice:

  • Import libraries
  • Functions
  • Setup (code that is only run once)
  • Loop

If you read between the lines here, you may realize that MicroPython doesn’t even look at code until it must. When making conditional statements that might never be met, you need to take caution that you don’t have any hidden errors that won’t be found until long into your program. Test all your conditional statement while developing your code. There is no error checking of the code other than running it!

Going Further

Are you brand new to Python and want to learn more about how to program with it? Here are some great resources for learning Python:

Google’s Python Class – An Oldie but a Goody. This class was made a while back, but everything is still relevant today. This class is complete with videos and lessons that take you from basic Python to a bit more advanced. This is a perfect place to start if you want to know EVERYTHING!

Python, Getting Started – This is the official Python documentation page. There are a ton of links and guides to installing Python on your system if you want to use actual Python, and of course, using it.

The Hitchhikers Guide to Python – Very complete Python Tutorials

The basic structure of MicroPython is Python. If you want to start using MicroPython you need to use a MicroPython compatible board. Some of the libraries for each board will be specific to that hardware, so it makes the most sense to learn MicroPython specific to the device you are using.

MicroPython Compatible Devices

Pycom Lopy4 with HeadersPycom – The entire line of Pycom boards are programmable with MicroPython
Getting Started with MicroPython for Pycom
Introduction to MicroPython, Pycom

Pyboard for MicroPython

Pyboard – This is the official MicroPython board made by MicroPython.org
MicroPython Official Documentation 

Microbit Board is compatible with MicroPython

BBC Micro:bit – This great educator friendly board can also be programmed with MicroPython

MicroPython Documentation for the Micro:bit

ESP2866 is compatible with MicroPython

ESP2866 – There is great support for these Wi-Fi capable boards

ESP2866 MicroPython Documentation

There are more boards out there that are capable of running MicroPython, but don't have full level support yet. A great example is the Teensy 3.X, it has a MicroPython port but only try it out if you are an advanced user!

CircuitPython Boards – There are more boards out there that use CircuitPython, a very similar derivative of MicroPython. You can find a full description of the differences in our Tutorial, CircuitPython vs MicroPython: Key Differences.

Circuit Playground Express - Great feature packed board great for beginners and educators.

Adafruit Feather M0 Express - All the great features and 'wings' of the Adafruit Feather but made for CircuitPython

ItsyBitsy M0 Express, GEMMA M0, Trinket M0 - Small profile boards perfect where space is at a premium

Going Further

Interested in learning more about MicroPython? We have a whole section of Tutorials Devoted to MicroPython and boards that use it! If you want to learn more about the differences between CircuitPython and MicroPython, check out CircuitPython vs MicroPython: Key Differences! There is a whole world of new programming opportunities out there for both MicroPython and CircuitPython. There is a reason that Python is the most taught programming language around the world! Try it out for yourself!

This Tutorial is all about the MicroPython Programming Language. We will discuss what MicroPython is, why you will want to us...

Have a question? Ask the Author of this guide today!