CircuitPython Tutorial: CircuitPython Install and Examples

게시자

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.

최신 뉴스

Sorry, your filter selection returned no results.

개인정보 보호정책이 업데이트되었습니다. 잠시 시간을 내어 변경사항을 검토하시기 바랍니다. 동의를 클릭하면 Arrow Electronics 개인정보 보호정책 및 이용 조건에 동의하는 것입니다.

당사의 웹사이트에서는 사용자의 경험 향상과 사이트 개선을 위해 사용자의 기기에 쿠키를 저장합니다. 당사에서 사용하는 쿠키 및 쿠키 비활성화 방법에 대해 자세히 알아보십시오. 쿠키와 추적 기술은 마케팅 목적으로 사용될 수 있습니다. '동의'를 클릭하면 기기에 쿠키를 배치하고 추적 기술을 사용하는 데 동의하는 것입니다. 쿠키 및 추적 기술을 해제하는 방법에 대한 자세한 내용과 지침을 알아보려면 아래의 '자세히 알아보기'를 클릭하십시오. 쿠키 및 추적 기술 수락은 사용자의 자발적 선택이지만, 웹사이트가 제대로 작동하지 않을 수 있으며 사용자와 관련이 적은 광고가 표시될 수 있습니다. Arrow는 사용자의 개인정보를 존중합니다. 여기에서 당사의 개인정보 보호정책을 읽을 수 있습니다.