Skip to content



Intermédiaire | MakeCode, Python | Microphone, Écran LED | Capteurs, Opérateurs arithmétiques, Sélection, Variables

Étape 1 : Maîtrisez

Qu'est-ce que c'est ?

Measure how long applause - or any loud sound - lasts with this timer that uses the microphone on the new micro:bit.


Guide de codage

Ce que vous apprendrez

  • How to trigger events when loud and quiet sound measurements are made by the microphone
  • How to use the micro:bit's built-in timer
  • How to set the threshold for loud and quiet events

Comment ça marche

  • At the start of the program the threshold for triggering a loud sound event is set. Use bigger numbers so louder sounds are needed, smaller number for smaller sounds. You can use any number from 0 to 255.
  • A variable called start is set to 0. This is used for tracking when the loud sound began.
  • When the microphone detects a loud sound, the start variable is then set to the micro:bit's current running time and an icon is shown on the LED display so you know the timer has started.
  • Running time is a measure of how long your micro:bit has been running your program in milliseconds (thousandths of a second).
  • When the loud sound stops, a quiet sound event is triggered.
  • If there has already been a loud event, and the timer has started, the start variable will have a value greater (>) 0. In this case a variable called time is set to the new current running time minus the start time. This tells us how long the loud sound lasted.
  • Because the time is measured in milliseconds, the program divides it by 1000 to convert it to seconds and displays it on the LED display.

Ce dont vous aurez besoin

  • Un micro:bit
  • MakeCode ou éditeur Python
  • un boîtier de piles (optionnel)

Étape 2 : Codez

Étape 3 : Améliorez

  • Experiment with different thresholds for loud and quiet sound events to find the values that work best for you.
  • Add code from the Sound logger project so you can also measure how loud the applause was.