CircuitPython Code

The Prop-Maker FeatherWing is loaded with features to help with your next build. Here, we'll go over how to use each of the features in CircuitPython. We'll provide a simple example that you can expand on to include it in your project. Save the code to code.py on your board to run the example!

NeoPixels

CircuitPython makes it super simple to use the NeoPIxels connected via the snap-in NeoPixel connector on the Prop-Maker FeatherWing.

This example lights up a 30-pixel NeoPixel strip in a rainbow. Change NUM_PIXELS to match the number of NeoPixels you've connected to your FeatherWing.

"""Simple rainbow example for 30-pixel NeoPixel strip"""
import digitalio
import board
import neopixel

NUM_PIXELS = 30  # NeoPixel strip length (in pixels)

enable = digitalio.DigitalInOut(board.D10)
enable.direction = digitalio.Direction.OUTPUT
enable.value = True

strip = neopixel.NeoPixel(board.D5, NUM_PIXELS, brightness=1)


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 (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)


while True:
    for i in range(255):
        strip.fill((wheel(i)))

For more information, check out the Adafruit NeoPixel Überguide!

Audio

The speaker connector on the Prop-Maker FeatherWing makes it easy to attach a speaker to playback audio for your project.

This example only works on Feathers that have analog audio out, such as the Feather M0 Express or Feather M4.

This example plays once through a wave file called "StreetChicken.wav". Click the button below to download the file and copy it to your board. You can also add any compatible wave file and change the code to match the name of your file.

"""Simple example to play a wave file"""
# This example only works on Feathers that have analog audio out!
import digitalio
import board
import audioio

WAV_FILE_NAME = "StreetChicken.wav"  # Change to the name of your wav file!

enable = digitalio.DigitalInOut(board.D10)
enable.direction = digitalio.Direction.OUTPUT
enable.value = True

with audioio.AudioOut(board.A0) as audio:  # Speaker connector
    wave_file = open(WAV_FILE_NAME, "rb")
    wave = audioio.WaveFile(wave_file)

    audio.play(wave)
    while audio.playing:
        pass

For more information, check out Audio Out in the CircuitPython Essentials Guide!

Accelerometer

You can easily access the acceleration data from the LIS3DH accelerometer in the center of the Prop-Maker FeatherWing using CircuitPython.

This example prints the acceleration data to the serial console. Move the board around to see the values change.

"""Simple example to print acceleration data to console"""
import time
import digitalio
import board
import busio
import adafruit_lis3dh

# Set up accelerometer on I2C bus, 4G range:
i2c = busio.I2C(board.SCL, board.SDA)
int1 = digitalio.DigitalInOut(board.D5)
accel = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
accel.range = adafruit_lis3dh.RANGE_4_G
accel.set_tap(1, 100)

while True:
    x, y, z = accel.acceleration
    print(x, y, z)
    time.sleep(0.1)
    if accel.tapped:
        print("Tapped!")

For more information, check out the LIS3DH guide!

3 Watt LED

You can control the colors and brightness of a 3 Watt RGB LED using CircuitPython.

This example uses PWM to light up the LED with a rainbow swirl.

"""Simple rainbow swirl example for 3W LED"""
import pulseio
import board
import digitalio

enable = digitalio.DigitalInOut(board.D10)
enable.direction = digitalio.Direction.OUTPUT
enable.value = True

red = pulseio.PWMOut(board.D11, duty_cycle=0, frequency=20000)
green = pulseio.PWMOut(board.D12, duty_cycle=0, frequency=20000)
blue = pulseio.PWMOut(board.D13, duty_cycle=0, frequency=20000)


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 (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)


while True:
    for i in range(255):
        r, g, b = wheel(i)
        red.duty_cycle = int(r * 65536 / 256)
        green.duty_cycle = int(g * 65536 / 256)
        blue.duty_cycle = int(b * 65536 / 256)

For more information, check out PWM in the CircuitPython Essentials Guide!

Switch

You can easily use the mechanical switch on the Switch pin with CircuitPython.

The following example prints to the serial console when the switch is closed.

"""Simple example to print when switch is pressed"""
import time
import digitalio
import board

switch = digitalio.DigitalInOut(board.D9)
switch.switch_to_input(pull=digitalio.Pull.UP)

while True:
    if not switch.value:
        print("Switch pressed!")
    time.sleep(0.1)
This guide was first published on Jan 23, 2019. It was last updated on Jan 23, 2019. This page (CircuitPython Code) was last updated on May 23, 2019.