Software

NeoPixel displays allow you to set the color and brightness of every pixel in the display independently. The following example program uses just two colors: one as a background the other color being the color for just one pixel that will rotate round from one pixel to the next.

Don't overload your micro:bit. With a 24 pixel display, like this, your micro:bit will only be able to supply enough current to light all the pixels at half maximum brightness, so, keep the numbers low to avoid problems. Some older micro:bits had a voltage regulator that did not automaticlly protect the micro:bit against overcurrent, so if you have one of these and light too many pixels too brightly, you could destroy your micro:bit.

JavaScript Block Code

The JavaScript Blocks Code editor is embedded directly on this page below. From the editor, you can click on Download button (bottom right) and then copy the downloaded file onto your micro:bit. Alternatively, you can Click here to open the editor in a separate browser tab.

The Blocks code works like this:

First, in the on start block a variable ring is assigned to the NeoPixel display. This specified the pin that will be used to send data to the NeoPixel and also the number of pixels in the display.

The forever block contains a for loop that counts from 0 to 23, keeping the counter in the variable index.

Inside the for loop, the pixel at the index position is set to blue. The show block is needed to actually send the updated pixel color to the pixel ring. After a pause to 50 milliseconds, the pixel at position index is then set to be a color defined by its red, green and blue components. As all three values are equal, the background color will be white, but not a very bright white as the value is only 16 (out of 255).

 

Using NeoPixels in the Blocks Editor

If you want to make your own project that uses NeoPixels, before you can use NeoPixels with the Blocks Editor, you will need to add Adafruit's NeoPixel package to your environment. Todo do this, click on the Add Package option circled below.

This will open the dialog below from which you should select the neopixel package. Once you have done this you will find an extra category of blocks called Neopixel appears below Math.

Micro Python

To run the MicroPython version of the code, open up the online Python editor here and paste the following code into the editor window.

Download: file
    from microbit import *
from neopixel import NeoPixel

num_pixels = 24
foreground = [0x00, 0x00, 0xFF]  # Hex color - red, green and blue
background = [0x10, 0x10, 0x10]

ring = NeoPixel(pin2, num_pixels)

while True:
    # blue dot circles around a white background (for PixelRing 24)
    for i in range(0, num_pixels):
        ring[i] = foreground     # set pixel i to foreground
        ring.show()              # actually display it
        sleep(50)                # milliseconds
        ring[i] = background     # set pixel to background before moving on
        ring.show()
  

The code defines three variables: num_pixels (the number of pixels in the display, foreground and background, the two colors are specified as standard red, green and blue hexadecimal numbers. If you prefer to use decimal numbers to specify the color, then you could change these lines to be:

Download: file
foreground = [0, 0, 255]  # Hex color - red, green and blue
background = [16, 16, 16]

For a discussion on different ways of specifying colors, see https://en.wikipedia.org/wiki/Web_colors

Arduino

Make sure that you have your Arduino environment set up for micro:bit by following this guide.

Before you can use NeoPixels with a micro:bit programmed from the Arduino IDE, you will need to make sure that your Arduino IDE has a recent version of the Adafruit NeoPixel library installed. To do this select Sketch menu -> Include Library -> Manage Libraries..

From the Library Manager, search for Adafruit Neopixel and then select and install the library called just Adafruit Neopixel by Adafruit.

Now start a new Sketch by clicking on the File menu and New. Then paste the following code into the editor window.

Download: file
#include <Adafruit_NeoPixel.h>

const int numPixels = 24;
const int pixelPin = 2;

Adafruit_NeoPixel ring = Adafruit_NeoPixel(numPixels, pixelPin);
uint32_t foreground = ring.Color(0x00, 0x00, 0xFF); // r, g, b - blue
uint32_t background = ring.Color(0x10, 0x10, 0x10); // dim white

void setup() {
  ring.begin(); // start the NeoPixel display
}

void loop() {
  // blue dot circles around a white background (for PixelRing 24)
  for (int i = 0; i < numPixels; i++) {
    ring.setPixelColor(i, foreground); // set pixel i to foreground
    ring.show();                       // actually display it
    delay(50);                         // milliseconds 
    ring.setPixelColor(i, background); // set pixel to background before moving on
    ring.show(); 
  }
}

For a description of how the code works, take a look at the description in the MicroPython section. The programs are very similar.

This guide was first published on Mar 09, 2018. It was last updated on Mar 09, 2018. This page (Software) was last updated on Feb 17, 2020.