Microbit
Beginner
60 mins
Teacher/Student led
+110 XP
What you need:
Chromebook/Laptop/PC
Microbit

Microbit Sounds

Start by creating a new microbit project named 'Music Maker'. Program the 'A' button to play a melody using 'music.play'. Use the 'B' button to play tones like C, E, and G. Set the A+B buttons to produce a laser sound effect. 
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    2 - Create a New Project

    Start by creating a new microbit project. Give it a name like 'Music Maker'.

    3 - Play a Melody

    Now it's time to add some music! We'll use the 'music.play' function to play a melody when you press the 'A' button. Here's the code:

    input.onButtonPressed(Button.A, function () {
        music.play(music.stringPlayable("- - - - - - - - ", 120), music.PlaybackMode.UntilDone)
    })

    The code above sets up the music to play when you press the 'A' button. The melody is represented by '- - - - - - - - ', and '120' is the tempo, which you can adjust to make the music faster or slower.

    To create your own melody, you can use different notes. Each note is represented by a letter (like 'C', 'D', 'E') followed by its octave (like '4', '5'). For example, 'C4' is the note C in the fourth octave. You can string these together to make a melody, like 'C4 D4 E4 -'. The '-' represents a rest or pause in the music.

    Alternatively, you can choose a melody from the gallery. To do this, click on the 'Music' category in the MakeCode editor, then select 'Melodies' and choose one that you like. You can then replace the '- - - - - - - - ' in the code with the melody you've chosen.

    Experiment with different melodies and tempos to create your own unique music!


    4 - Play Some Tones

    Now let's program the 'B' button to play some different tones. Here's how you can do it:

    input.onButtonPressed(Button.B, function () {
        music.playTone(Note.C, music.beat(BeatFraction.Whole))
        music.playTone(Note.E, music.beat(BeatFraction.Whole))
        music.playTone(Note.G, music.beat(BeatFraction.Whole))
    })

    This code will play the notes C, E, and G, each for a whole beat, when you press the 'B' button. You can change the notes and the duration to create your own sequence of tones.

    Experiment with different notes and beats to see what sounds you can create!

    5 - Play a Sound Effect

    Now let's program the A+B buttons to play a laser sound effect. Here's how you can do it:

    input.onButtonPressed(Button.AB, function () {
        music.playSoundEffect(music.createSoundEffect(WaveShape.Sine, 500, 0, 255, 100, 500, SoundExpressionEffect.None, InterpolationCurve.Linear))
    })

    This code will play a laser sound effect when you press both the A and B buttons at the same time. The sound effect is created using a sine wave with a frequency of 500 Hz, starting volume of 0, maximum volume of 255, and a duration of 100 milliseconds, followed by a 500 millisecond pause.

    Now, experiment with different settings for the waveform, frequency, and volume to see how they affect the sound. You can change the waveform to 'Square', 'Sawtooth', or 'Triangle', adjust the frequency to make the sound higher or lower, and tweak the volume to make it louder or softer. Here's how you can modify the code:

    input.onButtonPressed(Button.AB, function () {
        music.playSoundEffect(music.createSoundEffect(WaveShape.Square, 1000, 100, 200, 200, 300, SoundExpressionEffect.None, InterpolationCurve.Linear))
    })
    Try different combinations and listen to the changes in the sound effect!

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more