Arduino

Arduino With Python Tutorial for Beginners

When you have started playing with Arduino boards, the standard programming language is provided by Arduino. This language is extremely useful for getting started and can even be used for real use. People who have used it for a while, though, notice a few limitations. You might also be used to programming in Python already. For this reason, developers have invented Micropython.

With Micropython, you have all the basics of Python, with limitations due to the hardware you are finally running it on. This article will not discuss these limitations. Hopefully, you have a clear picture of what a microcontroller can do. Most likely, you will find that it can do much more than you imagined before you started.

Some solutions

There is a multitude of ways you can start programming an Arduino using Python. Before you start, you want to think about whether you are preparing a new Arduino program or want to talk to one. There are several libraries that create new Arduino programs, bypassing the standard programming system that they supply.

You have boards that already run Micropython; you can find these on their respective home pages.

You may want to create a Python program that talks to a standard microcontroller. If you do, you have a few interface libraries for Python. Well-known ones are Micropython and CircuitPython; these are ready distributions for running on special boards. You can compile for other boards if you have the skills.

The mu-editor for micropython

A simple editor to use is a mu-editor. This editor is prepared so that it detects your board on the serial port if you have one. If you do not have one, you can start working with regular Python. To choose, change the mode from the left top corner. The standard Python works, and you can get used to the editor.

This editor has a few IDE features, like code completion, highlighting, and you can start a REPL. These features all work even when connected directly to the board. To install the editor, you can find it in your distribution’s repository.

$ sudo apt install micropython mu-editor mu-editor-doc

These are all the tools you need with a board that already has Micropython on it. One simple code you can try is the common blinking of the LED on the board. To get to the hardware, like an LED, you need to import the library.

from pyb import LED

import time


state=False;


while True:

    time.sleep(0.5)

    if state == False:

        LED(on);

        state=True;

    else:

        LED(off);

        state=False;

Use the code above to try your new board. Note that the ‘pyb’ will vary from board to board, Adafruit uses the machine. Take the time to learn what your boards’ values are from the documentation.

REPL – Read, Evaluate, Print, Loop

When using MicroPython, or any Python, you have a REPL available. This is a great way to test short snippets of code. In this case, you can use it to discover what modules are available. The help() function does a great job of guiding you through the basics of what you have available.

When you run help() without parameters, it gives you a list of options. After that, it is interactive; type in what you need to ask about and guidance on using it.

Use the REPL to find what libraries the board supports. It is a slightly harder learning method, but you get in the habit of using the built-in documentation. To truly learn, you need to take a few tutorials and build something else upon them.

Boards running Micropython

The easiest way to start programming for Arduino using Python is to buy a board ready for it. The boards that exist on the market are impressive and come from many suppliers. The main libraries are CircuitPython and Micropython.

An impressive line of boards come from Adafruit, called Circuit Playground. These boards are round, which is odd. More importantly, they have 10 Neopixels onboard, and that is just the visual part. Several sensors are on the board, also included are two push buttons and a slide switch. The input/output pins are made for using alligator clips while still being available as capacitive touch buttons.

Seedstudio also has a range of boards supporting CircuitPython. These come in a range from very small to very capable. The WiPy 2.0 is a tiny board that is ready to go, though it is useful to get the antenna kit. The board sports a WiFi module for the ESP32, one RGB LED, and a reset switch. You get much less hardware, but the size is 42mm x 20mm x 3.5mm, and you still have many pins on the board.

Simple projects to get you started

After you have made your blink program, you are certain to want to try something harder. Make sure you have something compelling that is challenging but solvable. Here are some suggestions.

Make a program that flashes one light at a steady pace. At the same time, make a button turn on and off another lamp. You will quickly see the limitations of delay()!

Make a MIDI controller.

Make a simple alarm system using an infrared sensor and some NeoPixels.

Conclusion

The best way to get started with MicroPython is to get a decent board that already supports MicroPython or CircuitPython and start trying out your ideas. Since the idea is to control other things, look for a package, or a kit, that contains a few sensors and a display or two.

Happy Hacking.

About the author

Mats Tage Axelsson

I am a freelance writer for Linux magazines. I enjoy finding out what is possible under Linux and how we can all chip in to improve it. I also cover renewable energy and the new way the grid operates. You can find more of my writing on my blog.