CircuitPython

CircuitPython is Adafruit's branch of MicroPython designed to simplify experimentation and education on modern low-cost microcontrollers. It makes it easier than ever to get prototyping by requiring no upfront desktop software downloads.

With CircuitPython you can write clean and simple Python code to control hardware instead of having to use complex low-level languages like C or C++ (what Arduino uses for programming). It's great for beginners!

The simplicity of the Python programming language makes CircuitPython an excellent choice for beginners who are new to programming and hardware. CircuitPython is also quite full-featured and supports all of Python's syntax (Python version 3.4) and implements a small subset of the Python standard library so even seasoned Python veterans will find CircuitPython familiar and fun to use.

Setting Up

Some of our latest boards ship from the factory with CircuitPython preinstalled. For those that don’t, or if you’ve overwritten CircuitPython with an Arduino sketch and want to get it back, you’ll find setup directions in the Adafruit Learning System guide specific to each board:

For other boards (Feather HUZZAH, Feather M0 Basic and Arduino Zero), check the CircuitPython Github repository for the latest setup notes.

Here's an example with a lot of different visual effects you can check out. You'll need the neopixel.mpy library file if you don't have it yet!

# CircuitPython demo - NeoPixel

import board
import neopixel
import time

pixpin = board.D1
numpix = 10

strip = neopixel.NeoPixel(pixpin, numpix, brightness=0.3, auto_write=False)


def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0) or (pos > 255):
        return (0, 0, 0)
    if (pos < 85):
        return (int(pos * 3), int(255 - (pos*3)), 0)
    elif (pos < 170):
        pos -= 85
        return (int(255 - pos*3), 0, int(pos*3))
    else:
        pos -= 170
        return (0, int(pos*3), int(255 - pos*3))

def rainbow_cycle(wait):
    for j in range(255):
        for i in range(len(strip)):
            idx = int ((i * 256 / len(strip)) + j)
            strip[i] = wheel(idx & 255)
        strip.write()
        time.sleep(wait)

while True:
    strip.fill((255, 0, 0))
    strip.write()
    time.sleep(1)

    strip.fill((0, 255, 0))
    strip.write()
    time.sleep(1)

    strip.fill((0, 0, 255))
    strip.write()
    time.sleep(1)

    rainbow_cycle(0.001)    # rainbowcycle with 1ms delay per step

When declaring a NeoPixel object in CircuitPython, the object's argument list requires the output pin you'll use and the number of pixels. There's two optional arguments, brightness (range from 0 (off) to 1.0 (full brightness)) and auto_write. When auto_write is set to True, every pixel change is immediately written to the strip…this is easier to use but way slower. If you set auto_write=False then you will  have to call strip.show() when you want to actually write color data out.

You can easily set colors by indexing into the location strip[n] = (red, green, blue). For example, strip[0] = (100, 0, 0) will set the first pixel to a medium-brightness red, and strip[2] = (0, 255, 0) will set the third pixel to bright green. Then, if you have auto_write=Falsedon't forget to call strip.show() .

Last updated on 2017-10-12 at 11.10.05 PM Published on 2013-08-30 at 05.06.47 PM