CircuitPython's rotaryio module makes it easy to read the rotary encoder rotation. Along with the Adafruit CircuitPython NeoPixel library, you can easily create a NeoPixel color picker using the rotary encoder and the built-in NeoPixel LED.

All the necessary modules and libraries for this example are included with CircuitPython for the Rotary Trinkey, so you do not need to load any separate files onto your board.

NeoPixel and Rotary Encoder Location

The rotary encoder (highlighted in red) is on the top of the board. The Rotary Trinkey does not come with an encoder! You must provide and solder on your own.

The NeoPixel LED (highlighted in green) is located on the bottom of the board towards the opposite end from the USB connector.

Controlling the NeoPixel Color and Brightness

In the example below, click the Download Project Bundle button below to download the necessary files in a zip file. Extract the contents of the zip file, open the directory Rotary_Trinkey/CircuitPython_ColorPicker_Example/ and then click on the directory that matches the version of CircuitPython you're using and copy to your CIRCUITPY drive.

You do not need to copy the entire Project Bundle to your board - all the necessary libraries are included in CircuitPython for Rotary Trinkey!
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Rotary Trinkey NeoPixel color picker example"""
import rotaryio
import digitalio
import board
from rainbowio import colorwheel
import neopixel

print("Rotary Trinkey color picker example")

pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.5)
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
switch = digitalio.DigitalInOut(board.SWITCH)

last_position = -1
color = 0  # start at red
while True:
    position = encoder.position
    if last_position is None or position != last_position:
        if not switch.value:
            # change the color
            if position > last_position:  # increase
                color += 1
                color -= 1
            color = (color + 256) % 256  # wrap around to 0-256
            # change the brightness
            if position > last_position:  # increase
                pixel.brightness = min(1.0, pixel.brightness + 0.1)
                pixel.brightness = max(0, pixel.brightness - 0.1)
    last_position = position

Rotate the rotary encoder to change the color of the NeoPixel LED. Press the button on the rotary encoder and rotate while holding it down to change the brightness of the NeoPixel LED.

First, you import the necessary modules and library. Then you setup the NeoPixel LED, the rotary encoder, and the rotary encoder button switch.

Before the loop, you choose a value for the last_position variable, and set it, and you set color = 0 so the NeoPixel starts as red.

Inside the loop, you take a reading of the encoder position. Then, you check whether the encoder has moved in any direction, and if so, print the position. This position is relative to the starting position, so if you want a particular position to represent "0", then make sure your rotary encoder is in that physical position when you plug it in.

If the button is not pushed, and the rotary encoder is rotated, the code cycles through the rainbow colors on the NeoPixel LED forward or backward depending on whether the rotation is clockwise or counter-clockwise respectively. Forward cycles from red to green to blue and back to red, and backward cycles from red to blue to green and back to red.

If the button is pushed, and the rotary encoder is rotated, the brightness of the NeoPixel LED increases or decreases depending on clockwise or counter-clockwise rotation respectively.

That's all there is to using CircuitPython to make a NeoPixel LED color picker using the Rotary Trinkey!

This guide was first published on Jun 02, 2021. It was last updated on Oct 21, 2017.

This page (NeoPixel Color Picker) was last updated on Nov 30, 2023.

Text editor powered by tinymce.