-
Notifications
You must be signed in to change notification settings - Fork 344
neopixel
This class includes full support for various types of Neopixel, individually-addressable RGB(W) LEDs.
ESP32 RMT peripheral is used for very precise timing ( +/- 50 ns ).
Up to 8 Neopixel strips can be used, 1~1024 pixels each.
ESP32 pin | Neopixel | Notes |
---|---|---|
Any output pin | DIN | Neopixel data input pin |
GND | GND | Neopixel ground |
4 - 7V | 5VDC | Neopixel Power |
Make shure the Neopixel Power can supply enough power to drive all connected LEDs.
Do not use ESP32 3.3V
Before using the neopixel, the machine module must be imported and the instance of the Neopixel class has to be created:
pin can be given as pin number or machine.Pin object.
pixels is the number of the pixels, 1 ~ 1024
type Neopixel type: 0 (machine.Neopixel.RGB) for RGB LEDs, or 1 (machine.Neopixel.RGBQ) for RGBW LEDs
import machine
np = machine.Neopixel(22, 115, 0)
npw = machine.Neopixel(machine.Pin(25), 24, machine.Neopixel.RGBW)
color values are given as 24 bit integer numbers, 8-bit per R,G,B color.
For example: 0xFF0000 represents the RED color.
The following color constants are defined and can be used as color arguments:
BLACK, WHITE, RED, LIME, BLUE, YELLOW, CYAN, MAGENTA, SILVER, GRAY, MAROON, OLIVE, GREEN, PURPLE, TEAL, NAVY
For RGBW Neopixels, white argument can be given as integer 0 ~ 255.
Set the color of the pixe at position pos
Argument | Description |
---|---|
pos | required, pixel position; 1 ~ pix_num |
color | required, pixel color; use nuneric value or predifined color constant |
white | optional; default: 0; white level; only for RGBW Neopixels |
num | optional; default: 1; number of pixels to set to the same color, starting from pos |
update | optional, default: True; update the Neopixel strip. If False np.show() has to be used to update the strip |
Get the color of the pixe at position pos For 24-bit RGB Neopixels returns the integer representing the RGB color value. For 32-bit RGBW Neopixels returns the tuple of 24-bit RGB color value and 8-bit while level.
Set the color of the pixe at position pos using hue/saturation/brightness color model
Argument | Description |
---|---|
pos | required, pixel position; 1 ~ pix_num |
hue | float: any number, the floor of this number is subtracted from it to create a fraction between 0 and 1. This fractional number is then multiplied by 360 to produce the hue angle in the HSB color model. |
saturation | float; 0 ~ 1.0 |
brightness | float; 0 ~ 1.0 |
num | optional; default: 1; number of pixels to set to the same color, starting from pos |
update | optional, default: True; update the Neopixel strip. If False np.show() has to be used to update the strip |
Set or get the brightnes factor. Brightnes factor is used to reduce the color level for all the pixels in the strip. Alloved value is 1 to 255.
If no argument is given, the current brightnes factor will be returned. If update=True the Neopixel strip will be updated immediately.
The pixel color is represented as 24-bit RGB value where RED is most significant byte and BLUE the least significant value. Various Neopixels types requires the color bytes to be sent in specific order (for example WS2812 requires "GRB" color order).
The color order can be set to any combination of 'R', 'G', 'B' and 'W' (for RGBW type Neopixels). Without arguments, returns the current color order.
Various Neopixels types may require different timings for encoding the 1 and 0 bits in comunication protocol. When Neopixel object is created, the default timings for WS2812 are used for RGB type and SK6812 timings for RGBW type.
The timings argument must be tuple with the following structure: ((T1H,T1L), (T0H, T0L), Treset). All times are given as ns (nano second) values.
Without arguments, returns the current timings tuple.
Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model. Returns 24-bit integer value suitable to be used as color argiment
Arguments
Argument | Description |
---|---|
hue | float: any number, the floor of this number is subtracted from it to create a fraction between 0 and 1. This fractional number is then multiplied by 360 to produce the hue angle in the HSB color model. |
saturation | float; 0 ~ 1.0 |
brightness | float; 0 ~ 1.0 |
Converts 24-bit integer value to the the components of a color, as specified by the HSB model. Returns tuple: (hue, saturation, brightness)
Update the Neopixel strip.
clear the Neopixel strip.
Deinitialize the Neopixel Object and free all used memory
import machine, time
np=machine.Neopixel(machine.Pin(22), 24)
def rainbow(loops=120, delay=1, sat=1.0, bri=0.2):
for pos in range(0, loops):
for i in range(0, 24):
dHue = 360.0/24*(pos+i);
hue = dHue % 360;
np.setHSB(i, hue, sat, bri, 1, False)
np.show()
if delay > 0:
time.sleep_ms(delay)
def blinkRainbow(loops=10, delay=250):
for pos in range(0, loops):
for i in range(0, 24):
dHue = 360.0/24*(pos+i);
hue = dHue % 360;
np.setHSB(i, hue, 1.0, 0.1, 1, False)
np.show()
time.sleep_ms(delay)
np.clear()
time.sleep_ms(delay)