Build a Clap-Activated Light with microbit!
2021-03-30 | By Pimoroni
License: See Original Project
Courtesy of Pimoroni
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 block radio set group and place it under on 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 out if true then and put it under forever.
- From Enviro:Bit, select Sound, drag out Wait 1000ms for a double clap and drop it onto the true.
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 select radio send number.
- Change 0 to 2.
- From Basic, select show number.
- 4. Change 0 to 2.
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 drag else if under the if statement.
- Select the blue toggle again to turn it off.
- From Enviro:Bit, select Sound, drag out Wait 1000ms for a single clap and drop it onto if else.
- Change 1000 to 500.
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 select radio send number.
- Change 0 to 1.
- From Basic select show number.
- Change 0 to 1.
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.
Microsoft MakeCode | Terms of Use | Privacy | Download
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 block radio set group and place it under on 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.
- From the Variables menu, drag out Set item to 0 and place it under On start.
- Change item to Lights.
- From the Neopixel menu, select the first block Neopixel at pin 0 and drop it onto 0.
- 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 out on radio received receivedNumber.
- From Logic, drag out if true then and put it under on radio received receivedNumber.
- From Logic, drag out 0 = 0 and drop it on top of the true.
- From Variables, drag out receivedNumber and drop it onto the first 0.
- Change the second 0 to 1.
- From Neopixel, drag out item show color red.
- Change item to Lights.
- Change red to blue (or whatever your favourite colour is!.)
Turning the lights off:
- Select the blue toggle on the if statement and drag else if under the if statement.
- Select the blue toggle again to turn it off.
- From Logic, drag out 0 = 0 and drop it on top of the true of the else if.
- From Variables, drag out receivedNumber and drop it onto the first 0.
- Change the second 0 to 2.
- From Neopixel drag out item show color red.
- Change item to Lights.
- Change red to black.
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 out on button A pressed.
- From Neopixel, drag out item clear.
- From Neopixel, drag out item show.
- Change item to Lights 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.
Microsoft Make Code | Terms of Use | Privacy | Download
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.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum