What is Christmas without shiny lights?? On this day you will learn how to control a set of lights for your home using a BBC micro:bit. You'll be able to flash the lights, change them all at once or individually, create cool animations and even control the lights remotely from another micro:bit.
I'll go through what equipment you need, giving you lots of different options on what you can buy for this project. Then I'll go through the code, giving you examples of code and ideas for further projects.
I've used the BBC micro:bit on this project. It's cheap, it's lightweight, there is no setup and a lot of schools in the UK have access to them. You could use a Raspberry Pi or a Circuit Playground Express or even an Arduino. You just need any device that can access the Neopixel library.
For all the devices you're connecting ground and data to the light strip. You need to power the device separately.
You have lots of choices here.
In the UK on Ebay if you type WS2812B you'll get loads of options for light strips. You're looking for 5V light strips. I like the white waterproof ones in strips of 30. They're sturdy and have worked well for me.
Neopixels will work too. You're looking for lights strip with three wires: Ground, Power and Data. We need 5V strips not 12V.
The micro:bit can only power 8 RGB lights from its own power source. If we want more (which we do) we're going to need a separate power source.
If you're connecting 30 lights, which is a common size for light strips another 2xAAA battery pack will do. You'll need a terminal block to connect it to the light strip.
I have two different light strips that both claim to be the same light strip. One of them will run off 3xAAA, the other won't. If you want your light strip t olast long you'll have to test it with different battery packs and be prepared for failure.
To connect the micro:bit to the light strip you can use any of the following setups:
I've ordered them in order of reliability/stability from most reliable to least
I'm connecting the battery to the data out end of the light strip. The power can connect to either end, it doesn't matter.
My photo above shows the terminal block up close with all 4 wires connected.
Repeat after me:
ground to ground
power to power
If you connect these up wrong you risk draining your battery in seconds.
There are many ways of doing this. I've listed three of the easiest. I like method 1 but if you've got limited equipment the other two methods work just as well.
We're going to connect the 0 data pin from the micro:bit to the data wire of the light strip. We also need to connect ground.
You can buy for around £5 an edge connector for the micro:bit. This lets you slot the micro:bit into a board that gives you male pins like the Raspberry Pi.
Connect Ground
Connect Data
Tada! Connected
If you don't have an edge connector you can use crocodile clips
Connect Ground
Connect Data
Tada! Connected. Make sure the crocodile clips don't touch each other
If you don't have any jumper wires you can just use crocodile clips
Connect Ground
Connect Data
Tada! Connected
Maybe cover the bare wires/crocodile connection with insulating tape.
I don't like this method as the wire frays as the crocodile clip moves and you don't have a lot of wire on these strips.
Here's what the whole setup looks like using method 1 for connecting wires. We've got power coming out one end and the micro:bit on the other.
Let's get coding
MakeCode is a block like language for coding the micro:bit. I find it really easy to use, many children are already familiar with blocks through Scratch coding. You can code in microphyton as well.
Note: While you can use MakeCode offline, you need to be online to add the Neopixel library.
This adds Neopixel to your menu on the left. Now you're ready to code! Here are some examples
Notes:
MakeCode gives you all the colours of the rainbow to use with your lights
You can control individual lights as well as the entire strip
Easy to show how for loops work compared to the last example
My favourite :)
Oh so much!
The micro:bit has 2 buttons: Add an animation to each button.
The micro:bit has a compass. How about turning the lights yellow when the micro:bit points towards North??
The micro:bit has a light sensor: ooooo! Turn on the light strip when it gets dark
The micro:bit has a radio: Use another micro:bit as a remote for your light strip.
I hope your enjoyed this tutorial and please tag me on any projects you make with my instructions. I'm @LMcUnderwood on Twitter. I live there and my DMs are open if you want to chat!
I think lights are great way to enthuse students in learning how to code. When people think coding they think computers and robots. They don't think lights and art. It can be a real eye-opener to people when they realise you can control art with code.
Some projects I've created using lights: