Path: EDN Asia >> Design Ideas >> IC/Board/Systems Design >> Guide to using Micro Python
IC/Board/Systems Design Share print

Guide to using Micro Python

20 Apr 2016  | Jacob Beningo

Share this page with your friends

In a previous article, 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 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.

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 5Hz using the following code:


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:


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.

About the author
Jacob Beningo is a Certified Software Development Professional (CSDP) whose expertise is in embedded software. He works with companies to decrease costs and time to market while maintaining a quality and robust product.

Want to more of this to be delivered to you for FREE?

Subscribe to EDN Asia alerts and receive the latest design ideas and product news in your inbox.

Got to make sure you're not a robot. Please enter the code displayed on the right.

Time to activate your subscription - it's easy!

We have sent an activate request to your registerd e-email. Simply click on the link to activate your subscription.

We're doing this to protect your privacy and ensure you successfully receive your e-mail alerts.

Add New Comment
Visitor (To avoid code verification, simply login or register with us. It is fast and free!)
*Verify code:
Tech Impact

Regional Roundup
Control this smart glass with the blink of an eye
K-Glass 2 detects users' eye movements to point the cursor to recognise computer icons or objects in the Internet, and uses winks for commands. The researchers call this interface the "i-Mouse."

GlobalFoundries extends grants to Singapore students
ARM, Tencent Games team up to improve mobile gaming

News | Products | Design Features | Regional Roundup | Tech Impact