Code: Select all
import machine, time, neopixel
n = 5 #number of led
p = 13 #Pin number on the board
freq = 5000 #frequency
np = neopixel.NeoPixel(machine.Pin(p), n) #neopixel object
led = machine.PWM(machine.Pin(p), freq) #PWM object
while True:
#color rgb
r = 103
g = 202
b = 232
for i in range(n):
np[i] = (r, g, b)
np.write()
This above snipped is (almost) the correct use of the NeoPixel driver.
First, remove the PWM config:
Code: Select all
# This correctly instantiates a NeoPIxel controller
np = neopixel.NeoPixel(machine.Pin(p), n) #neopixel object
# This creates a PWM on the same pin, effectively undoing any specific configuration of the pin required for the NeoPIxel. Remove this line.
led = machine.PWM(machine.Pin(p), freq) #PWM object
Second, unindent the np.write call, it only needs to be called once to send the data to the strip. Filling the array creates the command string for the strip. np.write sends the command string.
Code: Select all
# Fill the array, one color tuple for each LED
for i in range(n):
np[i] = (r, g, b)
# send entire array of colors to LED strip all at once
np.write()
And the "while True" is not doing anything here as you are not changing the color in the while loop.
Do this:
Code: Select all
import machine, time, neopixel
n = 5 #number of led
p = 13 #Pin number on the board
np = neopixel.NeoPixel(machine.Pin(p), n) #neopixel object
#color rgb
r = 103
g = 202
b = 232
for i in range(n):
np[i] = (r, g, b)
np.write()
All of the rest is incorrect for addressable LEDS and unneeded.
Code: Select all
for duty_cycle in range(0, 1024, 10):
led.duty(duty_cycle)
time.sleep(0.05)
for duty_cycle in range(1024, 0, -10):
led.duty(duty_cycle)
time.sleep(0.05)
PWMs control brightness by rapidly turning the LED on and off. Addressible LEDs take commands as each individual LED has a controller on the strip or in the LED. You send them commands only, you cannot control them via PWM.