BBC micro:bit
With A Buzzer

Introduction

These exercises all involve using the micro:bit with some sort of buzzer/speaker connected. There are various options for the circuit that will all work with these exercises.

  • Alligator clip headphone socket accessory and a pair of headphones.
  • Headphone hack - alligator clips directly to the jack on your headphones.
  • Buzzer connected via a breadboard and jumper cables.
  • Accessory which has a buzzer (eg Piano for micro:bit)
  • Accessory which has a speaker (eg Noise:bit)

The buzzer or speaker needs to be connected to pin0 on the micro:bit. If you are using one of the accessories with an edge connector, you will not need to worry about this.

Task 1

Copy and test the following program. Then read the explanations before starting the task.

from microbit import *
import music
while True:
    if button_a.is_pressed():
        music.pitch(440,-1)
    else:
        music.stop()
    sleep(20)

The first two lines import the modules we need, the microbit module as always and the music module to be able to play sounds. The program has an infinite loop. Inside the loop, the program checks to see if the A button is held down. If the A button is held down, a tone is played. 440 is the frequency of the tone and -1 means to play it forever. If the A button is not held down, all sounds are stopped.

Adapt the program so that pressing the A button starts the tone from playing. Pressing the B button should stop the tone. To do this, remove the ELSE clause and the statement that stops the tone from playing. Add an IF statement to check if button B was pressed. If it was, stop the tone from playing. This program will not work correctly if your second IF statement is not at the same level of indentation as your first.

Task 2

The following statement plays a 440Hz tone for 1000 milliseconds.

music.pitch(440,1000)

Adapt the following program to make a Morse Code sounder. The lines that start with a # are comments. Replace these with statements that do the job indicated by the comment. In Morse Code, a dash is meant to be 3 times longer than a dot. Around 100-300 milliseconds makes sense for a dot.

from microbit import *
import music

while True:
    if button_a.was_pressed():
        # play a 'dot' tone
    elif button_b.was_pressed():
        # play a 'dash' tone

Task 3

The following program defines and plays a music tune.

from microbit import *
import music

music.set_tempo(bpm=240, ticks=4)

tune = [
    'F#4:4','G4:4','A4:4','B4:12','A4:4','B4:4','E5:4',
    'D5:4','B4:4','A4:4','G4:4','E4:12','G4:4','B4:4',
    'C5:4','D5:12','E5:4','D5:4','B4:4','G4:4','B4:4',
    'A4:16','R:4','F#4:4','G4:4','A4:4','B4:12','A4:4',
    'B4:4','E5:4','D5:4','B4:4','A4:4','G4:4','E4:12',
    'F#4:4','G4:4','A4:4','B4:12','C5:4','B4:4','A4:4',
    'G4:4','A4:4','G4:16','R:4','D5:4','E5:4','F#5:4',
    'G5:12','F#5:4','F#5:4','E5:4','D5:4','E5:4','D5:4',
    'B4:4','G4:12','D5:4','E5:4','F#5:4','G5:12','F#5:4',
    'F#5:4','E5:4','D5:4','B4:4','A4:16','R:4','D5:4',
    'D5:4','D5:4','B5:12','A5:4','A5:4','G5:4','E5:4',
    'G5:4','D5:4','B4:4','G4:12','F#4:4','G4:4','A4:4',
    'B4:4','E5:4','D5:4','B4:4','A4:4','G4:4','E4:4',
    'F#4:4','G4:16','R:4']

music.play(tune)

Study the way that the tune has been defined. When you see something like 'C4:4', this means Middle C (4th octave on the piano), played for 4 beats. Use the table below to define the notes for a different tune.

NoteBeats
Middle C4
Middle C4
Middle G4
Middle G4
Middle A4
Middle A4
Middle G8
Middle F4
Middle F4
Middle E4
Middle E4
Middle D4
Middle D4
Middle C8
Middle G4
Middle G4
Middle F4
Middle F4
Middle E4
Middle E4
Middle D8
Middle G4
Middle G4
Middle F4
Middle F4
Middle E4
Middle E4
Middle D8
Middle C4
Middle C4
Middle G4
Middle G4
Middle A4
Middle A4
Middle G8
Middle F4
Middle F4
Middle E4
Middle E4
Middle D4
Middle D4
Middle C8

Task 4

Find some sheet music (or other musical notation) for a tune that you want to encode. All sorts of weird and wonderful tunes can be played, along with the classics (popular and classical).

Task 5

The X and Y axes on the accelerometer return values from about -1000 to 1000.

Look back at a program where you read from the accelerometer or check the following page, http://multiwingspan.co.uk/micro.php?page=pyacc.

Write a program with an infinite loop. Inside the loop, check if the A button is being pressed ('is' not 'was'). If it is, take a reading from the X or Y axis of the accelerometer. Write some code to convert the reading to a number that is positive - negative numbers are less than 0, multiplying a negative number by -1 will turn it into a positive number.

If the button is being pressed, use your accelerometer reading to play a frequency. You need the music.pitch statement to do this.

If the button is not being pressed, the music should be stopped.

Task 6

Copy and test the following code. It creates a dot and then moves it randomly every half a second.

from microbit import *
import music
from random import randint

x = 2
y = 2

while True:
    display.set_pixel(x,y,0)
    dx = randint(-1,1)
    dy = randint(-1,1)
    x += dx
    y += dy
    x = max(0, min(x, 4))
    y = max(0, min(y, 4))
    display.set_pixel(x,y,9)
    sleep(500)

The variables x and y, store the position of the dot. An infinite loop is created. Inside the loop, the pixel is 'undrawn'. 2 random numbers are chosen from -1 to 1. These numbers are added to the x asnd y coordinates. The max and min statements are used to keep x and y in the 0-4 range. The pixel is redrawn at the end of the loop and a half second pause happens before the loop repeats.

Start by adding a simple beep once in each loop. Use the music.pitch statement. Be careful with using timings - starting and stopping the pitch using -1 as the length allows the same timings to continue in the program.

If dx and dy are both 0, the dot doesn't move - so there should not be a beep. The dot does not move if it is on the edge and dy/dx would take it off the edge. This should also not get a beep.

The final refinement is to vary the pitch of the beep depending on the direction of movement. The direction of movement can be worked from dy and dx.

Task 7

If you vary the pitch of a tone quickly up and down across a large range of frequencies, you can make a siren sound. Use a for loop that counts from a few hundred to around a 1000. Use the music.pitch statement to play the frequency with no delay. Experiment until you have an annoying siren/alarm sound.

Task 8

Study the Morse Code program on the following web page,

http://multiwingspan.co.uk/micro.php?page=morse

Copy and adapt the program so that it makes the appropriate beeping noises. You should remove any statements that print - you will not see this output using the browser-based editor. You will also need to add a line calling the procedure into your program. On the web page, you see this being typed somewhere else. You should include it at the end of the program.