You are on page 1of 8

Tutorial 1: The Neopixel

What you need:

A Microbit

A USB cable

3x Crocodile Clips

A Neopixel strip

Neopixels are strips of red, green and blue LED lights, using the micro:bits we can control what
colours the LEDs are.

Set up
Attach your three crocodile clips to the microbit.

Connect the crocodile clip attached to ‘0’ to the BLUE wire on


the Neopixel. Attach the crocodile clip on ‘3V’ to the RED wire,
and finally the GND crocodile clip to the BLACK wire from the
strip.

Once you’ve done this, go to https://makecode.microbit.org


then click on “New project”

CODING
On the central column, click on “Advanced” and scroll to the bottom until you find “Add Package”.
Choose the Neopixel option.
Starting to code – block colours
Go to the Neopixel drawer and drag the top block into the workspace, and inside the “on start”.
Change 24 to be the number of LEDs on the Neopixel strip (usually either 7 or 8). To make your lights
begin to show colours, go to the Neopixel drawer and find the “Item show colour [red]” block. Drag
this in to the “Forever” event block.

Download your program and try out the different colours!

Sequences of colours
Now we have programmed single block colours, how can we start experimenting with a sequence of
the same colour?

Delete the blocks inside your “Forever” event block so it’s empty again. Now go to the “Loops”
drawer, and find the “For index 0 to 4” block and drag it inside your Forever loop. Change 4 to be
the number of LEDs on your neopixel strip.

To individual address each of the LEDs, click on Neopixel, and then click on the “… More” option
which appears underneath. This gives you even more blocks to play with. In this case, pick the top
block and drag it inside your green “for index from 0 to [7]” loop.

Go to the variables drawer, and find the block called “index”. Drag the “index” block to replace the 0
on “strip set pixel colour”.
Go back to Neopixel drawer and find the “item show” block, and place this inside the loop under you
“set pixel color” block.

You should see on the simulator that the Neopizel strip begins to light up, but maybe a little too
quick. To add pauses to your sequence of lights, go to the Basic drawer and find the “pause [100]”
block – put this underneath your “item show” block. You can experiment with the pauses, but I
found that 200 worked best!

Rainbow colours

After just one round, it looks like your program stops, because all the lights are now red. Lets start to
add in some random colours to make a rainbow. Go to Neopixel drawer and click the “… More”
which appears underneath. Once in the Neopixel -> More drawer, find the “red [255] green [255]
blue [255]” block, and drag this into your workspace. This block is used to set a colour for the
neopixel.

To repeat this process for the green and the blue, just right click on the purple “pick random 0 to
[255]” block and select duplicate, and replace the original [255]s until your block looks like:

Now we’re going to use this block to replace the “red” option inside your “strip set pixel color at
index” block inside your forever loop. This means every time this block is called, the colour will be
randomised.

Once your code looks like the example below. Give it a download:

Responsive colours
The micro:bits also contain accelerometers.

Accelerometers are used to detect how far the micro:bit is moved, tilted or rotated. So wouldn’t it
be cool if we could use the lights to react to the these changes? If you completed the code above,
delete the code inside your forever loop.

Go to the Variables drawer, and click the grey “make a variable” button. Make 3 new variables called
“x”, “y”, and “z”. When you’re finished, your variable drawer should look like:
Now drag in 3 “set [x] to [0]” inside the Forever event block. Using the small downwards arrow next
to “[x]”, change each of the blocks to set the “x”, “y” and “z” variables to 0 instead. Like:

Because the neopixel colour values can only accept up to 255, we need to do some maths to make
them work. The first thing we need to do, is go to the Math drawer and find the “[0] / [0]” block, and
attach one to each of our xyz variables. Then change each of the righthand zeros to be “4”.

Then we’re heading back to the Math drawer to find the “absolute of [0]” block. Drag this in to
replace the lefthand zero of your division blocks.

To get the accelerometer data, head to the Input drawer and find the “acceleration (mg) [x]” block.
Place this inside the [0] of the “absolute of [0]” block for the “x” variable. Repeat this for your “y”
and “z” variables, by changing the “acceleration (mg) [x]” by using the small downwards arrow next
to “x”.
Now to use these values for our neopixel. Head to the Neopixel drawer and find the “[x] show colour
[red]” block. Drag this inside your forever loop. Then head to the Neopixel -> More menu, to find the
“red [255] green [255] blue [255]” block, and drag this onto your previous block to replace the [red].

Now head back to the Variable drawer, and drag in the “x”, “y” and “z” to replace the [255]s.
Once your code looks like this, go ahead and download it onto your micro:bit. Once the orange light
on the back stops flashing, have a go at rotating and twisting your micro:bit to change the colours.

You might also like