RGB LED
The mPython Board has 3 onboard WS2812 LEDs. WS2812 is a low-power RGB tri-color LED with an integrated current control chip, capable of 256-level brightness display and 16,777,216 full-color display. It uses a special single-wire communication method to control RGB LED colors, making it simple to use.
Onboard RGB LED
Example: Light up RGB LED
from mpython import *
rgb[0] = (255, 0, 0) # Set to red, full brightness
rgb[1] = (0, 128, 0) # Set to green, half brightness
rgb[2] = (0, 0, 64) # Set to blue, quarter brightness
rgb.write()
First import the mpython module:
from mpython import *
Note
After importing the mpython module, a NeoPixel object rgb is created for the board. To control the onboard RGB, simply operate on the rgb object.
Set colors:
rgb[0] = (255, 0, 0) # Set to red, full brightness
rgb[1] = (0, 128, 0) # Set to green, half brightness
rgb[2] = (0, 0, 64) # Set to blue, quarter brightness
Note
rgb[n] = (r, g, b) can set the color of each pixel.
nis the index of the onboard RGB LED, starting from 0.r,g,bare color brightness values ranging from 0-255.rgb.fill(rgb_buf) can fill the color of all pixels. For example: rgb.fill((255,0,0)) sets all RGB LEDs to red at full brightness.
Output colors to RGB LED:
rgb.write()
External LED Strip
Example: Light up external LED strip
from mpython import *
import neopixel
np = neopixel.NeoPixel(Pin(Pin.P15), n=24,bpp=3,timing=1)
def wheel(pos):
# Generate a rainbow color spectrum by changing each color parameter between 0 and 255
# 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:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos*3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos*3)
g = 0
b = int(pos*3)
else:
pos -= 170
r = 0
g = int(pos*3)
b = int(255 - pos*3)
return (r, g, b)
def cycle(np,r,g,b,wait=20):
# Cycle effect: one pixel runs through all strip positions while others are off
for i in range(4 * np.n):
for j in range(np.n):
np[j] = (0, 0, 0)
np[i % np.n] = (r, g, b)
np.write()
sleep_ms(wait)
def bounce(np,r,g,b,wait=20):
# Bounce effect: wait time determines the speed of the bounce effect
n=np.n
for i in range(4 * n):
for j in range(n):
np[j] = (r, g, b)
if (i // n) % 2 == 0:
np[i % n] = (0, 0, 0)
else:
np[n - 1 - (i % n)] = (0, 0, 0)
np.write()
sleep_ms(wait)
def rainbow_cycle(np,wait_us):
# Rainbow effect
n=np.n
for j in range(255):
for i in range(n):
pixel_index = (i * 256 // n) + j
np[i] = wheel(pixel_index & 255)
np.write()
sleep_us(wait_us)
while True:
cycle(np,50,50,50,wait=20)
bounce(np,50,0,0,wait=20)
rainbow_cycle(np,20)
Cycle effect
Bounce effect
Rainbow effect
To use an external LED strip, you must first create a neopixel object, defining the pin, bpp, and timing parameters, then you can control the LEDs on the strip through that object.
For more detailed usage, please refer to the neopixel module.
Hint
ledstrip module for neopixel, with richer neopixel display effects and simple operation. For details, please visit the following link.