David PerksCreate Music with Light!

About the Author

David is an artist maker who has added electronic junk and microprocessors to his palette.

@pdbperks http://dperks.co.uk/

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:

  1. Use MakeCode.

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

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

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

  5. Select the Input menu; scroll to the bottom and select the ‘light level’ block; place this block in ‘ring tone (Hz)’ block in the slot that currently reads ‘Middle C’.

  6. Save the project, open the save folder, and 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.

Headphones attached to micro:bit

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.

Drawing of wiring the ground Pin

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.

Drawing of wiring the headphone jack

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.