Make your microbit squeal

For this project, you will need:

  • 1 × micro:bit
  • 1 × Kitronik inventors kit
  • 1 × Audio amplifier (optional)

Connect the micro:bit breakout board to a piezo buzzer, as shown in the diagram below.

Make a new variable called hertz.

Inside on start, set hertz to the number 520.

Add a ring tone block, and plug the hertz variable into it.

Download the code to your microbit. When the microbit restarts, it should play a tone forever.

Let’s vary the tone when we tilt the microbit!

We’ll make it so that the lowest sound is a Middle C (260 hertz) and the highest is a High C (1040 hertz).

Remember those two numbers – they will come in useful!

Rather than setting the hertz value only once, when then micro:bit starts up, we want to constantly update the hertz based on the micro:bit’s current position.

So, move your code from the on start block into the forever block.

Replace the number 520 with a map block.

Map converts a number on one scale to a number at the equivalent position on another scale.

Find the acceleration (mg) x block, and plug it into the first part of the map block.

The acceleration (mg) x block returns a number between -1023 (when the microbit is tilted all the way to the left) and +1023 (when the microbit is tilted all the way to the right).

Type -1023 into the from low block, and 1023 into the from high block.

The lowest sound we want to play is Middle C (260 hertz), so type 260 in the to low box.

What do you think we should type into the to high box, so that it plays a High C?

If you’re stuck, remind yourself of some of the important numbers we mentioned earlier on!

Download the code, and tilt your microbit. The tone will go up and down as you tilt the microbit to the left and right.

Now for a challenge! The microbit can detect all sorts of inputs. Can you make the tone vary based on the light level reaching your microbit?

Remember, you’ll need to change the -1023 and 1023 numbers, to match the minimum and maximum values returned by the light level block. Try Googling “microbit light level” to read the documentation for the light level block, and see whether you can spot the minimum and maximum values!

Once you’ve made the sound change based on light level, can you work out how to make it change based on compass heading?

Just like last time, you can use Google to find out the minimum and maximum values returned by the compass heading block.

Once you’ve done that, how might you vary the tone based on how near one microbit is to another?

The following code blocks might come in useful:

Projects designed and written by &

CC BY-SA 4.0