TranslateProject/sources/tech/20190717 Start tinkering with the Circuit Playground Express.md
DarkSun 1f5d0f0e06 选题: 20190717 Start tinkering with the Circuit Playground Express
sources/tech/20190717 Start tinkering with the Circuit Playground Express.md
2019-07-18 00:53:43 +08:00

7.6 KiB

Start tinkering with the Circuit Playground Express

Learn what you can do with these tiny gadgets and a bit of Python code. Tools in a cloud

I've been a gadget person as long as I can remember, so I was delighted when I discovered an Adafruit Circuit Playground Express (CPX) in the swag bag I got at PyConUS in May. I became fascinated with these little devices last year, when Nina Zakharenko highlighted them in her All Things Open presentation, Five Things You Didn't Know Python Could Do, with Python-powered earrings.

After finding one in my PyCon bag, I set out to learn more about these mesmerizing little devices. First, I attended a "how-to" session at one of the Open Spaces meetups at PyCon. But learning always requires hands-on practice, and that's what I did when I got home. I connected the CPX device to my Linux laptop with a USB-to-MicroUSB cable. The unit mounts just like any standard USB drive, listed as CIRCUITPY.

Circuit Playground Express mounted as USB drive

The CPX works on MacOS, Windows, and Linux (including Chromebooks). The device comes pre-loaded with code and some sound files. Adafruit's extremely well-written documentation answered most of my questions. I discovered the unit can be programmed on Linux three different ways: MakeCode, the Arduino IDE, and the Python-based CircuitPython, which I chose.

Adafruit provides excellent documentation for creating and editing CircuitPython code, which I found helpful. You can use a variety of editors (e.g., Emacs, Visual Studio Code, gedit), but Adafruit recommends the Mu Python editor, which I wrote about last year. I installed Mu on my system with pip3 install --user mu-editor. Then I opened a terminal and entered mu-editor. It asked me how to run Mu, and I chose Adafruit Circuit Python. Then I was able to look at the code that powers the CPX.

Selecting CircuitPython mode to run Mu

To open a connection between Mu and the CPX connected to your computer, press the Serial button in Mu. Then you can see any serial data from the CPX and edit it using Python's REPL shell.

Adafruit's programmers have written a library called adafruit_circuitplayground.express that enables CircuitPython to interact with the CPX board. To use it, add import adafruit.circuitplayground.express to your code. Or, to make it simpler, you can use the acronym cpx, shortening the code (as shown below) to from adafruit_circuitplayground.express import cpx.

Importing Adafruit's CPX library

The way you name your file is essential. The four options are code.txt, code.py, main.txt, and main.py. CircuitPython looks for the code files in that order and runs the first one it finds. Save the code to your CIRCUITPY drive each time you change it.

The main.py code included with a new CPX offers an example of the device's capabilities.

CPX's default main.py

When you execute this code, the CPX displays beautiful, brightly colored LEDs whirling in a rainbow of colors. With my rudimentary knowledge, I could tweak a few settings, like increasing the brightness and turning on the TOUCH_PIANO capability, but other modifications were beyond my coding ability at this point.

Eager to do more, I wanted to find code snippets I could use as building blocks to learn. First, I reached out to Nina Zakharenko on Twitter and asked for some help. She recommended I contact Kattni Rembor, who pointed me to her GitHub repo and some code examples she wrote for the Chicago Linux User Group.

Each of these simple building blocks left me more confident in my Python journey. In addition to making lights blink, the CPX can also function as a sensor, and I wanted to try that. Here is code for a simple light sensor:

CPX code for a blinking LED

And here is the CPX with the D13 LED blinking:

CPX with a blinking LED

I also discovered a way to create some fun for my grandson by making the CPX "come to life." I recorded a couple of .wav files with Audacity and saved them to the device. Then I wrote some simple code that utilized the A and B buttons on the device to make the CPX "talk" to him:

Code to play a sound when a button is pressed on CPX

I've really enjoyed tinkering with the code to explore the CPX's capabilities. I am always looking for ways to make Python code come alive for students I teach. The CPX is a great way to help new users learn and enjoy coding and digital making. Another excellent resource for new users is Mike Barela's book Getting Started with Adafruit Circuit Playground Express. I found its information and examples very helpful as I was learning.

Get a Circuit Playground Express and start writing your own code. And then please share how you are using it in the comments.


via: https://opensource.com/article/19/7/circuit-playground-express

作者:Don Watkins 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出