Create a Light-sensitive Alarm with micro:bit

Difficulty
Easy
Steps
19

A buzzer is an audio signalling device. There are two types of buzzers, a passive buzzer and an active buzzer. In this guide, we will learn to differentiate between the two, then connect a passive buzzer module to the micro:bit and get it to play a song and change its bpm with a button press. 

A light dependent resistor will also be added to the circuit, to create an alarm clock that automatically rings when it gets bright!

After completing this guide, you will understand the basics involved in using a buzzer, and can go on to make your own creations!

Parts Used in This Guide

Buy

Step 1  The Module

Let's take a closer look at the light-dependent resistor module as well as the buzzer module.

The light-dependent resistor module has four pins:

  • 3.3V  : While 'VCC' stands for Voltage Common Collector, we'll connect the VCC pin to 3.3V on the micro:bit
  • GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called  ground or GND.
    • Note: Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to. 
  • DO: Digital Output 
  • AO: Analog Output 

The buzzer module has three pins:

  • 3.3V  : While 'VCC' stands for Voltage Common Collector, we'll connect the VCC pin to 3.3V on the micro:bit
  • I/O: Input/Output 
  • GND

Note: There are two buzzer modules in the kit. Open up the 'Day 15' bag as well, one of the buzzer modules only has a + on the top as shown in the third image, while the other has 'HYDZ' written on it.

One is a passive buzzer while the other is an active buzzer. An active buzzer has additional circuitry in it, and it can generate its own tone. On the other hand, a passive buzzer is like a speaker, where a changing input signal produces the sound. If you put them side by side, the active buzzer is the slightly taller one. 

Step 2  Connect module to breadboard

Step 3  Connect + to VCC

Step 4  Connect GND to GND (light-dependent resistor)

Step 5  Connect P5 to DO

Step 6  Connect P1 to AO

Step 7  Connect + to VCC (Buzzer module)

Step 8  Connect P0 to I/O

Step 9  Connect GND to GND (Buzzer module)

Step 10  Connect 3.3v to +

Step 11  Test out the two buzzer modules

Before we begin, let's take a look at the difference between the two buzzer modules. 

Use this same MakeCode for both buzzers, swapping them out one after the other. Upload this MakeCode to the Micro:bit to hear the difference!

Using the passive buzzer module, you will hear a faint ticking sound as it does not produce its own tone. The active buzzer module will produce a loud beep. In the following MakeCode examples, we will be using the passive buzzer module.

Step 12  Power up sound

  • First, open up MakeCode editor and start a new project.
  • Let's test the buzzer with a power-up sound. 
  • From the MakeCode editor, click on the 'Music' tab.
  • Drag and drop a 'start melody ... repeating ...' block
  • Change it to 'birthday' and 'forever.'
  • Attach it to a 'on start' block.


Step 13  On button A press!

We can also change the bpm (beats per minute) of the tune. 

  • Change the 'start melody ... repeating ...' block to 'nyan' and 'forever'. 
  • From 'Input' tab, drag and drop a 'on button A press' block. 
  • Again from 'Music' tab, grab a 'change tempo by bpm' block and replace its value with '5'.

Step 14  Add variable 'lightVal'

You can play all sorts of sounds with a buzzer module and a micro:bit. But what about making a light-sensitive alarm clock with it? So let's add a light dependent resistor to the circuit! 

The alarm will automatically sound when it gets bright enough. 

We will set a threshold level value for this brightness level.

Firstly, create a variable for 'lightVal' which is short for light value. This will store the analog value rather than digital value (1 or 0) of the light level. 

  • To do so, go to 'Variables' tab and click on 'Make a variable'. Name it 'lightVal'
  • Click on 'Pins' tab and drag and drop a 'analog read pin P0' block. Change its value to 'P1' instead.

Step 15  Show number

To set the threshold level, let's take a look at how bright it is around your surroundings. 

So from 'Basic' tab, grab a 'show number... ' block, and add 'lightVal' as its value.

Step 16  The loop

We will create the loop that will run forever until the micro:bit is turned off. 

  • From 'Logic' tab, grab a '0 < 0' block. 
  • Change its value to 'lightVal < 600'
  • From 'Music' tab, grab a 'start melody ... repeating ...' block

Try changing the tune to a longer one such as 'birthday' or 'entertainer' then upload this code to your Micro:bit now. You might realise that only part of the tune plays.

To play a full tune, grab the next MakeCode!

Step 17  Play the whole tune!

Click the download button below to get this MakeCode!

Step 18  Use the active buzzer!

Optionally, if you want to use the active buzzer instead, you can. Click the download button below and use the active buzzer with this MakeCode!

Step 19  Upload the code

To upload the code to the micro:bit, follow these steps:

  1. Connect the micro:bit to the computer via microUSB cable
  2. Click on the 'Download' button and the hex file will be automatically downloaded to your 'Downloads' folder
  3. Drag and drop the downloaded .hex file to the MICROBIT drive
  4. Leave the micro:bit alone for a few seconds as it blinks. The code is uploading!

Once powered up, the buzzer will sound the alarm when it gets bright. Cover the light dependent resistor with your hands, and the buzzer will stop making sound!