18
Nov
19

Making music with the BBC Micro:bit, Pt 1

Recently I was lucky enough to be given a BBC Micro:bit as a present.

What is a Micro:bit? – It’s a micro-sized computer, half the size of a credit card, with a row of input and output pins along one edge, Bluetooth capability, a display consisting of 25 LEDs in a a 5×5 pattern, and able to be powered and programmed via USB.  For ultimate simplicity, it can be programmed directly from the website www.microbit.org, via WebUSB using the Chrome web browser.

When it was introduced, its purpose was to ‘encourage children to get actively involved in writing software for computers and building new things‘, rather than being merely consumers of media, and around a million of them were given away to schoolchildren by the BBC.

Once I found out that it was considered suitable for primary school children, I was greatly encouraged that I would be able to write programs for it; and, in fact, there is a particularly straightforward way of doing this (as well as Javascript and a version of the Python programming language): that is, the Microsoft MakeCode system, which involves arranging and sequencing interlocking ‘blocks’.  You still have to have the programmer’s mentality, but without having to remember the precise wording and syntax.

As shown in the pictures above, The Micro:bit has a row of about 20 pins on its bottom edge, which have different digital or analog functions, and 5 of them, in particular, are easily accessed with crocodile clips or 4mm banana plugs.  These are +3v (actually 3.3v) and Ground, and analog pins P0, P1 and P2.  As a matter of fact, these last 3 pins can be operated simply by touching them!  You may have to touch Ground with your other hand to make sure they work, though.

Two tactile switch buttons are also available for incorporation into simple programs, and there is a tactile ‘Reset’ button on the back, which causes the Micro:bit to reboot.

For more complex applications – which can even include operating external devices such as servos – a range of edge connectors is available, which can aid permanent connections or take advantage of the smaller input/output pins.

There seemed to be several different ways in which I could make use of the Micro:bit’s capabilities.  First of all, it can, with a speaker attached directly to one of its analogue pins (typically pin P0), make monophonic square-wave sounds of its own, including playing a number of tunes stored in its own memory; secondly, it can be programmed to output MIDI messages; and thirdly, it can utilise and output data from its onboard detectors, which include light and temperature sensing, a three-dimensional accelerometer, and a compass/magnetic sensor.

I decided to start by using the Micro:bit’s internal sound-generating capabilities.

For this, I decided to make a module into which the Micro:bit could be inserted, and which would contain a power supply, speaker and various input and output sockets – all the hardware needed for a free-standing Micro:bit-controlled musical instrument.  That is the subject of this post.

*

I began with an empty Stylophone case, left over from an earlier project.  I left the speaker in, and the two slide switches on the front left.

In order to incorporate the Micro:bit, I bought a ‘proto-board’ – an edge connector into which the Micro:bit slots at a right-angle, with a section of stripboard attached for ancillary circuits to be built on.  I chose the right-angle one because I needed the Micro:bit to be visible when in use, and to have access to the front and back of the Micro:bit where its sensors are located.

I used this board for a couple of things: a simple op-amp based buffer/voltage follower, and an audio output transformer to be used with an alternative to the speaker, a piezo element.

I also bought a sturdy protective cover for the Micro:bit to ensure that it suffered no damage while being plugged in and out.  This particular type of cover leaves the edge with the pins exposed for just this sort of application.

This picture shows the board in situ, kept in place with matchsticks used as locating pins.  The transformer and op amp are on the right-hand side; on the left are a 4-pin i.c. socket and capacitor left over from an experiment with an audio amplifier, which I decided not to keep.

The voltage follower is the simplest possible op-amp circuit:

I used one half of an NE5532, as that’s what I happened to have available, but any op-amp can perform this task of ensuring the output has a suitably low impedence level.

I’m not sure how useful the transformer is in this exact context, but I learned of its use with piezos from this Nic Collins video on YouTube and had bought a handful of them for this kind of use.

These pictures show how the piezo was fixed into the former Stylophone case, from the inside (L) and outside (R) in the place previously occupied by the tuning knob, which protruded from the bottom of the case.

The Micro:bit can also be powered through thie proto board: mine had a terminal block already installed in it for +3v and Ground; I powered the unit with a PP3 battery, as I am wont to do, so added a small adjustable voltage regulator before the terminal block, set at 3.3v.  This meant that the Micro:bit was receiving its correct voltage, but more volts (up to 9v) would be available for any additional circuitry required.  Two LEDs show whether the 9v and 3.3v parts of the circuit are powered up.

While working on the inputs and outputs, I added the following: an external 9v power in socket; send and return sockets so that external tone controls (such as these) could be used; sockets for an external speaker or headphones and an external piezo; a line out socket for easier mixing or recording; and two sockets connected to analog input pins P1 and P2.

