I have to admit that I am pretty ‘oldskool’ when it comes to programming my toys microcontroller based systems: I like the C programming language and for serious embedded programming, I am still waiting to be convinced that there is anything better.
That said, I also like quick and dirty ways to get a proof-of-concept up and running and I also appreciate that while my back was turned, Python took the crown as the most used programming language and is being directed toward more and more serious applications, such as machine learning with the likes of the Intel Movidius Neural Compute Stick.
So in this article, I want to show how you can get to the point where you are running a ‘Hello World’ application, written in Python and running on a mainstream dev kit, from scratch, in under half an hour. Unless you have a slow internet connection, of course; then it might take you a little longer.
For our dev kit, we are using the ST Micro Nucleo F401RE board. This is a capable board for embedded applications and provides us with:
MCU: STM32F401RET6 ARM®Cortex®-M4 32-bit, 84MHz
SRAM: 96 KB
Flash: 512 KB
Digital I/O: 50 pins at 3.3V (in the ubiquitous Arduino format)
ADC input: 16 pins
..amongst a host of other fun features.
One nice thing about using the Nucleo is that the only other bit of hardware we need to get started is a USB A to USB mini cable which we will be using both for power and for programming the board.
Before We Get Started
There are a couple of things we need to do before we get to use our ST Nucleo board in earnest. The first thing to do is make sure our power jumper is in the right place on the Nucleo board: On JP5, the jumper should connect pin 1 and pin 2:
If you are using Windows, your next step is to download and install the ST-Link driver. This will give us access to the ST Nucleo programming port and create a virtual COM port on our host computer. If you are using MAC OSX or Linux, you are good to go already, as the device will be recognised automatically.
We also need to upgrade our onboard firmware before we can use our Python IDE. The upgrade and instructions for installing it can be found here. Once you have downloaded the zip file and unpacked it, Windows users will find an executable file:
Run that executable and in the interface, click the [Device Connect] button:
Click on the now active [Yes>>>] button. When done, you should get an “Update Successful” message.
We are going to download and install Zerynth Studio as our IDE for programming the ST Nucleo board in Python. Although there is a ‘Pro’ version available, the free version gives us everything we need to build pretty sophisticated applications across a range of supported boards from Arduino, Hexiware, NodeMCU, Particle, RedBear, ST Micro, Sparkfun and a number of other manufacturers.
The installation process is pretty simple, no matter what OS you are using, as you can download a Windows executable installer, a MacOS Disk image (.dmg) file or a ‘./zerynth’ script (inside the “tar.xz” archive) for Linux.
Once installed and running you will be presented with an interface not too dissimilar to this:
If you now connect up your ST Nucleo to a USB port on your computer (or if you are already connected) Zerynth will recognise the ST Nucleo and you can select it in the ‘target’ box on the Device Management Toolbar:
Next, we want to register the device using the [Z] button on the DMT:
Click on the [Register] button:
Which will show us that it is registering the device:
If your board is successfully registered, you should see something like this:
Now we are going to hit that [Z] button again to create a virtual machine on the target to run our code. This will bring up an action box where you need to click the [Create] button:
Make sure the ‘Standard VM’ radial button is selected and hit [Create]:
Then Hit the [Virtualize] button:
Followed by [Virtualize] again:
After a bit of whirring (depending on the speed of your host PC) we should be told that our device has virtualised:
That’s it! We are now ready to program our board in Python.
Programming In Python
If you are new to embedded Python programming, then you are going to love this next part, because Zerynth gives you a tremendous headstart. First, is the rather attractive I/O pin map - always useful when working out what pin names we need for our program:
But more importantly, If you look down the icons on the left-hand side of the IDE window you will see a light-bulb shaped icon, which is the examples button. Click on this, and you bring up a list of Python example code that can take you from first steps to some quite involved hardware interaction using the Zerynth libraries. As we’re looking to get our basic ‘hello world’ blink application up and running, we want to click on the arrow next to ‘Basics’ to list the introductory examples:
Then click on ‘Blink’ to bring up the example code box. We can drop that code into our active programming window by hitting the [Clone] button:
Just so we feel as though we have actually done some work here, let’s make the example our own and give our flashing LED a short duty cycle:
To get this code onto our device we will simply use the ‘uplink’ button:
Note that if you just want to verify your code, without uploading it, you can just use the button to the left of the uplink button.
Now, a word of advice: keep your finger over the reset button on the Nucleo…
…as you will be asked to reset your board during the uplink process and don’t have a huge amount of time before the uplink times out:
Now, if everything has gone to plan, we will get the ‘uplink done’ message in the message window:
We should also have a blinking LED on our Nucleo board. Yea, us!
The ST Micro Nucleo board is a solid microcontroller dev kit that has support from many of the traditional embedded tools developers, including IAR EWARM, Keil MDK-ARM, mbed and GCC/LLVM.
However, I like what Zerynth has done here. The amount of time it takes to get some code on the board, from a standing start, is minimal. This makes it really easy to try out and tinker with ideas on your dev kit.
Zerynth has also put a lot of effort into the documentation, examples and programming guide so you can learn Python by using it for real-world applications that aren’t just web scripts. Take it from this old dog, there are some new tricks you can learn out there that are well worth your time. Happy coding!