Skip to main content

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.

STM32F401 Nucleo

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:

JP5_marked_ebf52f4c44dea8ac7859c0d5db280426036b8722.jpg

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.

01_-_link_driver1_4aaa433678f98b29532254d04d4c18518ed08d51.jpg

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:

02-_executable_1a82c4761b4717513fcca6c88174819e6b944b15.jpg

Run that executable and in the interface, click the [Device Connect] button:

03a_-_update_yes_button2_b4ab484d449beb3e6b7057246973cbc2b764e4f8.jpg

Click on the now active [Yes>>>] button. When done, you should get an “Update Successful” message.

Zerynth Studio

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:

04a_-_updated1_aeb1fc4ba340b896da5253b8e6f7b2323d608f71.jpg

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:

05a---target1_f5896e2356799c50431a5c3ce441d946b556d41c.jpg

Next, we want to register the device using the [Z] button on the DMT:

06---z-button_arrow_c4bb1419ef0ec9da2e146bd5f25e41c0dab6ec3e.jpg

Click on the [Register] button:

07_-_register1_b743edd35e80b5d992b2a0d886aba69c14735944.jpg

Which will show us that it is registering the device:

07b_-_registering1_faaa69406344dc481571399d6976756d18ef260e.jpg

If your board is successfully registered, you should see something like this:

08---succesful-arrow_a8150abfaabefe2786aed7131a154afc6f677dbc.jpg

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:

09_-_create_VM_857983d7cd5df57f542e3ea6dfba379c7c3c281a.jpg

Make sure the ‘Standard VM’ radial button is selected and hit [Create]:

10b2_8a68a951858335ae3ca7704451636f26b2da2ca0.jpg

Then Hit the [Virtualize] button:

11_-_virtualise2_304b772e0b2ac113ee8b9d1d271be2adce98608d.jpg

Followed by [Virtualize] again:

11b_-_virtualise_pt2_5d988636ad93817d5cbcd56ab386d378ab4f91c8.jpg

After a bit of whirring (depending on the speed of your host PC) we should be told that our device has virtualised:

12b---VM-ok_86397613ded30407a962c34a17ee3c29cc25f928.jpg

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:

ST_nucleof401re_pin_io3_92ceddaf8fe85f1bca88547332eb1d0ef5d8a76a.jpg

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:

13_-_examples_50f3509e15339dde0a4a8b32429519b3e941eb2b.jpg

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:

14_-_clone_blink_db17be125e73487bd1399cc61a434100bd782991.jpg

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:

15_-_make_it_our_own_2b1278f3012564f7f58f8e0f4d44f9d45f9ab89e.jpg

To get this code onto our device we will simply use the ‘uplink’ button:

16---uplink-arrow_8721815039dc40fd6ac136fdb0b76aca1c4c5980.jpg

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…

reset_buttonsm_752f5195138fc3590ce51914a21c4ad72857f47d.jpg

…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:

17_-_reset_c7f328cadf3a3e82dcfbf3b28af2e64fe3afdf6c.jpg

Now, if everything has gone to plan, we will get the ‘uplink done’ message in the message window:

18---done-arrow_f05449b6650ca0e7601f6716ef7c5995c79cf91e.jpg

We should also have a blinking LED on our Nucleo board. Yea, us!

Final Thoughts

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!

Mark completed his Electronic Engineering degree in 1991 and worked in real-time digital signal processing applications engineering for a number of years, before moving into technical marketing.
DesignSpark Electrical Logolinkedin