2.1 Smart Doorbell
-
Have you ever thought about how a doorbell works? Like so many things in our lives, it’s also a computer.
A common doorbell will use buttons, remotely communicate with a receiver, play music, and be able to select different tones. With the right code blocks, we can do all of these things just using the micro:bit.
-
The micro:bit has a built-in speaker, which we can use to play music. You can use the music blocks to play individual notes, or create more complex compositions that include multiple notes of different lengths and pauses. The micro:bit also has a library of existing melodies that you can use if you would prefer not to create your own composition.
All music can be expressed in terms of notes and rests of different lengths, so while the micro:bit tools are pretty simple to use, you can create quite complex music with them if you choose to!
Here are some tips on how to use the micro:bit music blocks:
- You can find all of the code blocks for playing music in the Music menu in MakeCode.
- Use the play melody block to create a short melody of up to eight notes.
- Use the start melody block to play one of the songs from the micro:bit music library.
- Use the stop melody block to stop any melodies that are currently playing.
- Use the play tone block to play a note for a specified number of beats.
- Use the ring tone block to play a note forever.
- Use the rest block to create a break in a series of notes.
- Use the tempo blocks to make melodies play faster or slower.
- Use the set volume block to increase or decrease the volume of your speaker.
-
The Challenge
- When you press the A button, the micro:bit plays High E for 1 beat, then High C for 2 beats.
- Your doorbell chime plays at volume 75.
Required Device
- 1 micro:bit
Suggested Blocks
- on button pressed
- play tone
- set volume
Bonus Challenges
- How could you make a doorbell with two micro:bits that both ring at the same time?
-
The Challenge
- When you press the A button, the micro:bit plays a melody you have composed.
- Your doorbell chime plays at volume 75.
Required Device
- 1 micro:bit
Suggested Blocks
- on button pressed
- play melody
- set volume
Bonus Challenge
- Can you create a doorbell chime based on a well-known nursery rhyme? Try looking online for easy piano tutorials and see if you can recreate those melodies using micro:bit tones.
-
The Challenge
- When you press the A button, the micro:bit plays a song from the micro:bit music library once.
- Your chosen song only plays once when the A button is pressed.
- When you press the B button, the melody that is currently playing stops.
- Your doorbell chime plays at volume 75.
Required Device
- 1 micro:bit
Suggested Blocks
- on button pressed
- start melody
- stop melody
- set volume
Bonus Challenge
- How could you convert this doorbell into one that would work for someone who is deaf?
-
The Challenge
- When you press the A button, the micro:bit plays the currently selected song from the micro:bit music library.
- When you press the B button, a new song is selected from the micro:bit music library, and the currently playing melody stops.
- A number is shown on the micro:bit display to indicate which song is currently selected.
- Your doorbell chime plays at volume 75.
Required Device
- 1 micro:bit
Suggested Blocks
- on start
- forever
- show number
- on button pressed
- play melody
- stop melody
- set volume
- 0 > 0
- 0 = 0
- if true then else
- [variable name]
- set [variable name]
- change [variable name]
Hints
- This is an extra challenging challenge! If you aren't able to solve this one right away, you can move on to the next lesson and come back to it later.
- When you press the A button, the micro:bit plays the currently selected song from the micro:bit music library.
-
In this lesson, you should have learned how to use your micro:bit to play tones and melodies, how to set the volume, and how to create a song selector.
In the next lesson, we'll learn how to detect temperature, we'll meet some new devices, and we'll learn how to program the micro:bit to talk to those devices to trigger specific actions.