# Example using PIO to drive a set of WS2812 LEDs. # Drive 12 NeoPixels connected to pin 12 # Uses in-line assembly to drive the NeoPixels import array, time from machine import Pin import rp2 # Configure the number of WS2812 LEDs. 8 NUM_LEDS = 8 NUM_LEDS = 12 PIN_NUM = 12 @rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True,pull_thresh=24) def ws2812(): T1 = 2 T2 = 5 T3 = 3 wrap_target() label("bitloop") out(x, 1) .side(0) [T3 - 1] jmp(not_x, "do_zero") .side(1) [T1 - 1] jmp("bitloop") .side(1) [T2 - 1] label("do_zero") nop() .side(0) [T2 - 1] wrap() # Create the StateMachine with the ws2812 program, outputting on Pin(12). sm = rp2.StateMachine(0, ws2812, freq=8_000_000, sideset_base=Pin(PIN_NUM)) # Start the StateMachine, it will wait for data on its FIFO. sm.active(1) r = 50 g = 0 b = 0 while(True): while(r>0): r = r - 1 g = g + 1 b = 0 ar = (g<<16) + (r<<8) + b for i in range(0, NUM_LEDS): sm.put(ar, 8) time.sleep_ms(10) while(g>0): g = g - 1 b = b + 1 r = 0 ar = (g<<16) + (r<<8) + b for i in range(0, NUM_LEDS): sm.put(ar, 8) time.sleep_ms(10) while(b>0): b = b - 1 r = r + 1 g = 0 ar = (g<<16) + (r<<8) + b for i in range(0, NUM_LEDS): sm.put(ar, 8) time.sleep_ms(10)