So Easy MicroPython — NeoPixel (WS2812B) LED Strip

NeoPixels, also known as WS2812 full-color LED (values of red, green, and blue setting between 0~255).

Step 1: Upload library to your MCU

Step 2: Lets’ learn some examples

import utime as time
from MyKitNeoPixel import myNeoPixel
LED_PIN = 14 # ESP8266: D5
LED_PIXELS = 12 # 12 LEDs
LED_BRI = 10 # Brightness set to 10%, to avoid dazzling
np = myNeoPixel(LED_PIN, pixels=LED_PIXELS, brightness=LED_BRI)

Example 1:Light: On / Off

for _ in range(3):
np.lightON()
time.sleep(0.5)
np.lightOFF()
time.sleep(0.5)

Example 2:Light: Toggle

np.lightON()
for _ in range(5):
time.sleep_ms(500)
np.lightToggle()

Example 3:Colors

np.light(255, 0, 0)      # RED: using (R,G,B) to color
time.sleep(2)
np.light(color="green") # Green:using name to color

Example 4:Colors: Random

for _ in range(5):
r, g, b = np.randomRGB()
np.light(r, g, b)
time.sleep(1)

Example 5:Colors: Code

for c in range(17):
np.lightColor(c)
time.sleep(0.5)

Example 6:Colors: Name

colors = ["black", "red", "orange", "yellow", "green", "blue", "indigo", "purple", "cyan", "magenta", "silver", "gray", "maroon", "olive", "teal", "navy", "white", "random", "off"]
for c in colors:
np.lightColor(c)
time.sleep(0.5)

Example 7:Brightness

for b in range(5, 50):
np.setBrightness(b) # 0 ~ 100
np.light()
time.sleep_ms(100)

Example 8:Pixel:

np.light(r, g, b, 4)  # only LED pixel #4 light ON, color is r, g, b
np.lightOFF(4) # only LED pixel #4 light OFF

Example 9:Pixel: One-by-One ON

for i in range(LED_PIXELS):
np.lightON(pixel=i)
time.sleep_ms(500)

Example 10:: One ON, others OFF

for i in range(LED_PIXELS):
np.lightOFF()
np.lightON(pixel=i)
time.sleep_ms(500)

Step 3: Some Light SHOWs

import utime as time
from MyKitNeoPixelShow import myNeoPixelShow
LED_PIN = 14 # ESP8266: D5
LED_PIXELS = 12 # 12 LEDs
LED_BRI = 10 # Brightness set to 10%, to avoid dazzling
np = myNeoPixelShow(LED_PIN, pixels=LED_PIXELS, brightness=LED_BRI)

Example 11:Flash show

np.show('blink', 500, 6)

Example 12:Fading In/Out show

np.show('up', 1000)
np.show('down', 1000)

Example 13:Marquee show

np.show('forward', 1000, 2)
np.show('backward', 1000, 2)

Example 14:Crazy lantern show

np.show('crazy', 1000, 3)

Example 15:Rainbow show

np.show('rainbow', 10000)

Example 16:Random color show

np.show('random', 1000, 5)

Example 17:Space Jump show

np.show('jump', 2000, 3)

Example 18:Dancing show

np.show('dance', 500, 10)

Example 19:360 Streamer show

np.show('360', 5000)

Example 20:360 Wheel show

np.show('360wheel', 30, 100)

Example 21:Personal special show

^_^ Show by yourself !!

DEMO 1: Comprehensive light show

DEMO 2: Smart Lamp, IoT

  1. As soon as the smart lamp powered on, it will automatically connect to the WiFi network.
  2. The smart lamp can simulated as a Publisher, automatically publish the command message of the light show to the MQTT Broker
  3. The smart lamp itself also can play as a Subscriber, automatically receive the subscribed new message from the MQTT Broker
  4. Turn into a light show command according to the MQTT message, and control the LED light bar or light a certain LED
  5. The user can also manually input the light show command manually or control it through any other MQTT Client App

DEMO 3: Network NTP Streamer Clock

That’s all, so easy right ? Hope this helps !

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store