Pin P1 was connected to a 10k lin potentiometer, one end of which was connected to 3.3v, the supply voltage for this part of the circuit, the other to 0v; in this way a continuously variable voltage could be available at pin P1 for control purposes: the Micro:bit would interpret this as a number between 0 and 1023.  If something was plugged into the P1 socket, the potentiometer would be disconnected and the external signal read by the pin instead.

Pin P2 was not connected, unless something was plugged into the P2 socket.  A second 10k lin potentiometer, set up exactly like the one for P1, was added, terminating in a 3.5mm mono plug.  This was available to be plugged into the socket and used for the same purpose if a program were to require it.

In use, I found I had problems with the circuit if the PP3 battery wasn’t new or, if rechargeable, well charged; fortunately, with the external socket it would be possible to use a mains adapter.

*

I set the mIcro:bit up to make some trial tones, but the sound was rather quiet, through both speakers and piezo, so I added a small amplifier.  This was a very economical (99p) module from a UK eBay seller, based on the PAM8403 chip.  This is a 3W stereo amplifier chip, but I would only be needing one of the channels.

It required a 5v supply, so I powered it from the 9v source and added a small 7805 5v regulator.

7805

I put a 10k log volume control before the amplifier input, with the line out socket between the volume control and the amplifier input.  If a plug is inserted in the line out socket, the signal to the amplifier is cut.

After the amplifier there is a 3-way switch to choose between internal speaker, external speaker or headphones, and piezo element.

The following picture shows the majority of the interior at the end of phase 1 of construction (but before I added the two LEDs):

Features marked: 1 = the original Stylophone switch PCB, repurposed for 9v on/off and 3v on/off; 2 = volume control; 3 = the original Stylophone speaker; 4 = the 5v voltage regulator and amplifier PCB; 5 = the 3.3v voltage regulator PCB; and 6 = the 3-way audio output switch (internal speaker/external speaker or headphones/piezo element).

On the exterior I added three tactile switches to replace the two switches A and B on the front of the Micro:bit.  The first two switches simply duplicate the effect of the two switches on the Micro:bit; with the aid of a couple of diodes and a 4066 digital switch i.c., the third tactile duplicates the effect of pressing A and B at the same time, an action recognised by the Micro:bit.

I didn’t add any pull-up resistors on the outputs of the 4066 as the Micro:bit already has these internally.

*

After this I had a working module, so it was time for some programming!

*

I started with a couple of simple programs to make use of the light sensitivity and magnetometer functions.

While experimenting, I connected the Micro:bit to my Macbook with a USB cable, opened the Chrome web browser and wrote the programs using MakeCode on the microbit.org website.

The following picture shows the main features of the coding page:

1 = the simulator.  Most – but not always all – of the coding you write is automatically simulated here.  As you can see, the first instruction of my program, to display the number 1, is shown, as is the indication that I have chosen instructions which require the speaker to be attached;

2 = the button you press to load the program into your USB-connected Micro:bit.  WebUSB has not yet been implemented in most web browsers, which is why I use Chrome to do this.  Incidentally, if you’ve made any syntactical errors in the program, you will be told when you click this button;

3 = the area to name and save your program as a hex file.  The alternative way to program the Micro:bit is to open its window and drag and drop a hex file onto it;

4 = where you choose to program using the blocks, as I have done, or to write the code in Javascript;

5 = the area where the program is written.

*

As for my first program: the light control on the Micro:bit takes advantage of the fact that if the current to an LED is inverted, it becomes (slightly) sensitive to light.   The Micro:bit has an array of 25 LEDs on it, and it’s possible to use 9 of these together to record light levels on and around the device.

I found that the most useful way to use this feature was to set the Micro:bit up to sense the ambient light level, and then react to brighter lights being shone onto the LEDs, rather than the other way round (i.e. reacting to being shaded from the ambient light).

In this case, the reaction I programmed was to sound a note, then increase the pitch of that note as the light level increased.  Initially I just used the torch in my mobile phone, but I had already made 2 devices which could be used to control the Micro:bit in Light Reactive mode, the UFO and the Shuttlecraft.

As I said, the Micro:bit was programmed to start up in a waiting state and display the number ‘1’:

Then, when its Button ‘B’ was pressed – or the Button ‘B’ on the top of my module – it would run the part of the program related to the Light Reactive Instrument and display the number ‘3’:

