Are you looking for easy access to the world of microcontrollers and electronic projects? Or are you already an old hand and just feel like doing something new? There are many reasons to try CirycuitPython!

In this article you will learn what CircuitPython is and what you need to get started. We’ll write some Python code together and let some LEDs do wild things. 🙂

What is CircuitPython?

The New York company Adafruit presented its “new” programming language in the summer of 2017: CircuitPython. This is a so-called fork of MicroPython, another language that was also developed for programming microcontrollers. Both programming languages are based on Python 3 – but CircuitPython was developed with the goal of being particularly beginner-friendly.

Python is considered to be an easy to learn programming language and is now also taught in schools. But Python is not only for beginners – complex projects in the field of artificial intelligence are also realized with Python.

What advantages does CircuitPython have?

Apart from the fact that Python is relatively easy to learn, the use of CircuitPython has other advantages.

Probably you have already written sketches in C++ in the Arduino IDE and loaded them onto your board. Then you know that every change in the code involves a longer update of the board: The sketch is compiled and then fully loaded onto the microcontroller – which can take quite a long time.

CircuitPython is different: When you connect your board to your computer via USB, it is recognized as a drive. The Python code can be found in the file code.py – you can theoretically open and modify this file with any text editor. Once you have saved your changes, your board will automatically reset itself and run with the new code – a matter of seconds.

The big difference here is that C++ is compiled and Python (also CircuitPython) is interpreted. Pardon? In order not to go beyond the scope of this article, please read more about compiling and interpreting hereif you are interested in the topic.

The REPL

That sounds pretty fast, but it goes even faster. If your board is running CircuitPython, you can communicate with it via the REPL (read-eval-print loop). This means you can send commands directly to your microcontroller, which it then executes immediately.

This is especially handy for debugging, i.e. to quickly try out if a LED is connected correctly or if a function works the way you want it to. With the Arduino IDE and C++ this can be frustrating at times, because you have to recompile and upload everything again even for small changes in the sketch.

The REPL in the Mu Editor with CircuitPython

Later in this article you will get to know the Mu Editor. Then we will take a closer look at the REPL.

And the disadvantages?

Of course, the use of CircuitPython also has disadvantages. A more important one is the support with libraries: Adafruit and the community have already published suitable libraries for many sensors and components and of course provide them for free. But they don’t come close to the amount of libraries available in the Arduino IDE yet.

However, since CircuitPython is a relatively young technology, we are convinced that this will change over time. Adafruit already regularly releases new bundles of libraries that make your life easier.

Speaking of speed: CircuitPython has a higher development speed, but programs can run slower than compiled code in C++. But this difference should not be important for most hobby applications.

The matching microcontroller

The easiest way to experiment with CircuitPython and microcontrollers is to buy a specially designed board. Adafruit has produced an almost overwhelming amount of different boards in many sizes and for many applications.

You can also get CircuitPython to run on some Arduino boards like the Zero or MKR Zero. You can read more about this in the Adafruit documentation.

In this article we use the board Metro M0 Express from Adafruit. It has the same dimensions as an Arduino UNO and has a similar number of analog and digital pins. It also has 4 LEDs and a NeoPixel on board and has 2MB memory. Enough space for your code, files and libraries.

All boards that support CircuitPython can be found in an overview from Adafruit.

Preparations

You decided to get started with CircuitPython? Great! But before you start, you have to spend some time in preparation.

First you should download and install the appropriate firmware for your board, the latest libraries and a compatible editor.

The Firmware

Search for your board in the Adafruit overview and click on it. Then you will see the available versions of the firmware. Make sure to download the latest stable version – in our example version 5.3.1

Firmware Metro M0 Express

After downloading, connect your board via USB to your computer. Next you have to put it into boot mode: You do this by pressing the reset button on the board twice in quick succession.

Adafruit points out that this should be done at the “right” speed – however, if it worked, you will find a new drive in the Explorer (or Finder on your Mac). The name of the drive consists of the board name and “BOOT”. So in our case it is called METROBOOT.

The board in boot mode in the Finder

Now drag the file you downloaded earlier to this drive. This will install the firmware. After a few seconds the drive should eject automatically and another one called CIRCUITPY should appear.

If this is the case, everything has worked and your board is ready. 🙂 If it doesn’t work, have a look at the documentation of Adafruit – here you will find some more help.

The libraries

Now that your board is ready, the libraries are still missing. As mentioned in the beginning, there is already a library for many applications that will do the work for you.

Adafruit offers whole bundles that you can download here. Download the bundle that corresponds to your firmware version from there. If you have installed a firmware version 5.x you need the library bundle with the same version number.

