Make Some Noise
It has all been very quiet so far. Attaching a buzzer to the micro:bit will allow you to get your micro:bit to make some noise.
You will need a buzzer or speaker to do this. A buzzer might cost you less than 50p to buy from an online store. You need one that works with 3V3 logic. Most of the cheaper buzzers work with 2V-5V and are fine. One of these will look something like this,
It has two metal pins at the bottom. One of the pins is labelled +. You connect this to pin 0 on your micro:bit. You can do this with the breadboard if you have the suitable connector. If not, use some alligator clips.
Connect the pin that is labelled - to GND.
Playing A Scale
In Physics, sound is a pressure wave travelling through air or water. The pitch of a sound that we hear is determined by the frequency of that wave. We can get the micro:bit to play musical notes through the buzzer or speaker if we specify the correct frequency for that note. Fortunately, the Code Editors have some tools to make this process easier.
With your buzzer connected, try some code like this,
This will play you the C Major Scale.
- You could get the micro:bit to play you any scale you choose. You could get it to scroll the name of the scale being played across the LED Matrix and then follow that with the buzzing. A more complex program could then be made with the buttons or accelerometer being used to allow a choice of scale.
The blocks in the Block Editor only have notes covering a single octave. C4 is middle C.
If you want to play a complex tune, you'll need to be able to play notes outside of that range. Here is the same scale an octave higher. By entering the frequencies instead of a letter (pick up value block from the Maths section).
Here is a table of the approximate frequencies of musical notes. In the note names, the letters A-G are used for the notes. The number is the octave for the note, with 4 being the octave starting with middle C. The letter 'S' indicates a sharp. The note is a semitone higher than its note letter and semitone lower than the next note. This means each note with an S in the table is also a flat of the next note.
The timings for the notes can be in milliseconds or, more handily if you want to play a tune, in beats. Setting the tempo in your script sets the length of a beat. Treat a crotchet as a quarter note and work out other note sizes based on that. Here is a small part of tune using this method.
- There are lots of web sites that will let you look at free sheet music, particularly for traditional songs. If you are happy reading or decoding sheet music, then you can work out the notes you need from that. If not, you could try one of the sites that has free online 'courses' or tutorials in instruments like the recorder or the flute. They will have some short tunes that are easy to follow.
- You can use the random number generator to make noise. Rather than generate a number for the frequency, it is best to use IF statements and match the random number to a set of notes. Generate random numbers for the number of beats for the note too.
- Randomness, even if you choose from a small range of notes, tends not to sound too interesting or melodic. You can get something more pleasant if you add more control to the process. Start by reducing the computer's composition to a fixed number of bars of music, no more than 16 of them. Instead of choosing each note at random, write statements to generate a shorter sequence of notes. Think about the different ways you combine a handful of notes, increasing or decreasing the pitch towards the end of a sequence ending in a longer note. Use the randomness, but if you want the sequence of notes to progress upwards, make sure each note is never lower or more than one note lower than the previous.