Day 6
Shiny Lights
micro:bit controlled Christmas Lights
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.
BBC micro:bit
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.
Light strip
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.
Power
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.
Wires
To connect the micro:bit to the light strip you can use any of the following setups:
- an edge connector and 2 male-to-female jumper wires
- 2 crocodile clips and 2 male-to-male jumper wires
- 2 crocodile clips
I've ordered them in order of reliability/stability from most reliable to least
Let's get Making!
Connect the light strip to the power
Connect the light strip to the terminal block
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.
- Find the power wires on the light strip: 5V and GND. On mine 5V is red and GND is white.
- Strip the wires back so you can see bare wires
- Place each one inside a terminal block and screw them down
My photo above shows the terminal block up close with all 4 wires connected.
Connect the battery to the terminal block
- Take out any batteries in the battery pack
- Cut off the connector on the battery pack and strip back the wires.
- Screw the black ground wire into the terminal opposite the ground wire of the light strip
- Screw the red power wire into the terminal opposite the power wire of the light strip
Repeat after me:
ground to ground
power to power
If you connect these up wrong you risk draining your battery in seconds.
Connect the light strip to the micro:bit
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.
1. Edge connector
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 the female end of a male-to-female jumper wire a 0V pin, also known as GND/Ground
- Connect the male end of the 0V jumper wire into the black adaptor's hole for Ground
Connect Data
- Connect the female end of a male-to-female jumper wire the pin 0
- Connect the male end of the 0 pin jumper wire into the black adaptor's hole for Data
Tada! Connected
2. Crocodiles and Jumpers
If you don't have an edge connector you can use crocodile clips
Connect Ground
- Connect a crocodile clip to GND on the micro:bit
- Connect the other end of the crocodile clip to the male end of a male-to-male jumper wire
- Connect the other end of the jumper wire into the black adaptor's hole for Ground
Connect Data
- Connect a different crocodile clip to 0 on the micro:bit
- Connect the other end of the crocodile clip to the male end of a male-to-male jumper wire
- Connect the other end of the jumper wire into the black adaptor's hole for Data
Tada! Connected. Make sure the crocodile clips don't touch each other
3. Crocodiles
If you don't have any jumper wires you can just use crocodile clips
- Chop off the black connector from the Data In end of the light strip
- Strip back the Ground and Data wires
Connect Ground
- Connect a crocodile clip to GND on the micro:bit
- Connect the other end of the crocodile clip to the Ground bare wire on the light strip
Connect Data
- Connect a different crocodile clip to 0 on the micro:bit
- Connect the other end of the crocodile clip to the Data bare wire on the light strip
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.
Beautiful!
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
Coding in MakeCode
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.
- Go to https://makecode.microbit.org/ and select New Project
- From the left menu, scroll to the bottom and select Advanced
- Scroll to the bottom again and select Extensions
- The Neopixel extension will appear in the list of Extensions, select it.
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:
- You always need to setup the strip under on start. Change the number to the number of lights in your strip
- If you're setting individual LEDs you'll always need to use the block strip show after it or nothing will happen
- Use of the pause block is important here as without it you won't see changes in colour
Flash all the lights
MakeCode gives you all the colours of the rainbow to use with your lights
Change one at a time
You can control individual lights as well as the entire strip
Use the Loops
Easy to show how for loops work compared to the last example
Rainbow!
My favourite :)
Download
- Download your code into the micro:bit connected to the lightstrip. Find out more about downloading on the micro:bit website
- Power your micro:bit, either through a battery pack or a USB cable connected to your laptop
- Double check you've connected Ground to Ground and Power to Power.
- Fingers crossed and turn everything on
Troubleshooting
- Does your code work on the simulator in MakeCode?
- Are you powering the micro:bit? The power source only powers the light strip. The micro:bit needs its own power, a battery pack or USB connection.
- From the micro:bit there should be two connection to the light strip: Data (Pin 0) and Ground.
- From the power source there should be two connection to the light strip: Power and Ground
- If you have crocodile clips make sure they're not touching each other and they're clamped firmly onto the wires/micro:bit. Straighten up any connections on the micro:bit. There are other pins between 0 and 1 on the micro:bit.
- Is your power source turned on?
What Next?
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.
Enjoy
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:
- I have a set of lights setup on my stairs that show the outside temperature for my house.
- I made a giant RGB LED cube that you can play 3D snake on and
- In my upcoming book Save the World with Code: 20 Fun Projects for Kids Using Raspberry Pi, micro:bit, and Circuit Playground Express I show you how to create a light up backpack and sword using the same lightstrip.