Science Oxford Logo

CCC - Musical Microbits

Resources for the 'Musical Microbits' Creative Computing Club.

During this workshop, you will create a musical instrument by coding a micro:bit to bang, rattle or ring an object.

This page recaps what we discussed as a group.


Coding a micro:bit in Python

Open the new Python editor by visiting python.microbit.org/. Your screen will look something like this:

screenshot of the Python editor screen

Edit the code on the screen, then flash it to your micro:bit to see the changes.

Try:


Using a button as an input

Currently, your code runs forever. The micro:bit accepts inputs - for example, you can choose to run code only when you press the micro:bit's A button.

Find the Buttons section of the Reference menu.

Drag-and-drop the if button_a.was_pressed(): example into your code, so that it looks like this:

while True:
    if button_a.was_pressed():
        display.scroll('A')
    display.show(Image.HEART)

Try running your code in the simulator. What should happen is:


Indentation is very important - this is the gap on the left in the code. It determines when sections of code are run.

To make all of the code run when you press button A, and nothing to happen if you don't, edit the indentation, to make your code look like this:

while True:
    if button_a.was_pressed():
        display.scroll('A')
        display.show(Image.HEART)

All of your code should line up neatly.

Does your code work the way you expect?

Challenge: can you make something different happen if you press button B?


Using a servo motor

You have added inputs, to control when your code will run. Now you are going to add a new output - a motor.

Edit your code, so that it looks like this:

while True:
    if button_a.was_pressed():
        pin1.write_analog(35)
        sleep(1000)
        pin1.write_analog(85)
        sleep(1000)

You can keep your original code in there as well if you like, but be careful of the indentation, and think about the order your code will run in.

What this new code does, is send a signal to pin1 of your micro:bit - one of the gold teeth on the bottom.


Connect your motor to the micro:bit and see if your code works. Does your micro:bit wave to you?

photograph of a BBC micro:bit. A crocodile clip with a white wire is attached to the gold pin labelled 1, second from the left. A crocodile clip with a black wire is attached to gold pin labelled GND, furthest right.

For more details about this circuit, click here.


Challenge: You can make this code more efficient by using variables. Try setting a tempo variable, and using this inside sleep. You could even extend this further by letting the micro:bit randomly choose the tempo for you! For some help to implement this challenge, click here.


Using the speaker

Now we will add musical notes into the program - to start with, we'll time the notes to play whenever the servo motor moves.

Firstly, add this line above your while True loop:

import music

This line is needed to use the music note code.

Use this syntax to add a musical note:

music.play('c')

To add a 'C' when the servo moves one way, and a 'D' when it moves the other, the code would look like this:

while True:
    if button_a.was_pressed():
        pin1.write_analog(35)
        music.play('c')
        sleep(1000)
        pin1.write_analog(85)
        music.play('d')
        sleep(1000)

Challenge: to expand on this code, take a look in the Sounds section of the Reference menu. Could you make a list of notes to make a whole tune?


Adding an intrument

Now add a physical instrument for your servo motor to hit!

animated photo showing a servo motor ringing a windchime, the structure is make out of K'nex and the motor is secured with blue tack

To get your project working you will need to:

Challenge: Can you work with someone else to make an orchestra? Discuss your timings to make a good tune!