CircuitPython DotStar

DotStars use two wires, unlike NeoPixel's one wire. They're very similar but you can write to DotStars much faster with hardware SPI and they have a faster PWM cycle so they are better for light painting.

You can drive 300 pixels with brightness control and 1000 pixels without (set brightness=1.0 in object creation). That's because to adjust the brighness we have to dynamically re-create the datastream each write.

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

The DotStar object's argument list requires the two pins you'll use and the number of pixels. Any pins can be used but if the two pins can form a hardware SPI port, the library will automatically switch over to hardware SPI. If you use hardware SPI then you'll get 4 MHz clock rate (that would mean updating a 64 pixel strand in about 500uS - that's 0.0005 seconds). If you use non-hardware SPI pins you'll drop down to about 3KHz, 1000 times as slow!

On the Gemma M0, if you use adafruit_dotstar.DotStar(board.D2, board.D0...) you'll get hardware SPI

On the Trinket M0, you can use D2 & D0, D2 & D3, D3 & D0,  or D3 & D4

There's two optional arguments, brightness (range from 0 off to 1.0 full brightness) and auto_write. When auto_write default is set to True, where every change is immediately written to the strip of pixels, 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.

# CircuitPython demo - Dotstar

import board
import adafruit_dotstar
import time

numpix = 64
strip = adafruit_dotstar.DotStar(board.D2, board.D0, numpix, brightness=0.2)

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.show()
        time.sleep(wait)

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

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

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

    rainbow_cycle(0.001) # high speed rainbow cycle w/1ms delay per sweep

This code will work with any DotStar-compatible.

DotStars can be driven by any two pins (just slower if they are not hardware pins)

For powering the pixels from the board, the 3.3V regulator output from the Trinket/Gemma M0 can handle about 500mA peak which is about 50 pixels with 'average' use. If you want really bright lights and a lot of pixels, we recommend powering direct from the power source. On the Gemma M0 this is the Vout pad - that pad has direct power from USB or BAT, depending on which is higher voltage. On the Trinket M0 the USB or BAT pins will give you direct power from the USB port or battery.

The DotStar object's argument list requires the 2 pins 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 default is set to True, where every change is immediately written to the strip of pixels, 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() 

Verify the wiring on your strip or device - plugging into the 'DOUT' side is a common mistake! Wire up DotStars only while the Trinket/Gemma is not on, to avoid possible damage!

If the power to the pixels is > 5.5V you may have some difficulty driving some strips, in which case you may need to lower the voltage to 4.5-5V or use a level shifter

We have a ton more information on general purpose DotStar know-how at our DotStar UberGuide https://learn.adafruit.com/adafruit-dotstar-leds
Last updated on 2017-12-01 at 04.59.09 PM Published on 2017-07-26 at 06.08.30 PM