CircuitPython Tutorial: CircuitPython Install and Examples

Pubblicato da

In the process of programming a microcontroller, many users turn to Arduino's C-based programming language. But if you've never programmed a microcontroller before, you won't have the Arduino IDE installed, nor are you likely to have PlatformIO or other programming alternatives. If you're one of these users, you may find Python―purported to be the fastest-growing programming language―to be a more familiar and easy-to-learn language than C.

Thanks to the efforts of Scott Shawcroft and other contributors, Adafruit developed a Python-based alternative programming language called CircuitPython. When you plug a CircuitPython-enabled board into your computer, you'll see a drive with the contents of its storage, which you can program with a simple text file named "code.py." CircuitPython, which is related to MicroPython, is a highly compatible version of Python with emphasis on usability.

Programming CircuitPython

Caption: CircuitPython code in Mu, Notepad++, and the standard Windows Notepad

To get started, plug your CircuitPython board into a computer and use a text editor to edit or create code.py. You can use Windows Notepad or advanced editors like Notepad++, but Mu is the officially recommended editor. If you connect your board and click Mu's 'Load' button, the editor automatically takes you to the device's directory where code and other files are stored. Mu also features a built-in serial console, which can be useful.

I feel more at home using Notepad++ and PuTTY for my serial interface. Just open the file, modify what you need, and save it. The new code will begin running after it loads, though it's always a good idea to back up your work off-board.

CircuitPython Example Codes: "Hello World" Blinky and others

With a new board in-hand, the first step is to show that you can interface with it by blinking an LED.  

You'll find the actual "blinky" sketch in the next section, CircuitPython Digital In & Out. This code features options for several CircuitPython boards, and blinks in response to a digital input pin, making it more advanced than just blinking the built-in LED on a timer. Unfortunately, I didn't see an option for the device I was using, the Adafruit Edge Badge.

Adafruit Edge Badge

Caption: LED connections to pin 13 and GND. Note the ATSAMD51J19 microcontroller, which powers the badge.

The Adafruit Edge Badge features a small screen to display your name and other credentials prominently. The device also contains several buttons, a row of five RGB LEDs, and less-obvious features such as a speaker, microphone, and accelerometer. The Edge Badge can even run TensorFlow Lite for Microcontrollers.

My Adafruit Edge Badge came with a demo that displays your name, info, and even a QR code, but running the "blink" demo meant accessing the GPIO pins broken out on the back. I installed an LED/resistor across pin 13 and ground, then modified the Digital In & Out code to blink with the built-in button A instead of another input. If you just want to make the LED blink on and off on a timer, use this straightforward piece of code. Copy the routine into code.py and include any resources noted in the comments.

Serial Interface Read-Eval-Print-Loop (REPL)

In addition to running a stored program, CircuitPython also features an REPL (Read-Evaluate-Print-Loop) interface that's accessible over serial. According to Adafruit, CircuitPython "allows you to enter individual lines of code and have them run immediately." To log on via serial, open the Mu editor and click the serial icon. You can also use PuTTY or another serial tool of your choice.

One important caveat is that REPL won't work if you have a program with no serial output running. You'll need to either delete your code or rename it to something that's not code.py. Once you put code.py back on the system, reset the board to get your code running again.

The REPL allows you to create a print statement―i.e., print("hello")―to display the text. This statement is conveniently displayed on your badge screen as well if you're using this type of device. Creating a print statement and similar commands would be a solid introduction into basic programming concepts, and it's essential if you have an issue with the file system.

How to Troubleshoot and Recover CircuitPython

Unfortunately, CircuitPython has its weaknesses. If your USB connection is interrupted as you attempt to load a new program, your data may be corrupted. It can also take a while (around 25 seconds in my very limited testing) for a newly loaded program to begin running, so don't be too quick to restart.

If you do prematurely restart a program, CircuitPython offers helpful troubleshooting instructions:

1. Log on via serial, as noted earlier.

2. Enter import storage, then storage.erase_filesystem().

You should be able to copy files onto your board again, but this process will also erase everything stored in memory. Back up your data off-board regularly so you can pop the proper files back onto it when needed.

CircuitPython: A New Standard?

As of now, we mostly see CircuitPython implemented on Adafruit products that come with it pre-installed. However, you can install CircuitPython on other SAMD21-based boards, like the Arduino MKRZero. At one point, Adafruit supported the popular ESP8266 module as well, though they don't anymore.

The applications and documentation for this programming paradigm may not be as developed as Arduino's C-based environment, but CircuitPython is still extremely young. In fact, its first release was in the summer of 2017. As Python's popularity increases, we're excited to watch this ecosystem develop. Because CircuitPython doesn't require special software, it will likely be attractive to beginner programmers, especially in educational environments.

Ultime notizie

Sorry, your filter selection returned no results.

Non perderti le ultime novità sull'elettronica

Abbiamo aggiornato la nostra politica sulla privacy. Si prega di prendere un momento per rivedere questi cambiamenti. Cliccando su Accetto, l'utente accetta la Politica sulla privacy e Condizioni di utilizzo di Arrow Electronics.

Il nostro sito web mette i cookies sul vostro dispositivo per migliorare la vostra esperienza e il nostro sito. Leggete altre informazioni sui cookies che usiamo e su come disabilitarli qui. I cookies e le tecnologie di tracking possono essere usati per scopi commerciali.

Con un click su “Accept”, voi consentite l'inserimento dei cookies sul vostro dispositivo e l'uso da parte nostra di tecnologie di tracking. Per avere altre informazioni e istruzioni su come disabilitare i cookies e le tecnologie di tracking, clickate su “Read More” qui sotto. Mentre l'accettazione dei cookies e delle tecnologie di tracking è volontaria, una loro disabilitazione potrebbe determinare un funzionamento non corretto del sito web, ed alcuni messaggi di allarme potrebbero essere per voi meno importanti.

Noi rispettiamo la vostra privacy. Leggete qui la nostra politica relativa alla privacy