Open MakeCode, start a new project and add
the extension: Extensions, search neopixel. Its blocks appear in the blue Neopixel drawer. Go as
far as you like: every story works on its own.
Start the MakeCode tutorial
all 4 stories, step by step
The Arduino needs a library for the LED strip: ready-made code that someone wrote and shared, so you do not
have to send the 24 bits per LED yourself.
- In the Arduino IDE: Tools, Manage Libraries. Search Adafruit NeoPixel and click
Install.
- The first line of the sketch brings the library in: #include <Adafruit_NeoPixel.h>
- Then you make a strip object: Adafruit_NeoPixel strip(10, 6, NEO_GRB + NEO_KHZ800); means 10
LEDs on pin 6, colours in the order green, red, blue.
- Now you call the library's functions on it: strip.begin(), strip.setBrightness(40),
strip.setPixelColor(index, color), strip.show(), strip.clear(), strip.rainbow(hue).
Go as far as you like: every story works on its own.
Story 1: A moving rainbowfollow the code
The same in Python
strip = neopixel.create(DigitalPin.P1, 10, NeoPixelMode.RGB)
strip.set_brightness(40)
strip.show_rainbow(1, 360)
def on_forever():
strip.rotate(1)
strip.show()
basic.pause(50)
basic.forever(on_forever)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
rotate moves every colour one LED along, and the last one comes back to the start. show sends the
new colours to the strip: nothing changes until you show.
Your turn: make it run faster, slower, or the other way (rotate by -1). Rainbow from 1 to 120?
#include <Adafruit_NeoPixel.h>
const int stripPin = 6;
const int ledCount = 10;
Adafruit_NeoPixel strip(ledCount, stripPin, NEO_GRB + NEO_KHZ800); // the strip object from the library
unsigned int firstHue = 0; // where the rainbow starts on the colour wheel (0 to 65535)
void setup() {
strip.begin();
strip.setBrightness(40); // of 255: keep it low
}
void loop() {
strip.rainbow(firstHue); // the whole colour wheel along the strip
strip.show(); // send the colours to the strip
firstHue += 512; // move the rainbow a little
delay(20);
}
rainbow paints the whole colour wheel along the strip, starting at firstHue. Adding 512 each time
moves it a little. show sends the new colours to the strip: nothing changes until you show.
Your turn: make it run faster, slower, or the other way (firstHue -= 512).
Story 2: Chase the lighta loop
The same in Python
strip = neopixel.create(DigitalPin.P1, 10, NeoPixelMode.RGB)
strip.set_brightness(40)
def on_forever():
for index in range(10):
strip.clear()
strip.set_pixel_color(index, neopixel.colors(NeoPixelColors.RED))
strip.show()
basic.pause(30)
basic.forever(on_forever)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
The LEDs are numbered from 0, so 10 LEDs go from 0 to 9. LED 0 is the one next to the wires: its address
is 0. Two strips plugged together? Then it is 20 LEDs, from 0 to 19.
Your turn: run back again (set pixel color at 9 − index), or leave a tail: remove
strip clear and turn off the LED three places behind.
#include <Adafruit_NeoPixel.h>
const int stripPin = 6;
const int ledCount = 10;
Adafruit_NeoPixel strip(ledCount, stripPin, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.setBrightness(40);
}
void loop() {
for (int index = 0; index < ledCount; index++) {
strip.clear(); // all LEDs off
strip.setPixelColor(index, strip.Color(255, 0, 0)); // LED number index: red, green, blue
strip.show();
delay(30);
}
}
The LEDs are numbered from 0, so 10 LEDs go from 0 to 9. strip.Color(255, 0, 0) makes a colour
from red, green and blue, each 0 to 255.
Your turn: run back again (ledCount - 1 - index), or leave a tail of dimmer LEDs.
Story 3: Switch on the bladebuttons
The same in Python
strip = neopixel.create(DigitalPin.P1, 10, NeoPixelMode.RGB)
strip.set_brightness(40)
def on_button_pressed_a():
for index in range(10):
strip.set_pixel_color(index, neopixel.colors(NeoPixelColors.BLUE))
strip.show()
basic.pause(20)
input.on_button_pressed(Button.A, on_button_pressed_a)
def on_button_pressed_b():
for index2 in range(10):
strip.set_pixel_color(9 - index2, neopixel.colors(NeoPixelColors.BLACK))
strip.show()
basic.pause(20)
input.on_button_pressed(Button.B, on_button_pressed_b)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
Button A ignites the blade from the handle to the tip, button B pulls it back. Keep the story 1 on start blocks,
but without the rainbow.
Your turn: pick your own blade colour with a red, green, blue block. Add a sound on the
Micro:bit speaker when the blade ignites.
#include <Adafruit_NeoPixel.h>
const int stripPin = 6;
const int ledCount = 10;
const int igniteButtonPin = 2;
const int retractButtonPin = 3;
Adafruit_NeoPixel strip(ledCount, stripPin, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.setBrightness(40);
strip.show(); // start with all LEDs off
pinMode(igniteButtonPin, INPUT_PULLUP); // LOW when pressed
pinMode(retractButtonPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(igniteButtonPin) == LOW) {
for (int index = 0; index < ledCount; index++) { // from the handle to the tip
strip.setPixelColor(index, strip.Color(0, 0, 255));
strip.show();
delay(20);
}
}
if (digitalRead(retractButtonPin) == LOW) {
for (int index = ledCount - 1; index >= 0; index--) { // from the tip back to the handle
strip.setPixelColor(index, 0);
strip.show();
delay(20);
}
}
}
The ignite button lights the blade from the handle to the tip, the retract button turns it off from the tip.
INPUT_PULLUP makes a pin read LOW while its button is pressed.
Your turn: your own blade colour with strip.Color(red, green, blue).
Story 4: Swing for coloursTurn for coloursmotion sensoranalog input
The same in Python
hue = 0
strip = neopixel.create(DigitalPin.P1, 10, NeoPixelMode.RGB)
strip.set_brightness(40)
def on_gesture_shake():
strip.show_color(neopixel.colors(NeoPixelColors.WHITE))
basic.pause(80)
strip.show_color(neopixel.hsl(hue, 100, 50))
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
def on_forever():
global hue
hue = Math.map(input.acceleration(Dimension.X), -1023, 1023, 0, 359)
strip.show_color(neopixel.hsl(hue, 100, 50))
basic.pause(50)
basic.forever(on_forever)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
The Micro:bit feels movement with its accelerometer. A swing flashes the blade white. Tilting left and right
gives a number from −1023 to 1023, and map turns it into a colour on the colour wheel (the hue, 0 to 359).
Your turn: only change colour while the blade is on. Hint: a variable lit that buttons A and B
set to true and false.
#include <Adafruit_NeoPixel.h>
const int stripPin = 6;
const int ledCount = 10;
const int igniteButtonPin = 2;
const int retractButtonPin = 3;
const int knobPin = A0;
Adafruit_NeoPixel strip(ledCount, stripPin, NEO_GRB + NEO_KHZ800);
bool lit = false; // is the blade on?
// The knob gives 0 to 1023; map turns it into a place on the colour wheel (0 to 65535).
uint32_t bladeColor() {
long hue = map(analogRead(knobPin), 0, 1023, 0, 65535);
return strip.gamma32(strip.ColorHSV(hue));
}
void setup() {
strip.begin();
strip.setBrightness(40);
strip.show();
pinMode(igniteButtonPin, INPUT_PULLUP);
pinMode(retractButtonPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(igniteButtonPin) == LOW && !lit) {
for (int index = 0; index < ledCount; index++) {
strip.setPixelColor(index, bladeColor());
strip.show();
delay(20);
}
lit = true;
}
if (digitalRead(retractButtonPin) == LOW && lit) {
lit = false;
for (int index = ledCount - 1; index >= 0; index--) {
strip.setPixelColor(index, 0);
strip.show();
delay(20);
}
}
if (lit) {
strip.fill(bladeColor()); // the whole blade in the knob's colour
strip.show();
}
delay(20);
}
The Arduino has no motion sensor, so a knob turns the colour. analogRead gives a number from 0
to 1023, and map turns it into a place on the colour wheel, 0 to 65535. The variable lit remembers
if the blade is on.
Your turn: print the knob's value in the Serial Monitor (Serial.println).
Really stuck? Try first: you learn the most by building it yourself. If it still does not work, your
teacher can give you a code for a ready-made program of your story.
Type the code in the lock at the top right of the page.