Build a clap-activated light with micro:bit!
Did you ever see in the movies when someone claps their hands twice and all the lights turn off?? I thought that was the coolest thing ever! Now, with the micro:bit, an enviro:bit, and some NeoPixel lights you can recreate that same effect!
Here's what you'll be making:
micro:bit #1 is going to be wearing the stylish enviro:bit. micro:bit #2 will be connected to one of the shiny ZIP Halo NeoPixel ring lights. When micro:bit #1 detects a clap, it will send a radio signal to micro:bit #2. micro:bit #2 will turn on/off the lights depending on the information it receives from micro:bit #1. Simples!
We’ll go through the code for both micro:bits. You’ll learn how to use the enviro:bit, the radio, and the NeoPixel package on the micro:bit.
We put the micro:bit with the ZIP Halo on inside an IKEA Sinnerlig glass lamp, after taking the original bulb and wiring out, and it makes a lovely mood light. We used battery packs to power both the ZIP Halo micro:bit and enviro:bit micro:bit, so the whole thing is wireless, but you could easily use wired power supplies for a more permanent solution.
Coding the micro:bit that is wearing the enviro:bit
This first part will set all of the code up on micro:bit #1 with the enviro:bit, sensing the claps and sending commands to micro:bit #2 with the radio to control the lights.
Setting up the package
For ease of use, we'll add the enviro:bit blocks package to your menu:
- Select
Advanced
- Scroll down and select
Add Package
- Type
enviro
and then click the search icon - Select the
Enviro:Bit
package
This will add the Enviro:Bit
menu to your menu.
Setting up the radio
The micro:bit has a built-in radio function that lets you send commands from one micro:bit to another, and you can even send different commands to particular micro:bits by assigning them to different groups.
Let's set the radio up on micro:bit #1 to let it send commands to micro:bit #2.
- From the
Radio
menu, drag out the blockradio set group
and place it underon start
Check for a double clap
Let’s check for a double clap first. If we hear a double clap, we'll send the number 2
to micro:bit #2. The double clap will turn the light off.
- From
Logic
, drag outif true then
and put it underforever
- From
Enviro:Bit
, selectSound
, drag outWait 1000ms for a double clap
and drop it onto thetrue
For a double clap, let’s send 2
to micro:bit #2 and also display 2
on the LED matrix on the micro:bit so that we can see better what's happening.
- From
Radio
selectradio send number
- Change
0
to2
- From
Basic
, selectshow number
- Change
0
to2
Check for a single clap
If it’s not a double clap, let’s check for a single clap and send the number 1
to micro:bit #2.
- Select the blue toggle on the
if
statement and dragelse if
under theif
statement - Select the blue toggle again to turn it off
- From
Enviro:Bit
, selectSound
, drag outWait 1000ms for a single clap
and drop it ontoif else
- Change
1000
to500
For a single clap, let’s send 1
to micro:bit #2 and also display 1
on the LED matrix on the micro:bit.
- From
Radio
selectradio send number
- Change
0
to1
- From
Basic
selectshow number
- Change
0
to1
Extra code
If you find that the clap-sensing by the enviro:bit is too sensitive or not sensitive enough, then you can lower the sensitivity. Under on start
, add the block Set clap sensitivity to 10
from the Enviro:Bit
menu.
You can also add an if
statement around the whole lot of code and only check for claps if the sound level is greater than 15. Your complete if
statement would be if Get sound > 15
.
Download the code from here, or from below, to a micro:bit and attach the enviro:bit to it.
Coding the light
This second part of the code is for micro:bit #2, and will receive radio commands from micro:bit #1 and then control the lights accordingly. We'll use the Kitronik ZIP Halo NeoPixel ring light connected to this micro:bit.
Select Project
and New Project
in the MakeCode editor to start a new project.
Setting up the NeoPixel package
Add the neopixel package to MakeCode
- Select
Advanced
- Scroll down and select
Add Package
- Select the
neopixel
package
Setting up the radio
Let's set the radio up to allow it to receive commands from micro:bit #1.
- From the
Radio
menu, drag out the blockradio set group
and place it underon start
Setting up the lights
We need a variable that we'll set to control the lights. We'll give it the very clever name of… Lights
- Create a variable called
Lights
- Select
Variable
- Select
Create New Variable
- Type
Lights
and click OK
- Select
- From the
Variables
menu, drag outSet item to 0
and place it underOn start
- Change
item
toLights
- From the
Neopixel
menu, select the first blockNeopixel at pin 0
and drop it onto0
- If you're using different NeoPixel lights, change
24 leds
to the number of LEDs that you have
Making the lights react
Here's the plan:
- If we receive the number
1
, then turn on the lights - If we receive the number
2
, then turn off the lights
Turning the lights on:
- From
Radio
, drag outon radio received receivedNumber
- From
Logic
, drag outif true then
and put it underon radio received receivedNumber
- From
Logic
, drag out0 = 0
and drop it on top of thetrue
- From
Variables
, drag outreceivedNumber
and drop it onto the first0
- Change the second
0
to1
- From
Neopixel
, drag outitem show color red
- Change
item
toLights
- Change
red
toblue
(or whatever your favourite colour is!)
- Change
Turning the lights off:
- Select the blue toggle on the
if
statement and dragelse if
under theif
statement - Select the blue toggle again to turn it off
- From
Logic
, drag out0 = 0
and drop it on top of thetrue
of theelse if
- From
Variables
, drag outreceivedNumber
and drop it onto the first0
- Change the second
0
to2
- From
Neopixel
drag outitem show color red
- Change
item
toLights
- Change
red
toblack
- Change
Tada! Simples!
Extra code
It's a good idea to have an off switch, in case the double clap doesn’t work!
- From
Input
, drag outon button A pressed
- From
Neopixel
, drag outitem clear
- From
Neopixel
, drag outitem show
- Change
item
toLights
in both blocks
Download the code from here, or from below, to a micro:bit and attach the ZIP Halo to it. Kitronik have a nice guide on getting started with the ZIP halo on their site.
Plug battery packs into both your micro:bits, remembering to plug the one powering the ZIP Halo into the connector on the Halo itself rather than the one on the micro:bit (it'll provide power to the micro:bit through the Halo).
Turn both battery packs on, and turn the switch on the ZIP Halo to the on position also. Try clapping, and your light should turn on! Clap twice and it'll go off again. It can take a little experimentation to get the claps exactly right in terms of loudness and speed.
We used a couple of lumps of Blu Tack to attach the battery pack and the ZIP Halo to the base of the IKEA Sinnerlig lamp, and then popped the glass cover back on.
Search above to find more great tutorials and guides.