# micro:bit Lesson 3. NeoPixels with micro:bit

## Overview

NeoPixels (addressable LEDs) are a great way to easily create colorful displays using strips of LEDs or LEDs arranged in a circle like the NeoPixel ring used in this lesson.

![](https://cdn-learn.adafruit.com/assets/assets/000/050/528/medium800thumb/led_pixels_rotating.jpg?1517238060)

In this lesson you will learn how to connect your NeoPixels to your micro:bit and then how to write software to control it using JavaScript Blocks, MicroPython and Arduino code.

Although this lesson uses a NeoPixel ring, the example programs will work fine with pretty much any NeoPixel display.

# micro:bit Lesson 3. NeoPixels with micro:bit

## Parts

This lesson uses the following parts, but you may choose to use a different NeoPixel display. The micro:bit cannot come with much more than 24 NeoPixels or you may draw too much current from the micro:bit.

Featured
### Small Alligator Clip to Male Jumper Wire Bundle - 12 Pieces

[Small Alligator Clip to Male Jumper Wire Bundle - 12 Pieces](https://www.adafruit.com/product/3255)
For bread-boarding with unusual non-header-friendly surfaces, these cables will be your best friends! No longer will you have long strands of alligator clips that are grabbing little wires. These compact jumper cables have a premium male header on one end, and a grippy mini alligator clip on...

In Stock
[Buy Now](https://www.adafruit.com/product/3255)
[Related Guides to the Product](https://learn.adafruit.com/products/3255/guides)
![ Bundle of Small Alligator Clip to Male Jumper Wires](https://cdn-shop.adafruit.com/640x480/3255-02.jpg)

Featured
### NeoPixel Ring - 24 x 5050 RGB LED with Integrated Drivers

[NeoPixel Ring - 24 x 5050 RGB LED with Integrated Drivers](https://www.adafruit.com/product/1586)
Round and round and round they go! 24 ultra bright smart LED NeoPixels are arranged in a circle with 2.6" (66mm) outer diameter. The rings are 'chainable' - connect the output pin of one to the input pin of another. Use only one microcontroller pin to control as many as you can...

In Stock
[Buy Now](https://www.adafruit.com/product/1586)
[Related Guides to the Product](https://learn.adafruit.com/products/1586/guides)
![Hand holding NeoPixel Ring with 24 x 5050 RGB LED, lit up rainbow](https://cdn-shop.adafruit.com/640x480/1586-00.jpg)

# micro:bit Lesson 3. NeoPixels with micro:bit

## Connections

NeoPixels are very easy to connect requiring just three connections:

- GND - ground
- Positive supply 3V (in the case of the micro:bit)
- Data Input - data is streamed into the chain of NeoPixels, each NeoPixel passing on the data to the next one along the chain. When the data stops all the NeoPixels show the color that their data indicates. Connect the Data Input connection on the NeoPixel display&nbsp; to Pin 2 of the micro:bit.

In terms of electrically connecting to the display, if you are using the suggested 24 pixel ring then you can use alligator to male header pin leads to connect up the display by pushing the male header pins into the pins marked GND, PWR and Data In.

![](https://cdn-learn.adafruit.com/assets/assets/000/050/529/medium800/led_pixels_pixel_ring_poked_through.jpg?1517242249)

Poking the pins through the holes like this is fine for trying things out, but to make things more permanent, solder the pins from the top side of the board and clip off the excess pin.

# micro:bit Lesson 3. NeoPixels with micro:bit

## 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.

![](https://cdn-learn.adafruit.com/assets/assets/000/050/617/medium800thumb/led_pixels_rotating.jpg?1518656060)

Danger: 

# 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](https://makecode.microbit.org/_2tuDt0HMufRY)&nbsp;to open the editor in a separate browser tab.

https://makecode.com/_2tuDt0HMufRY

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).

&nbsp;

# 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.

![](https://cdn-learn.adafruit.com/assets/assets/000/050/619/medium800/led_pixels_add_package.png?1517481700)

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_.

![](https://cdn-learn.adafruit.com/assets/assets/000/050/620/medium800/led_pixels_add_package2.png?1517481830)

# Micro Python

To run the MicroPython version of the code, open up the [online Python editor here](http://python.microbit.org/)&nbsp;and paste the following code into the editor window.

```auto
    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:

```auto
foreground = [0, 0, 255]  # Hex color - red, green and blue
background = [16, 16, 16]
```

For a discussion on different ways of specifying colors, see&nbsp;[https://en.wikipedia.org/wiki/Web\_colors](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](../../../../use-micro-bit-with-arduino/overview).

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_.

![](https://cdn-learn.adafruit.com/assets/assets/000/050/621/medium800/led_pixels_ardu_library.png?1517483152)

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

```auto
#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.

# micro:bit Lesson 3. NeoPixels with micro:bit

## Other Things to Do

Try changing the foregound and background colors of the program.&nbsp;

You can also try reducing the delay/pause period to speed up the rotating effect.

There are loads of interesting effects you can make with NeoPixels, so take a look at Lady Ada's&nbsp;[NeoPixel Über Guide](../../../../adafruit-neopixel-uberguide)

# About the Author

If you have found this lesson useful, and want to learn Python, then you might like my book Programming the BBC micro:bit: Getting Started with MicroPython.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/050/622/medium800/led_pixels_cover.jpg?1517484269)


## Guide Products

### BBC micro:bit

[BBC micro:bit](https://www.adafruit.com/product/3530)
The British Invasion is here! No, not music...microcontrollers! New to the USA is the newest and _easiest_ way to learn programming and electronics - the **BBC micro:bit**.

Designed specifically for kids and beginners, the&nbsp; **micro:bit** &nbsp;is a...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/3530)
[Related Guides to the Product](https://learn.adafruit.com/products/3530/guides)
### Small Alligator Clip to Male Jumper Wire Bundle - 12 Pieces

[Small Alligator Clip to Male Jumper Wire Bundle - 12 Pieces](https://www.adafruit.com/product/3255)
For bread-boarding with unusual non-header-friendly surfaces, these cables will be your best friends! No longer will you have long strands of alligator clips that are grabbing little wires. These compact jumper cables have a premium male header on one end, and a grippy mini alligator clip on...

In Stock
[Buy Now](https://www.adafruit.com/product/3255)
[Related Guides to the Product](https://learn.adafruit.com/products/3255/guides)
### NeoPixel Ring - 24 x 5050 RGB LED with Integrated Drivers

[NeoPixel Ring - 24 x 5050 RGB LED with Integrated Drivers](https://www.adafruit.com/product/1586)
Round and round and round they go! 24 ultra bright smart LED NeoPixels are arranged in a circle with 2.6" (66mm) outer diameter. The rings are 'chainable' - connect the output pin of one to the input pin of another. Use only one microcontroller pin to control as many as you can...

In Stock
[Buy Now](https://www.adafruit.com/product/1586)
[Related Guides to the Product](https://learn.adafruit.com/products/1586/guides)

## Related Guides

- [Wireless Control Button for WLED Projects](https://learn.adafruit.com/wireless-control-button-for-wled-projects.md)
- [NYE Circuit Playground Drop](https://learn.adafruit.com/nye-circuit-playground-drop.md)
- [CRICKIT WobblyBot](https://learn.adafruit.com/crickit-wobblybot.md)
- [Lucky Cat with Circuit Playground Express](https://learn.adafruit.com/lucky-cat-with-circuit-playground-express.md)
- [DIY Robotic Sky Tracking Astrophotography Mount with CircuitPython](https://learn.adafruit.com/diy-robotic-sky-tracking-astrophotography-mount.md)
- [Machine Learning with Marshmallows and Tiny Sorter](https://learn.adafruit.com/machine-learning-with-marshmallows-and-tiny-sorter.md)
- [FruitBox Sequencer: Musically Delicious Step Pattern Generator ](https://learn.adafruit.com/circuitpython-fruitbox-sequencer-musically-delicious-step-pattern-generator.md)
- [Combo Dial Safe with Circuit Playground Express](https://learn.adafruit.com/combo-dial-safe-with-circuit-playground-express.md)
- [Adafruit Simple Soil Moisture Sensor](https://learn.adafruit.com/adafruit-simple-soil-moisture-sensor.md)
- [Micro:bit with Arduino](https://learn.adafruit.com/use-micro-bit-with-arduino.md)
- [LED Rocket Lamp](https://learn.adafruit.com/led-rocket-lamp.md)
- [DIY Circuit Playground Shields](https://learn.adafruit.com/circuit-playground-shields.md)
- [PyPortal IoT Weather Station](https://learn.adafruit.com/pyportal-iot-weather-station.md)
- [CircuitPython BLE Crickit Rover](https://learn.adafruit.com/circuitpython-ble-crickit-rover.md)
- [NeoPixel Ring Lamp](https://learn.adafruit.com/neopixel-ring-lamp.md)
