Since we're using a Trinket M0 board, we can also write the code with CircuitPython! 

# SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
# SPDX-License-Identifier: MIT

import time

import board
import neopixel
from analogio import AnalogIn

pot = AnalogIn(board.A1)  # what pin the pot is on
pixpin = board.D0  # what pin the LEDs are on
numpix = 16  # number of LEDs in ring!
BPP = 4  # required for RGBW ring

ring = neopixel.NeoPixel(pixpin, numpix, bpp=BPP, brightness=0.9)

def val(pin):
    # divides voltage (65535) to get a value between 0 and 255
    return pin.value / 257

while True:
    # Two lines for troubleshooting to see analog value in REPL
    # print("A0: %f" % (pot.value / 65535 * 3.3))
    # time.sleep(1)

    # change floating point value to an int
    ring.fill((0, 0, 0, int(val(pot))))

As you can see, the code is very similar to the Arduino IDE code. We're still reading the analog value of Pin 2 (A1), converting it to a value between 0 and 255 and then having that be the value of the white portion of the NeoPixel.

Since we're using an RGBW ring, we need to declare this in the NeoPixel object. This is done using bpp (bytes per pixel). RGBW NeoPixels have 4 BPP, where as RGB NeoPixels have 3 BPP.

The other important point is that analog values in CircuitPython range from 0 to 65535. If you divide 65535 by 257, then it converts the range to 0 to 255 so that it can be read as a digital value for the NeoPixels.

This guide was first published on Feb 04, 2018. It was last updated on Feb 26, 2024.

This page (CircuitPython Code) was last updated on Feb 26, 2024.

Text editor powered by tinymce.