Create music with light!
Makes

Create music with light!

David Perks
David Perks

Three blocks of code and an earphone are enough to create this musical instrument. Or perhaps you will use it as a directional sensor in a maze game.  Its potential is up to your imagination.  One of the delights of the micro:bit is the ability to explore complex ideas using simple software and hardware solutions.  Use the MakeCode editor to create a program that would challenge a microPython or Arduino coder. Learn about the alternative functionality of LEDs and become a confident hardware hacker.

You will need:

  • micro:bit.
  • Earphones.
  • Paper.
  • Thin wire.

There are already guides to attaching headphones to the micro:bit and using the light sensor to create sound.  This article will show you how to achieve impressive results with the minimum of code and a rather neat hardware solution.

Software:

Use MakeCode.

Start a new file.  ‘on start’ and ‘forever’ blocks are automatically created.  

Drag the ‘on start’ block onto the menu; it becomes a bin; let go and delete it.

Click on Music menu; select ‘ring tone (Hz)’; drag it into the ‘forever’ block.

Select Input menu; scroll to the bottom and select ‘light level’ block; place this block in

‘ring tone (Hz)’ block in the slot that currently reads ‘Middle C’.  

Save the project, open the save folder, copy the file to the micro:bit.

Hardware:

One of the prime features of the micro:bit is its 5 x 5 array of light emitting diodes. This allows low resolution but effective visual communication to be delivered without additional hardware.  The designers have taken advantage of an interesting property of the LED; it will generate an electric current if a light is shone on it.  It is the LED array that provides MakeCode’ ‘light level’ reading: there is no need for an additional light sensor.

With the code in place, the virtual micro:bit on the MakeCode editor page shows Pin 0 and GND connected to the jack plug of a headphone.  If you have crocodile clip leads then this is the quickest way to test the circuit but it would be a shame to miss out on hardware projects due to a lack of crocodile clips.  A quick headphone socket can easily be improvised: indeed, it could be seen as a better option.

The large pin sockets are slightly larger than a headphone plug: so use a thin strip of paper, the width of the socket, and fold it back on itself two or three times. Use this strip to wedge the plug firmly into the GND socket.  The paper will insulate part of the plug but there will still be enough contact for the circuit.

Connect a thin wire from Pin 0 to the tip of the headphone plug.  If you do not have thin wire, try using a coated food tie.  This is actually a good option; the wire is just the right gauge for the job.  Strip 15mm of insulation from each end of the wire to allow a good connection.  

Start by attaching the wire to Pin 0.  Put the wire through the hole to the side and twist it tightly: it should sit safely just above the small Pin 3 but you could cover that with tape if you wanted to be extra careful.

Wrap the other end of the wire around the tip of the headphone plug: the tip has a notch that makes this task easy.  

Action:

Power up the micro:bit and expose it to light to generate sound. The stronger the light, the higher the pitch.   Use your hand over the LEDs to play the instrument.

Torches with strobe effects are great fun.  If you want louder noise then you can use the same method to connect the micro:bit to an amplifier using a suitable phono cable.