Getting started with MicroPython

-April 06, 2016

In a previous blog, Using MicroPython for real-time software development, I examined the idea behind using MicroPython for real-time embedded software. Now, let’s examine some of the core pieces that developers need to understand to get started using MicroPython.

The first step a developer interested in MicroPython must take before diving into a real-time embedded application is to select a supported development kit or microprocessor. The best place for a developer look for a supported development kit is to check-out the MicroPython Github Board Summary page. Developers who want to get up and running quickly should consider the PyBoard, which comes with MicroPython already installed. Developers who want to use a development kit that is suitable for working with Arduino shields and don't mind the extra step of putting MicroPython on the development kit (to learn how to compile and install MicroPython see Jacob's videos) should consider using the Netduino Plus 2.

Figure 1 – PyBoard (Left) and the Netduino Plus 2 (Right)

Once a developer has hardware running MicroPython, getting started with programming in MicroPython is relatively straight forward. The developer simply plugs their device into a PC over USB, which allows them to access the REPL (Read Evaluate Print Loop) terminal. The REPL allows a developer to test out commands and scripts using MicroPython simply by typing.

For example, a great first thing to try is to learn to control the on-board LEDs. Control of any microcontroller peripheral in MicroPython is done using the pyb library. The first step is to type “import pyb” into the REPL and press enter. A developer can then access any microcontroller peripheral through the pyb library. For example, a LED can be turned on by typing pyb.LED(LED #).on() or turned off by typing pyb.LED(LED #).off(). A simple example of how four different LEDs can be turned on with MicroPython can be seen in Figure 2.

Figure 2 – REPL Terminal with LED On commands

One of Python's advantages it isn’t just a scripting language; Python is an object oriented programming language. A developer can, for example, create an LED object that controls all of the LEDs on the development board. The PyBoard has four different LEDs; red, green, yellow and blue. Each LED is assigned a number starting with red at one and ending with blue at four. A simple script could be written that toggles the green LED every one thousand milliseconds by assigning an object, named Led, to pyb.LED(2). An example of what the LED toggle script would look like can be found in Figure 3.

Figure 3 – Green LED Toggle Script

The pyb library contains a number of predefined classes for controlling the microcontroller peripherals. A few examples include PWM, CAN, and timers. Figure 4 contains additional example classes and the MicroPython documentation contains all of the supported classes.

Figure 4 – Pyb library classes

Periodic tasks, such as toggling an LED, do not necessarily need to be performed through the Python script's main loop as in the example, though. Developers can select to use the microcontroller's timer peripheral and use an interrupt to change the LED's state. Setting up the timer is relatively straight forward. First, a developer needs to select which timer should be used (timer1, timer2, etc.) and create a timer object using code similar to the following:

TimerGreenLedToggle = pyb.Timer(2)

where the number 2 is the timer number that should be used with the TimerGreenLedToggle object.

Assigning the timer object is a great first start but a developer also needs to assign a frequency to the timer. The timer can be initialized to fire an interrupt at a frequency of 5 Hz using the following code:

TimerGreenLedToggle.init(freq=5)

However, a developer executing the above statements in the REPL would discover that nothing really interesting happens. The reason nothing happens is that the timer needs to have had assigned a callback function to execute every time the interrupt fires. MicroPython has made assigning such a callback function very easy. Take, for example, a function named GreenLedToggle that simply calls Led.toggle(). A developer can assign GreenLedToggle to the TimerGreenLedToggle callback by using the following code:

TimerGreenLedToggle.callback(GreenLedToggle)

You can see how simple it is to create a simple toggling LED! Even the timer setup takes just a few lines of Python code. The above timer code can literally be written in a couple of minutes. A developer working in traditional C code normally wouldn’t get away this easily. A fair amount of time and effort needs to be put into C code just to get to the starting point of MicroPython.

Jacob Beningo is principal consultant at Beningo Engineering, an embedded software consulting company. Jacob has experience developing, reviewing and critiquing drivers, frameworks and application code for companies requiring robust and scalable firmware. Jacob is actively involved in improving the general understanding of embedded software development through workshops, webinars and blogging. Feel free to contact him at jacob@beningo.com, at his website www.beningo.com, and sign-up for his monthly Embedded Bytes Newsletter here.

Also see:


Loading comments...

Write a Comment

To comment please Log In

FEATURED RESOURCES