Introduction
Want to turn off some LEDs with a clap of your hands? With the micro:bit V2 it's easy!
Video Overview
Featured Document
-
-
Gather your components. You will need a micro:bit V2, a Bit Board, some Crazy Circuits LEDs, and some 1/8" Maker Tape.
-
You do need a V2 micro:bit as it has a built-in microphone. Previous versions will not work for this project.
-
Once you have your components, assemble the parts onto a LEGO baseplate and use Maker Tape to connect the LEDs.
-
-
-
If you've never used a micro:bit before you'll want to check out this guide: Bit Board V2 Setup and Use
-
We're going to load the following code for our The Clapper v3 program: https://makecode.microbit.org/_4q0MgbKfh...
-
Note! We've updated and improved this code! It now runs a calibration function when it starts to calculate the sounds levels in the room.
-
-
-
Let's take a closer look at the calibration! First, we'll set some variables...
-
The padding variable adds a little padding to the sound calibration we do with this function. It means the sound has to hit a certain value plus the padding to turn off the LEDs.
-
The soundCal variable will hold the final result of the sound levels we capture into soundCalAll. The soundCalAll variable is the sum of all the samples we capture
-
This "sampling" is common when working with sensors where you are reading values and basically creates an average from a number of sample results.
-
In the repeat loop we fill up the SoundCalAll (which starts at 0) samples number of times (10 in this case) and then divide the total SoundCalAll by samples to get soundCal.
-
You'll see we also add in our padding value in the equation.
-
The sound level measurement will be between 0 and 255 with 0 being very quiet and 255 being very loud. Feel free to play with the value of the padding variable to require more (or less) sound to trigger the LEDs turning off.
-
-
-
Once you've got your circuit built, and the code is loaded, you can test it out.
-
Power up your circuit using a USB cable plugged into the micro:bit or using a two AAA battery pack plugged into the Bit Board.
-
Shhh! Be quiet... The micro:bit will beep once, calibrate the room sound, then beep a second time, and turn on the LEDs.
-
Now clap your hands near the micro:bit and the LEDs should turn off!
-
But wait! The LEDs turn back on after a second, so get ready to clap again!
-
Keep clapping, my friend... Keep clapping.
-