In this video lesson we show how neopixels work at a fundamental level, and show how you can use the Raspberry Pi Pico State Machines to control the neoixels. Neopixels are an excellent example for showing the importance of the pico state machine. The neopixels demand precise timing and sequence of bits to operate properly, and that precision can not be achieved in python. The video explains how the State Machines can be programmed to control the neopixels.
For your convenience, the code is included below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import rp2 from machine import Pin import time @rp2.asm_pio(sideset_init=rp2.PIO.OUT_HIGH, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24) def ws2812(): wrap_target() label("bitloop") out(x,1).side(0) jmp(not_x,"do_zero").side(1) nop().side(1)[5-1] nop().side(0)[2-1] jmp("bitloop").side(0) label("do_zero") nop().side(1)[2-1] jmp("bitloop").side(0)[6-1] wrap() sm=rp2.StateMachine(0,ws2812,freq=8000000,sideset_base=Pin(0)) sm.active(1) def write_neopixel(colors): for color in colors: grb=color[1]<<16 | color[0]<<8 | color[2] sm.put(grb,8) NUM_LEDS =8 myColor=[0]*NUM_LEDS myColor=[[100,0,0], [0,100,0], [0,0,100], [0,100,100], [100,0,100], [100,100,0], [200,100,0], [255,255,255]] write_neopixel(myColor) |