Home Code Micro:bit and Neopixel examples with Micropython

Micro:bit and Neopixel examples with 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

Lets look at the WS2812

WS2812 is a intelligent control LED light source that the control circuit and RGB chip are integrated in a package of 5050 components. It internal include intelligent digital port data latch and signal reshaping amplification drive circuit. Also include a precision internal oscillator and a 12V voltage programmable constant current control part, effectively ensuring the pixel point light color height consistent.

The data transfer protocol use single NZR communication mode. After the pixel power-on reset, the DIN port receive data from controller, the first pixel collect initial 24bit data then sent to the internal data latch, the other data which reshaping by the internal signal reshaping amplification circuit sent to the next cascade pixel through the DO port. After transmission for each pixel´╝îthe signal to reduce 24bit. pixel adopt auto reshaping transmit technology, making the pixel cascade number is not limited the signal transmission, only depend on the speed of signal transmission.

LED with low driving voltage, environmental protection and energy saving, high brightness, scattering angle is large, good consistency, low power, long life and other advantages. The control chip integrated in LED above becoming more simple circuit, small volume, convenient installation.

I used the following module which has 8 LEDs on it

Parts List

Name Link
Micro:bit Micro:bit Development Board
WS2812 NeoPixel NeoPixel Stick 8 channel WS2812 5050 RGB LED lights built-in full color-driven development board
Edge Breakout I/O Expansion Edge Breakout I/O Expansion Extension Board for BBC micro:bit
Connecting cables Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Layout

 

microbit and neopixel example

microbit and neopixel example

Code

This example will set each WS2812 to a different colour

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)

 

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

 

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)

 

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

 

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)

 

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

 

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)

You may also like

Leave a Comment