Home Code Micro:bit and Neopixel examples in Micropython

Micro:bit and Neopixel examples in Micropython

by shedboy71

The neopixel module lets you use Neopixel (WS2812) individually addressable RGB LED strips with the Microbit. Note to use the neopixel module, you need to import it separately with:

import neopixel

I used the following module which has 8 LEDs on it

neopixel-300x300

Connection is straight forward 0v on the module to 0v on the Micro:bit, Vcc or 5v on the module can go to 3.3v on the Microbit and then connect the DIN on the module to Pin 0 of your Microbit

Now for various simple code examples

Code

This example will set each WS2812 to a different colour

[codesyntax lang=”python”]

from microbit import *
import neopixel
from random import randint

#Neopixel with 8 LEDs
neo = neopixel.NeoPixel(pin0, 8)

while True:
        neo[0] = (255, 0, 0)
        neo[1] = (0, 255, 0)
        neo[2] = (0, 0, 255)
        neo[3] = (128, 0, 0)
        neo[4] = (0, 128, 0)
        neo[5] = (0, 0, 128)
        neo[6] = (128, 128, 0)
        neo[7] = (0, 128, 128)
        neo.show()
        sleep(250)

[/codesyntax]

 

This example will loop through all WS2812 LEDs and set them to red

 

[codesyntax lang=”python”]

from microbit import *
import neopixel
from random import randint

#Neopixel with 8 LEDs
neo = neopixel.NeoPixel(pin0, 8)

while True:
    #Iterate over each LED in the strip
    for pixel_id in range(0, len(neo)):
        neo[pixel_id] = (255, 0, 0)
        neo.show()
        sleep(250)

[/codesyntax]

 

Now we will loop through all Ws2812’s and set them to red, green and blue individually

 

[codesyntax lang=”python”]

from microbit import *
import neopixel
from random import randint

#Neopixel with 8 LEDs
neo = neopixel.NeoPixel(pin0, 8)

while True:
    #Iterate over each LED in the strip
    for pixel_id in range(0, len(neo)):
        neo[pixel_id] = (255, 0, 0)
        neo.show()
        sleep(250)
        neo[pixel_id] = (0, 255, 0)
        neo.show()
        sleep(250)
        neo[pixel_id] = (0, 0, 255)
        neo.show()
        sleep(250) 

[/codesyntax]

 

And last but not least random colours, this is my favourite

 

[codesyntax lang=”python”]

from microbit import *
import neopixel
from random import randint

#Neopixel with 8 LEDs
neo = neopixel.NeoPixel(pin0, 8)

while True:
    #Iterate over each LED in the strip
    for pixel_id in range(0, len(neo)):
        red = randint(0, 255)
        green = randint(0, 255)
        blue = randint(0, 255)
        neo[pixel_id] = (red, green, blue)
        neo.show()
        sleep(250)

[/codesyntax]

 

Links

10PCS/LOT WS2812 RGB LED Breakout Module For Arduino

WS2812 serial 5050 full-color LED Expansion Module

WS2812 WS 2811 5050 RGB LED Driver Module Board 8-Bit New

You may also like

Leave a Comment