The Micro:bit outputs a number between 0 and 255 to represent how bright the light level is.  0=dark and 255=bright.  The variable lightlevel in the program is set to be 20 times this number, i.e. between 0 and about 5,000.  The opening pitch chosen, 220Hz (the note A below middle C) and the range of values represented by lightlevel is designed to allow the Micro:bit to output pitches between 220Hz – a low to medium note – and about 5kHz – a very high note.

The variable lightambient is used to sample and remember the normal light level around the instrument on startup.  If the light level is no greater than this, the instrument makes no sound, the idea being that it should remain silent until a light is deliberately shone on it.

*

The second simple instrument I programmed was, in essence, the same thing, but reacting in this case to the presence of a magnet.

The Micro:bit measures magnetic force, as it says above, in microteslas (µT).  It turned out that the range of readings it gave did not need scaling or multiplying like the light level readings did, but produced a useful range of pitches without any changes.  So in this case the variable magforce was equal to the microtesla reading and gave rise to an output at the same Hz as the reading.

In this case, a magnet in the vicinity of the Micro:bit caused a pleasing arpeggiated effect, increasing in pitch the closer the magnet came to the Micro:bit, and decreasing as it was moved further away.

Again, the variable magambient was inserted in order to stop the instrument from sounding until the activating magnet was intentionally brought close to it.

I bought a neodymium (NdFeB) magnet especially for this purpose, as these are particularly powerful – up to 20 times as powerful as conventional ferrite (iron) magnets, in fact.

Neodymium magnets are graded from 28-52 according to their strength.  This one was an N52 (highest power) type.  It works very effectively, but has to be kept well away from any metal parts of the instrument – indeed metal parts of anything – as it will stick very easily and very strongly to them.  I wasn’t sure, but it even seemed to be affecting the speaker in some instances – a speaker, of couse, being driven by a magnet.

The only negative thing about this second instrument is that every time you reboot the Micro:bit, the compass/magnetometer has to be reset if you come to use it.  This is not, unfortunately, a simple matter, as you have to turn the Micro:bit every which way, with your progress shown by the LEDs lighting up one by one.  Only when all 25 of them are lit can you proceed.  This is obviously critical, if you genuinely want an accurate compass direction, but not so critical if all you want to do is make entertaining noises . . .

*

As I mentioned earlier, I had added two potentiometers connected to analog input pins P1 and P2.   I didn’t have an immediate use for these, but I thought it would be handy to have two variable inputs – for tuning or transposition, for example.

In preparation for Phase 2 of the instrument, I made use of the input to pin P1, and programmed it in the following way:

At the beginning of the program the potential input from pin 1, 0 – 1023, is divided into 21 equal sections, and scaled down to 0 – 20.  Each of the 21 divisions is numbered. ‘notenumber 0’, ‘notenumber’ 1′, ‘notenumber 2’, etc.

There are 20 notes on the former Stylophone keyboard, so there is one number for ‘off’, and one number for each note on the keyboard, ranging from 220Hz (‘Low A’) up to 659Hz (‘High E’).  When the potentiometer connected to pin 1 is turned fully anti-clockwise, the instrument is silent; as it is slowly turned clockwise, each note is stepped through in turn, until the highest note, ‘High E’ is reached.

This experiment proved that it should be possible to connect the keyboard to pin 1 and use the Micro:bit like a Stylophone.  This what I intend to do in Part 2 of this series.

Meanwhile, here are some recordings of these two instruments.  There are 8 short recordings, following this pattern: first the Light Reactive instrument through the internal speaker, external speaker, internal piezo element amd external piezo element; then the Magnet Reactive instrument through the same four media.

The recordings with the external piezo, glued to the bottom of the tin, are particularly interesting.  My experiments with piezos (beginning here), have so far only been in using them as microphonic elements, in particular as contact microphones, and as part of custom-made percussion instruments, and although the introductory article refers to their use as speakers, this is the first time I had actually used one that way.  Hopefully, there will be an opportunity to complete my survey of piezos by looking into this in more detail.

The tin, in fact, had been prepared for use as a kind of drum – and could easily function that way if attached to a suitable preamp.  It is quite large in size – 22cm (about 8.5″) in diameter and 14cm (about 5.5″) deep – and would make a very effective drum.  In this instance, using it as a speaker adds a noticeable reverb effect to the sound of the Micro:bit instrument.

 

 


1 Response to “Making music with the BBC Micro:bit, Pt 1”


  1. 1 ageing60hippy
    November 19, 2019 at 1:30 am

    WOW !!, You always amaze me. OZ


Post a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


andymurkin

November 2019
M T W T F S S
« Aug    
 123
45678910
11121314151617
18192021222324
252627282930  

Enter your email address to follow this blog and receive notifications of new posts by email.


%d bloggers like this: