Fun with LEDs and CircuitPython
Nina Zakharenko has been programming for a long time; when she was young she thought that "the idea that I could trick computers into doing what I tell them was pretty awesome". But as she joined the workforce, her opportunities for "creative coding" faded away; she regained some of that working with open source, but tinkering with hardware is what let her creativity "truly explode". It has taken her years to get back what she learned long ago, she said, and her keynote at PyCon 2019 was meant to show attendees the kinds of things can be built with Python—starting with something that attendees would find in their swag bag.
As part of her shift in thinking, she realized that "software doesn't have to be serious"; it can be used to make art, for example. But she also realized that hardware doesn't need to be serious either, putting up a clip from a YouTube video of "The Breakfast Machine" created by "an incredible maker", Simone Giertz. She showed pictures of some of her own projects (which can be seen in her Speaker Deck slides), such as an Arduino-based iridescent LED headdress.
![Glittery shoes [Glittery shoes]](https://static.lwn.net/images/2019/pycon-shoes-sm.jpg)
She described herself as a software engineer, who has worked for over a decade at some large companies "that you may have heard of", such as HBO, MeetUp, and Reddit. She is now at Microsoft as a senior cloud-developer advocate; her goal there is to make Visual Studio Code (VSCode) and Azure "a pleasure to use for Python developers". This was her seventh PyCon, Zakharenko said, and it is a "dream come true" to be on stage at PyCon "sharing what I am passionate about with all of you".
Python on hardware
She had already mentioned Arduino code, which is written in a C++ variant, that she had used for some of her projects. But C++ is hard to learn and error-prone, she said. We know from experience that Python is the opposite; it is easy to learn and beginner friendly. "We want to be able to program devices with Python". Luckily, she said, in each attendee's swag bag, they should find a Circuit Playground Express device. "I am going to show you how to program it."
She then did a live demo of the device, using a camera to display the device itself while typing Python at it in another window. One of the nice things about the device is how easy it is to get started writing programs for it. Hooking it up to a computer via USB provides power to it, a mountable storage device where programs can be copied to run on it, and a serial console. The console is where program output can be seen and the read-eval-print loop (REPL) can be accessed. She started by copying a five-line program to the mounted device as code.py; the device immediately ran the code resulting in the ten multi-color LEDs all being set to red. The program looked like the following:
from adafruit_circuitplayground.express import cpx RED = (255, 0, 0) cpx.pixels.brightness = 0.1 while True: cpx.pixels.fill(RED)With a small change to the code, she reloaded it and all the LEDs were set to green. "Easy peasy." As might be guessed, the change she made was to add a line below the RED definition:
GREEN = (0, 255, 0)She also changed the fill() call to use GREEN.
There are other devices that run Python, she said, including the Raspberry Pi Zero W, BBC micro:bit, and the Adafruit M0 and M4. Adafruit is also behind the Circuit Playground Express; she is big fan of the company, which produces low-cost, well-documented products for makers.
MicroPython (which LWN looked at back in 2015) is a variant of Python 3 that targets microcontrollers; it is compact enough to fit in 256KB of code space and run with 16KB of RAM. CircuitPython is an education-friendly fork of MicroPython created by Adafruit; it is what runs on the Playground Express device. Importantly, she said, both are open source.
Circuit Playground Express
The Circuit Playground Express is a $25 device that is meant to be a learning platform. Depending on where people get their devices, it may not have CircuitPython preinstalled, but that is not a problem for PyCon attendees as those in the swag bags are Python-ready. PEP 206 describes Python's "batteries included" philosophy, which means that Python has a rich standard library right out of the box (though it should be noted that there are some current efforts to potentially rethink the idea, which arose in a session at the 2018 Python Language Summit).
While the Playground Express device doesn't actually come with batteries, Zakharenko said with a grin, the philosophy behind it is the same. It comes with "everything you need to get started programming hardware on one board". She gave a bit of a tour of the board by pointing to various peripherals that it has. There are the ten NeoPixel multi-color LEDs, two buttons, a slide switch, a speaker and microphone, light and temperature sensors, and an accelerometer, all of which are accessible from CircuitPython.
![Nina Zakharenko [Nina Zakharenko]](https://static.lwn.net/images/2019/pycon-zakharenko-sm.jpg)
Programming the colors of the LEDs is straightforward, as long as you understand some basics of the color wheel, she said. Each LED takes a tuple of three values, from zero to 255, that represents the amount of red, green, and blue to display. Using that, you can display over 16-million colors on each of the ten NeoPixels. The LEDs are made up of three LED elements, one for each color; the eye mixes the colors that are produced by the elements to make a single color. She showed the operation of a NeoPixel from a microscope to illustrate how it worked.
It should be noted that not all micro-USB cables support both charging and data, she said, so it is important to use one that does both. The USB "drive" that shows up when the device is plugged in needs to be mounted. Then the code.py (or main.py) file in the root directory can be edited (or copied over) and the code will be reloaded and run whenever that file changes. She stepped through her original example, explaining each line; the import statement gets the Adafruit library that provides easy access to the hardware. She noted that the pixel brightness is high by default, so setting cpx.pixels.brightness to a lower value (0.1 in the example) is helpful. The infinite loop is there because the fill() function simply lights the LEDs once; for persistence they need to be refreshed continuously.
Demos
She then moved on to a more complicated example that used the buttons to control which color would display on the pixels. She loaded the code from a file on her laptop; the relevant section looked like:
if cpx.button_a: cpx.pixels.fill(RED) elif cpx.button_b: cpx.pixels.fill(BLUE) else: cpx.pixels.fill(0)She demonstrated that code working (it reloaded as soon as she saved it from her editor), then in a bit of a live-coding exercise added a definition for GREEN and some code to use that color if both buttons were pressed. She continued in that vein for a bit, making a version that cycled through the colors based on button presses, which had a simple button debounce mechanism by using time.sleep(0.2). She also added yellow, cyan, and purple (or magenta) as colors.
Any editor can be used, but she cautioned that some editors do not fully write out the file when a save is done, which can cause problems. She uses VSCode, since it is the editor she already uses for Python after she switched from Emacs a few months back (though she still has Emacs keybindings in VSCode). She also pointed to Mu, which is a simple Python editor for beginners.
In order to see any program output, including tracebacks if there are errors in the Python code, you need to connect to the serial console. The easiest way to do so is to use Mu's builtin serial console. A single button in the interface will provide a second pane in its window with the console output. More advanced users may want to use Screen or another terminal program instead. Typing control-c in the terminal will send a keyboard interrupt to the running programming and give access to the REPL; as might be expected control-d will return to running the existing program.
The Playground Express can be powered via USB, but it also has a two-pin JST connector for battery power. It needs 3.5V, so a battery pack that holds 3 AAA batteries can be used for portable power. In addition, advanced users can consider a lithium polymer (LiPo) battery, which are small, light, and energy dense, but are also somewhat fragile; without proper care, they can explode or catch on fire.
She showed another demo using code that had come from another Playground Express device that she had. It had two modes, which could be chosen based on the position of the slide switch. In both modes it individually changed the LED colors in a kind of spinning rainbow, but it would also act as a capacitive touch "piano" in the second mode. Examining the code will show it accessing some of the other peripherals and printing out readings from various sensors.
For those who want to "program" without typing code, she recommended Microsoft MakeCode. It is a block-based programming environment. There is an Adafruit MakeCode site that allows one to use the programming environment without any hardware at all. There is an emulator that can be used to interact with the "device".
Diversity
She gave some statistics based on a survey that the BBC did after it distributed micro:bit devices to schoolchildren in the UK. It found that 90% of students thought that the device showed them that anyone can code; 86% thought that the micro:bit made computer science more interesting. In a statistic that hit close to home, Zakharenko said, 70% more girls said that they would take up computer science as a subject option after using the micro:bit; that brought a round of applause from the audience.
A study called "Unlocking the Clubhouse" suggested that traditional computing culture is a boy's club that is unfriendly to women; it concluded that we need to find ways to unlock this clubhouse to make it accessible. Another study, "LilyPad in the wild", suggested building new clubhouses; rather than trying to fit new people into existing cultures, "it may be more constructive to spark and support new cultures". The idea is to make room for more diverse interests and passions; "diversity does not stop at gender".
She would like to see the "Python on hardware" community encourage everyone to participate, regardless of their gender, ethnicity, socioeconomic status, age, or any of a number of other factors. The overall hardware community is "really great" about sharing what it does with others, providing step-by-step guides to help build a wild assortment of different things. Python on hardware is really just getting started at this point, she said.
She quoted Madison Maxey, the founder of smart-fabric technology maker Loomia, who said: "You can be a creator and artist, not just a consumer". She also mentioned Angela Sheehan, who created infinity mirror heart heels "and a lot more". Zakharenko said that she really looked up to Sheehan as she follows the same passion, "making things that fit her aesthetic". That is the nature of hardware, Zakharenko said, it allows for instantaneous results in the real world, which helps make technology more accessible. She showed one of her favorite pictures: two girls with huge, beaming smiles who were wearing the light-up masks they built as part of a Made By Girls summer camp program. E-textiles, sewable electronics, papercraft, and hardware focused on education all help expose technology in new and unexpected ways, she said.
She noted that eagle-eyed attendees may have noticed that she is wearing one of her projects, pyearrings, which are LED earrings powered by Python. They are made with Adafruit Gemma M0 boards and two different LED rings, along with a LiPo battery. She turned them on to loud applause. They can be seen in the photo above.
In closing, she quoted Lawrence Durrell: "Our inventions mirror our secret wishes." That quote really drives it home, she said: "we want to build things that reflect us". She said that anyone in the room could start on that process using the Circuit Playground Express that they got from the conference sponsors; she is not an expert, just a hobbyist, but you don't need to be an expert.
"I implore you to learn, make things, create, teach others, share your knowledge; it is a fantastic cycle that we can all participate in." She suggested possibly donating the board to someone from a group that is underrepresented in technology and showing them how to get started with it. She recommended using the (almost) unused "#PythonHardware" hashtag to share results and ideas; the tag had only been used once by the time of the keynote, by Raymond Hettinger after getting a preview of Zakharenko's talk.
The device is compelling and fun; after playing around with it for a bit after the conference, it is clear that anyone with an interest could get started with it quickly—especially with a jump start from someone in the community.
A YouTube video of the talk is available.
[I would like to thank LWN's travel sponsor, the Linux Foundation, for
travel assistance to Cleveland for PyCon.]
Index entries for this article | |
---|---|
Conference | PyCon/2019 |
Python | CircuitPython |
Python | Embedded |
Posted Jun 6, 2019 9:04 UTC (Thu)
by zoobab (guest, #9945)
[Link]
All those Adafruit boards are much more expensive.
Posted Jun 16, 2019 15:55 UTC (Sun)
by muwlgr (guest, #35359)
[Link] (1 responses)
Posted Jan 23, 2020 21:39 UTC (Thu)
by mirabilos (subscriber, #84359)
[Link]
Fun with LEDs and CircuitPython
Fun with LEDs and CircuitPython
Fun with LEDs and CircuitPython