Getting Started with Micro Python

Getting Started with Micro Python

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

The first step a developer interested in Micro Python 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 Micro Python Github Board Summary page. Developers who want to get up and running quickly should consider the PyBoard, which comes with Micro Python 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 Micro Python on the development kit (to learn how to compile and install Micro Python 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 Micro Python, getting started with programming in Micro Python 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 Micro Python simply by typing.

[Learn Real-Time Software Using Micro Python from Jacob at ESC Boston]

For example, a great first thing to try is to learn to control the on-board LEDs. Control of any microcontroller peripheral in Micro Python 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 Micro Python 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 Micro Python 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. Micro Python 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 Micro Python.

For More Details: Getting Started with Micro Python

Scroll to Top
Scroll to Top
Read previous post:
jeep-inclinometer
Jeep Inclinometer

MEGR 3171 Project This project is the first time that I have ever attempted to do any type of coding...

Close