title | layout | meta-description | share | author | about | cats | simple-description | acknowledgements | date | date-updated |
---|---|---|---|---|---|---|---|---|---|---|
Use Neopixels with the Microbit & Python |
text-width-sidebar |
The micropython library enables the microbit to drive a Neopixel module. Here's how to set it up. |
true |
jez |
Use the neopixel library to light an array of neopixels |
external |
Neopixels |
Video & teaser image by Adafruit (CC-BY-2.0) |
2016-12-23 10:20:00 UTC |
2016-12-23 10:20:00 UTC |
{::options parse_block_html="true" /}
Neopixels are RGB LEDs sold by an electronics company called Adafruit. The modules come in different shapes and can contain any number of RGB LEDs. Each LED can be controlled (or addressed) individually. The microbit can drive around 8 LEDs; to control modules with more LEDs you'll need to use an external 5v power source.
You can get generic, Neopixel-style modules by searching for 'ws2812 LED' on eBay or Amazon.
{:.ui .dividing .clearing .header}
Most neopixel modules require the user to solder on their own connections. This is simple; here's one of my modules:
- Solder wires to PWR, GND and IN.
- In this example the wires are colour-coded: black (for GND), red (for PWR) or yellow (for IN)
{:.ui .dividing .header}
Connect the pins to the microbit. In this example, the in
pin on the neopixel is connected to pin 0
.
{:.ui .dividing .header}
Neopixels can now be used in PXT. I'll update this page.
The neopixel
library must be imported:
{% highlight python %} import neopixel {% endhighlight %}
Initialise the neopixel module. Here the variable neopixels
is assigned to neopixel.NeoPixel(pin0, 12)
.
{% highlight python %}
neopixels = neopixel.NeoPixel(pin0, 12) {% endhighlight %}
Each neopixel is now in the list neopixels
and can be individually addressed. Colour is controlled by a tuple of RGB values:
{% highlight python %} neopixels[0] = (0,255,0) # First LED to red neopixels[1] = (255,150,0) # Second LED to white neopixels[-1] = (255,0,0) # Final LED to yellow
neopixels.show () {% endhighlight %}
Light three pixels next to each other either red, amber or red.
{% highlight python %}
from microbit import * import neopixel
neopixels = neopixel.NeoPixel(pin0, 12)
neopixels[0] = (255,150,0) # First LED to Amber neopixels[1] = (0,255,0) # Second LED to Green neopixels[-1] = (255,0,0) # Final LED to Red
neopixels.show() # Light the LEDs {% endhighlight %}
Light one LED in the neopixel red for 200 milliseconds then light the next one.
{% highlight python %} from microbit import * import neopixel
neopixels = neopixel.NeoPixel(pin0, 12)
while True:
# Do this for each pixel in the neopixels list
for pixel in range(0, len(neopixels)):
neopixels[pixel] = (255,0,0) # Turn on red
neopixels.show() # show red
sleep(200)
neopixels[pixel] = (0,0,0) # Turn off red
{% endhighlight %}
- Use
random.ranrange(0,255)
to set an LED to a random colour - Define a function to fade in the LED