Unzip the downloaded file and open the folder. In the subfolder lib you will now find all available libraries. Choose the ones you need and copy them into the folder of the same name in the CIRCUITPY drive. If your board has enough memory, you can also copy all the libraries over there.

Libraries for CircuitPython on the Metro M0 Express

If you encounter problems, consult Adafruit’s documentation on libraries. Quite a few special cases are discussed here.

Examples

From the Arduino IDE you surely know the examples that come with most libraries. These are quite useful to find out what functions a library offers and how to use them.

These examples are also available for CircuitPython. On the page where you downloaded the library bundle, you can download a ZIP file with examples under Bundle Examples.

How you execute these examples on your board, we will see in a moment. But first of all the appropriate editor is still missing.

The Mu Editor

To write code in CircuitPython you need an editor, of course. In principle, the simplest text editor is enough – but we would like to recommend the Mu Editor. It’s free, beginner-friendly and designed for CircuitPython. You will find a Serial Monitor and Plotter like you are probably already used to from the Arduino IDE. You can also use the REPL described above.

Download the latest version here from the official website for your operating system.

First Steps

Now open the Mu Editor. Next we will have a look at the most important functions. After that you will write your first code in Python and get it running on your board.

Interface of the Mu Editor

In the upper area you will find a bar with functions:

Mode: Here you can set for which purpose you want to use the Mu Editor. Ours is called Adafruit CircuitPython.

New: Creates a new empty file.

Load & Save: That is certainly clear. 🙂 A note in advance: If you want your board to start your code automatically, you have to use the filename cody.py.

Serial & Plotter: You surely know these two from the Arduino IDE. Here you can view data as usual and output statements that help you with debugging.

Check:Here you can check your code for errors.

Open and use the REPL

To open the REPL and interact directly with your board, first open the Serial Monitor by clicking the corresponding button above. Then press Ctrl + C at the same time and then any key. Now the REPL opens:

The REPL in the Mu Editor

Time for a short test: Copy the following code, paste it into your REPL and press Enter:

import board
import digitalio
import time
 
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

Now you can directly switch on the onboard LED by typing the following and pressing Enter again

led.value = True

Did it turn on? Then try to turn it off again by setting led.value to False. Note: On most compatible boards you can control the internal LED via pin D13. But on some of them it is D17 and a few don’t have an internal LED.

How to save your program on the board

The REPL is great for testing something quickly. But in most cases you will write a regular program in CircuitPython and save it directly on your board.

Your board will automatically search for a file named code.py. If it does not find one, it looks for the file main.py and executes it. This means:Save the program to be executed under the name code.py directly on the CIRCUITPY drive of your board.

Whenever your board detects a change here – because you have updated and saved the code in Mu Editor – it reboots and executes the new code. This usually takes only a few seconds.

Your first program in CircuitPython

Now it’s time to write your first program, save it on your board and execute it. Also for this example a blinking LED should be enough.

Grab a breadboard, a LED and a resistor and set up the following:

A first test with CircuitPython and Adafruit Metro Express

Now copy the following code into an empty file in your Mu Editor and save it as code.py

import board
import digitalio
import time
 
led = digitalio.DigitalInOut(board.D1)
led.direction = digitalio.Direction.OUTPUT
 
while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

Your LED should now turn on and off every half second. Now let’s have a look at the different parts of the code.

First you include three libraries as you know it from the Arduino IDE. Many libraries are stored in extra files and have to be saved to the lib folder on your board before you can use them.

However, the three libraries in this example are part of CircuitPython. This means that you do not have to do anything else before you can use them, except to include them:

import board
import digitalio
import time

You need the library board to access your hardware, digitalio takes care of the inputs and outputs of the board and with time you can “put your program to sleep” – similar to what you do with delay() in C++.

Then you set the LED pin (D1) and its “direction” (OUPUT). You know this from the Arduino IDE from the function pinMode().

led = digitalio.DigitalInOut(board.D1)
led.direction = digitalio.Direction.OUTPUT

Finally, you create a While-Loop that runs continuously. Here you switch on the LED with led.value = True, wait half a second and switch it off for another half second.

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

Play around with the values to see how the intervals of the LED change. When you save your program, your changes should be visible after a few seconds.

What’s next?

You now have everything you need to get started with CircuitPython. Time for your experiments. Next, check out the libraries to see which of your components and sensors you might find suitable. Then take a look at the sample codes that come with each library and start your adventure with CircuitPython!

Letzte Aktualisierung am 2021-02-28 / Affiliate Links / Bilder von der Amazon Product Advertising API