Maker.io main logo

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:

  1. Select Advanced.
  2. Scroll down and select Add Package.
  3. Type enviro and then click the search icon.
  4. 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.

  1. From the Radio menu, drag out the block radio set group and place it under on start.

Code_1

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.

  1. From Logic, drag out if true then and put it under forever.
  2. From Enviro:Bit, select Sound, drag out Wait 1000ms for a double clap and drop it onto the true.

Code_2

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.

  1. From Radio select radio send number.
  2. Change 0 to 2.
  3. From Basic, select show number.
  4. 4. Change 0 to 2.

Code_3

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.

  1. Select the blue toggle on the if statement and drag else if under the if statement.
  2. Select the blue toggle again to turn it off.
  3. From Enviro:Bit, select Sound, drag out Wait 1000ms for a single clap and drop it onto if else.
  4. Change 1000 to 500.

Code_4

For a single clap, let’s send 1 to micro:bit #2 and also display 1 on the LED matrix on the micro:bit.

  1. From Radio select radio send number.
  2. Change 0 to 1.
  3. From Basic select show number.
  4. Change 0 to 1.

Code_5

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.

Code_6 

Download the code from here, or from below, to a micro:bit and attach the enviro:bit to it.

Code_6a

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.

  1. Select Advanced.
  2. Scroll down and select Add Package.
  3. Select the neopixel package.

Setting up the radio

Let's set the radio up to allow it to receive commands from micro:bit #1.

  1. From the Radio menu, drag out the block radio set group and place it under on start.

Code_7

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

  1. Create a variable called Lights.
    1. Select Variable.
    2. Select Create New Variable.
    3. Type Lights and click OK.
  2. From the Variables menu, drag out Set item to 0 and place it under On start.
  3. Change item to Lights.
  4. From the Neopixel menu, select the first block Neopixel at pin 0 and drop it onto 0.
  5. If you're using different NeoPixel lights, change 24 leds to the number of LEDs that you have.

Code_8

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:

  1. From Radio, drag out on radio received receivedNumber.
  2. From Logic, drag out if true then and put it under on radio received receivedNumber.
  3. From Logic, drag out 0 = 0 and drop it on top of the true.
  4. From Variables, drag out receivedNumber and drop it onto the first 0.
  5. Change the second 0 to 1.
  6. From Neopixel, drag out item show color red.
    1. Change item to Lights.
    2. Change red to blue (or whatever your favourite colour is!.)

Code_9

Turning the lights off:

  1. Select the blue toggle on the if statement and drag else if under the if statement.
  2. Select the blue toggle again to turn it off.
  3. From Logic, drag out 0 = 0 and drop it on top of the true of the else if.
  4. From Variables, drag out receivedNumber and drop it onto the first 0.
  5. Change the second 0 to 2.
  6. From Neopixel drag out item show color red.
    1. Change item to Lights.
    2. Change red to black.

Code_10

Tada! Simples!

Extra code

It's a good idea to have an off switch, in case the double clap doesn’t work!

  1. From Input, drag out on button A pressed.
  2. From Neopixel, drag out item clear.
  3. From Neopixel, drag out item show.
  4. Change item to Lights in both blocks.

Code_11

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.

Code_11a

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.

Complete_12

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.

Complete_12a

制造商零件编号 5625
ZIP HALO FOR THE BBC MICRO:BIT
Kitronik Ltd.
制造商零件编号 PIM355
ENVIRO:BIT
Pimoroni Ltd
制造商零件编号 MBIT0004
MICRO:BIT EVAL BRD
Pimoroni Ltd
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum