Skip to content

Activity

Clap hearts

Beginner | MakeCode, Python | LED display, Microphone | Input/output

Step 1: Make it

What is it?

Make the new micro:bit's microphone respond to claps and beats with an animated light show.

Introduction

Coding guide

What you'll learn

  • How computers take inputs, process them using code and create different outputs
  • How to use the new micro:bit's built-in microphone sensor to trigger events in your code
  • That the micro:bit's microphone can be used to respond to both quiet and loud sounds

How it works

  • When the microphone detects a loud sound, like a clap, it shows a large heart on the LED display.
  • If it detects a quiet sound, for example after your clap has finished, it shows a small heart.
  • The effect of this is to create a simple heart animation that responds to claps or strong beats in music.

What you need

  • new micro:bit with sound (or MakeCode simulator)
  • MakeCode or Python editor
  • battery pack (optional)

Step 2: Code it

1from microbit import *
2
3while True:
4    if microphone.current_event() == SoundEvent.LOUD:
5        display.show(Image.HEART)
6        sleep(200)
7    if microphone.current_event() == SoundEvent.QUIET:
8        display.show(Image.HEART_SMALL)

Step 3: Improve it

  • Create your own animation using other icons or drawing your own pictures.
  • Make the micro:bit respond to a loud sound by making a sound of its own. Does this cause any problems? How can you fix them?
  • You can change the sound level that triggers a loud sound event. This level is called a threshold. In MakeCode use the 'set loud sound threshold to...' input block to choose different sound levels to make it more or less sensitive to loud sounds.
  • In Python, to change the threshold for loud sounds use microphone.set_threshold(SoundEvent.LOUD, 128) - changing the number 128 to the value you want between 0 and 255.