# Todbot's NeoPixel / DotStar LED Tricks

## Moving rainbow on built-in `board.NEOPIXEL`

In CircuitPython 7, the `rainbowio` module has a `colorwheel()` function. Unfortunately, the `rainbowio` module is not available in all builds. In CircuitPython 6, `colorwheel()` is a built-in function part of `_pixelbuf` or `adafruit_pypixelbuf`.

The `colorwheel()` function takes a single value 0-255 hue and returns an `(R,G,B)` tuple given a single 0-255 hue. It's not a full `HSV_to_RGB()` function but often all you need is "hue to RGB", where you assume saturation=255 and value=255. It can be used with `neopixel`, `adafruit_dotstar`, or any place you need a (R,G,B) 3-byte tuple. Here's one way to use it.

```# CircuitPython 7 with or without rainbowio module
import time, board, neopixel
try:
from rainbowio import colorwheel
except:
def colorwheel(pos):
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)

led = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.4)
while True:
led.fill( colorwheel((time.monotonic()*50)%255) )
time.sleep(0.05)```
```# CircuitPython 6
import time
import board
import neopixel
led = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.4)
while True:
led.fill( neopixel._pixelbuf.colorwheel((time.monotonic()*50)%255) )
time.sleep(0.05)```

## Make moving rainbow gradient across an LED strip

```import time, random
import board, neopixel, rainbowio
num_leds = 16
leds = neopixel.NeoPixel(board.D2, num_leds, brightness=0.4, auto_write=False )
delta_hue = 256//num_leds
speed = 10  # higher numbers = faster rainbow spinning
i=0
while True:
for l in range(len(leds)):
leds[l] = rainbowio.colorwheel( int(i*speed + l * delta_hue) % 255  )
leds.show()  # only write to LEDs after updating them all
i = (i+1) % 255
time.sleep(0.05)```

## Fade all LEDs by amount for chase effects

```import time, random
import board, neopixel
num_leds = 16
leds = neopixel.NeoPixel(board.D2, num_leds, brightness=0.4, auto_write=False )
my_color = (55,200,230)
dim_by = 20  # dim amount, higher = shorter tails
pos = 0
while True:
leds[pos] = my_color
leds[0:] = [[max(i-dim_by,0) for i in l] for l in leds] # dim all by (dim_by,dim_by,dim_by)
pos = (pos+1) % num_leds  # move to next position
leds.show()  # only write to LEDs after updating them all
time.sleep(0.05)```

This guide was first published on Apr 02, 2022. It was last updated on Apr 02, 2022.

This page (NeoPixels / Dotstars) was last updated on May 24, 2022.