Responding to sound with your micro:bit

The MonkMakes Sensor Board for micro:bit offers a variety of ways for your micro:bit to interact with the wider world.

In this blog post, we’ll show you how to use the Sensor Board’s microphone to listen out and tell you about loud sounds, such as a clap.

First, we’ll use just the Sensor Board and the micro:bit’s built-in LED’s to demonstrate sound activation. The second part of the post will extend this, using the MonkMakes RGB LED for micro:bit, to make a clap-activated light switch.

Things you’ll need:

Part 1: Simple Sound Activation

In this first section we will begin by using the Sensor Board’s microphone to detect a loud sound and display a pattern on the micro:bit when this happens.

Connecting the micro:bit

  • Connect the 3V pin on the micro:bit to one of the 3V pins on the Sensor Board.
  • Connect the GND pin on the micro:bit to the GND pin below the 3V connection you use.
  • Connect pin 0 on the micro:bit to the microphone pin on the Sensor Board (see picture).

Note that here we are using a Charger for micro:bit so that the project can run from a rechargeable battery. You don’t have to do this, you can just connect the micro:bit directly to your computer.

Note: the leads do not need to be the same colors as the ones we’ve used, although it is usually a good idea to only use red leads for 3V and black for GND. Additionally, you can use any of pins 0, 1 or 2 to connect to the Sensor Board’s microphone as long as you change the pin number in the code too.

The Code

Here is the code used for this example:

Having a small loop to find the highest sound value recorded may seem a bit odd at first, but if you remove it and just set the reading to the analog pin each time in the forever loop, you will struggle to detect any claps. The reason for this is that the clap only causes a thin pulse of sound. If you read from the analog pin in the forever loop, the micro:bit is not quick enough to reliably catch the thin spike resulting from the clap as the other sections in the loop slow down the sampling rate. Therefore, by using a tight loop to do the sampling, the sampling itself is much quicker and can more easily detect the spike in sound. We use the maximum value recorded to get the top of the spike.

If the loudest sound recorded in any given run of the loop is above a threshold value, then this will trigger the if statement and show a pattern on the micro:bit’s LED’s to indicate that a loud sound has been detected. The threshold value used here is 700, but you can adjust this to make the system more sensitive i.e. quieter sounds will set it off by lowering the value, or less sensitive so that only louder sounds will set it off by increasing the value above 700.

Part 2: Sound Activated Light Switch

Now that we’ve got the sound activation working, let’s use it to make something a bit more practical a clap-activated light switch using an external LED. We will use the same equipment as the previous example but with the addition of a MonkMakes RGB LED for micro:bit and 2 extra alligator leads.

Connecting the micro:bit

To begin, connect the micro:bit in the exact same way as in part one. Next:

  • Connect the unused GND pin on the Sensor Board to the GND pin of the RGB LED.
  • Connect pin 1 on the micro:bit to the Green┬áconnector on the RGB LED.

Note: You can use any of the three different colored pins on the RGB LED, we simply choose Green for this example. Similarly to part one, the colors of the leads do not matter and if you wish you can connect the RGB LED to any of the pins 0, 1 or 2 on the micro:bit as long as you change the code to reflect this. Here, we use pin 0 for the microphone and pin 1 for the RGB LED.

The Code

The code for this example may look a bit different to the previous example but this is mainly because we have moved some of it around. The loop that quickly samples the microphone to detect sounds has been moved to a separate function called getReading, which is then called in the main forever loop. The purpose of the reading loop is exactly the same as before, but by moving it to a separate function we can clearly separate the code into two distinct parts: one part to get the readings, and the other part to compare the readings and then take the appropriate action.

Once a sound is loud enough to get past the first if statement, the micro:bit LED’s display the same pattern as before and we then need to toggle the attached LED. On the first run of the loop, the variable status is set to 0 by default, using the on start block. This means that the first time a loud sound is detected, the second if statement will pass as status = 0. The LED is then turned on, and the variable status is updated to reflect the state of the LED (0 represents off, 1 represents on).

The next time a loud sound activates the first if statement, because the LED is already on, status = 1 so this time the LED will be turned off and the status set to 0.


If you want to explore using the sound activation further, why not use the MonkMakes Relay for micro:bit to light up a bulb or drive a motor? You could even connect two of the color pins to the RGB LED and set it to change color based on how loud the sound was.

By. Matthew Monk.