RGB LED ===================== The mPython Board carries 3 onboard WS2812 LEDs. The WS2812 is a low-power RGB tri-color LED with an integrated current-control chip, supporting 256 levels of brightness and full true-color display of 16,777,216 colors. It uses a special single-wire communication scheme to control the LED colors, and is simple to use. Onboard RGB LED --------------- Example: light up the 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 named rgb is created for the board; to control the onboard RGB LED, 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) sets the color of each pixel, where ``n`` is the index of the onboard RGB LEDs and the first LED is 0. ``r``, ``g`` and ``b`` are brightness values ranging from 0 to 255. * rgb.fill(rgb_buf) fills the color of all pixels, e.g. rgb.fill((255,0,0)) sets all RGB LEDs to red at full brightness. Output the colors to the RGB LEDs:: rgb.write() .. _neopixel_strip: External LED Strip ------------------ .. image:: /_static/products/mpython-s3/_static/image/tutorials/glamour.jpg :width: 600 :align: center Example: light up an 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 spectrum by varying 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 positions of the strip while the other pixels 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; the 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) .. figure:: /_static/products/mpython-s3/_static/image/tutorials/neopixel_control_leds_cycle.png :align: center cycle effect .. figure:: /_static/products/mpython-s3/_static/image/tutorials/neopixel_control_leds_bounce.png :align: center bounce effect .. figure:: /_static/products/mpython-s3/_static/image/tutorials/neopixel_control_leds_rainbow.png :align: center rainbow effect To use an external LED strip, first create a neopixel object and define the ``pin``, ``bpp`` and ``timeing`` parameters; only then can you control the LEDs on the strip through that object. For more detailed usage, see the :ref:`neopixel` module. .. Hint:: | mPython also provides ``ledstrip``, an enhanced version of ``neopixel``, which wraps richer neopixel display effects and is easy to use. For details, visit the link below. | mPython-ledstrip: https://github.com/labplus-cn/awesome-mpython/tree/master/library