Skip to content

Activity

Audible cookie tin alarm

Intermediate | MakeCode | LED display, Light sensor, Sound | Iteration, Selection

What is it?

Use the micro:bit’s light sensor to make an audible alarm to protect your cookies. Students will learn about selection in this fun project.

Audible cookie tin alarm

How it works

  • The program turns the micro:bit’s LED display into a light sensor.
  • Put the micro:bit into your cookie tin. When the tin is opened, the light sensor detects a change in light level and sets off the alarm.
  • The program uses an infinite loop or forever block to keep checking the light levels. Loops are also known as iteration.
  • Selection or an if then else block is used with comparison logic so that if the light level is above a certain amount, an angry face appears on the display and an audible alarm plays.
  • Press the reset button on the back of your micro:bit to reset the alarm.

What you need

  • a micro:bit
  • MakeCode editor
  • battery pack
  • optional speaker and crocodile clips if you are using a micro:bit V1

Code your project

Here are the blocks you need to make the project - now put them together.

Step-by-step instructions

  1. Open the project to find the blocks you need using the buttons above.
  2. Find the if then else block and put it inside the forever block.
  3. Find the 0 > 30 comparison block and put it onto the word ‘true’ in the if then else block.
  4. Find the light level block and put it onto the zero in the 0 > 30 comparison block.
  5. Locate the show icon (angry) block and put it in the if then else block underneath ‘if light level > 30’.
  6. Find the play melody dadadum block and put it directly underneath the show icon block.
  7. Now, whenever the light level is more than 30, your micro:bit will show an angry icon and play an alarm. But you must also tell your micro:bit what to do if the light level is not more than 30.
  8. Under ‘else’ in the if then else block, add the final clear screen block.
  9. Download the program to your micro:bit, attach a battery pack, and try it out!

Make it your own

  • Make the alarm more or less sensitive… by adjusting the number in the comparison block. Numbers larger than 30 will require more light to trigger the alarm. Numbers smaller than 30 will require less light to trigger the alarm.
  • Change the icon… using the options in the show icon block. Which icon is most likely to scare cookie thieves away?
  • Change the melody… by using the options in the play melody block. There are 10 melodies to choose from.

Example of a completed project

Take a peek at some completed code