My week with Micro:Bit

10 thoughts
last posted Jan. 18, 2016, 11:01 p.m.

7 earlier thoughts


Project 4 - Music Player

I acquired some crocodile clips and decided to try out the music module with the microbit. The documentation explains pretty well how to use the module - you can play music by making a list of notes to play. There are also some ready tunes available for you to use.

Before I could test the music I needed to connect Marja into speakers. I had some old passive speakers (unpowered) that I used, but I tested with my headphones as well and they worked too (but the sound was way too loud to wear the headphones). Here is a picture of the speaker I used:

I also used 2 crocodile clip wires. The black one was connected to the Ground pin on the Microbit, and then to the bottom of the audio jack. The red one was connected to pin0 on the Microbit and then either to the top of the jack (to play music from left speaker) or the middle of the jack (to play from the right speaker).

The music module uses the pin0 by default, but you could change it to another pin too if you wanted to. I had some trouble with the music module at first as I did not realise it had to be imported separately, I'd been used to having everything I needed by starting the code with from microbit import *. With the music module I also needed to import music.

Playing music is "blocking" by default, which means you will have to wait until the tune finishes before you can do anything else. I turned this off using the wait=False parameter so that I was able to change songs in the middle of the tune if I wanted to.

I wanted to change the tune by shaking the device. I used the code Martin O'Hanlon came up with for his Magic 8-ball to detect if the device had been shaken, and it worked really well.

Source code can be found here.

2 later thoughts