IAN LANG ELECTRONICS
In this topic we are going to enter the phase where we are sticking peripheral items on to the BBC Microbit. Well, to begin with, one peripheral item. Why are we sticking peripheral items on? Because the Microbit can be used to make musical noises. You can code up for as much beeping and booping as you like; you could if you had the time, patience and musical talent code it up to play the last movement of Beethoven's Ninth (although if you can make it sing in German convincingly you are better at it than I am) if you like - but your code's going nowhere if you can't make the output reach your ears. There follows a bit of a tangential wander off into what sound actually is. Chin up, it's good for your physics classes if you're at school and you can always skip this paragraph of peas and carrots and go where the meat is a bit further down the page. But you might miss out on a joke or two (which to continue the metaphor are the Yorkshire puddings).
Sound then. We all know what sound is. If we are Zen Buddhists we have often contemplated whether or not if a tree falls in the woods and there's no-one there to hear it, does it make a noise? We may also have considered the sound of one hand clapping. Personally I think that the answer to the latter is either "cl" or "ap" depending on which hand you use but Buddhists don't like it much if you tell them that. As to the tree in the woods thing it's actually a very good conundrum to serve our purposes. It probably doesn't because to make a sound two things have to happen. Firstly there must be some disturbance of air. Secondly there must be something capable of detecting those disturbances and turning them into signals that your brain can process. In the case of our tree this is the wheeeep-crack-thunk-a-dunk-whoosh-whoom-baroom-badoom-clunk-thumpa-wumpa-boom of several tons of wood suddenly finding itself in an embarrasingly horizontal position instead of the vertical it had previously enjoyed. It's caused by the movement of the tree and its outlying members moving through the air and causing that air to move out of the way. The quicker the air moves, the higher the frequency of the sound will be because what happens to that air next is that it hits a bit inside your earhole that's called the timpanic membrane (aka eardrum) which wobbles back and forth causing a series of signals that your brain interprets as the aforementioned wheeep-crack-thunk-a-dunk etc. If too much air hits your membrane too quickly it hurts and can lead to deafness. This is because as well as frequency the pressure level is important; how fast the air's moving on your eardrum determines the musical pitch and how much determines the loudness of the sound. The usual unit used to express sound pressure levels is the deciBel at one metre from the origin (dBm). The dB is a logarithmic unit and roughly speaking in electrical work for every three dB increase in sound you hear you are doubling the electrical power needed to do it. If over 100 dB hits your tabs over a sustained length then it's painful, if it's 120 then you are likely to go deaf as your eardrum may not recover. If it's 140 then you will almost certainly suffer a rupture in the membrane instantly and that's it for sound for you. Of course if the aforementioned tree itself hits your timpanic membrane then you will not have any problems with deafness. Or in fact with anything else. Your relatives will have a few things to sort out though and you will severely inconvenience the Forestry Commission. Which leads to Lang Buddhism's conjecture: If a tree falls in the woods and you are there to hear it get out of the way very quickly. The above also explains the tag-line to the first Alien film: "in space, no-one can hear you scream" - there's no air to disturb. Although having no air there leads to a more immediate problem of in space no-one can breathe either. And given the pressure differences inside and outside your body you'd probably explode. Not that it would bother you because you'd have frozen like an ice lolly before that. Probably a scientifically better line might have been "in space it's not terribly pleasant at all" but admittedly this has a somewhat reduced dramatic impact.
Back to that peripheral item then. You'll be pleased to know it's not a tree. Nor is it one hand clapping as that would not only be messy but would probably attract the attention of the local constabulary (or Garda, Heddlu, Gendarmerie or whatever it is called where you live). It is in fact a speaker. The speaker is the thing that's going to take the electrical signal and wobble about backwards and forwards like a reverse eardrum to cause disturbances in the air which your real, hopefully tree-free eardrum is going to detect and do exactly the opposite to reproduce the signals in your thinking organ.
On the left there is a diagram of a loudspeaker unashamedly borrowed from the training manual of the United States Navy Electrical and Electronic Training System (NEETS).
This is in fact a very big, high power speaker and we will be using something much smaller than this but which works on exactly the same principle and so this diagram is good for our purposes here and is in fact one of the best I've seen generally. I sometimes extract the Michael out of the Americans for their inability to spell the word "colour" properly (COLOUR - not color Hank) but their NEETS programme is genuinely very good and if you can find it on the internet then I recommend you read it if you intend to do Electronics or Electrical as an academic or vocational qualification as it's full of practical stuff that's most useful.
On the diagram you will see that a permanent magnet sits on a frame and immediately in front of that sits a voice coil and a cone. The voice coil is usually made of copper wire and is attached to the cone. The cone is rigidly spanned and attached to a basket (which for clarity is not shown here) making it stiff. The electrical signal is provided by an amplifier, and is a modified DC current which in
effect means it behaves in the same way as an AC current.
Passing any kind of current through a wire produces a magnetic field. If you coil up that wire you increase the strength of the magnetic field. If the current changes, the magnetic field changes; as the current gets higher the coil develops a stronger field and as it drops the strength of the magnetic field drops too as the coil tries to maintain the current. In electrical terms it's known as an inductor. Now here's the thing: as the coil becomes magnetic it reacts to the permanent magnet. It either pulls towards the magnet or repels itself away and it does this many times a second. As it does it wobbles the cone slightly. The cone wobbles the air. The wobbled air wobbles your eardrum. And your brain gets a sound. Changing from one kind of signal to another like this is called transduction, and the thing that does it is not unnaturally called a transducer. The loudspeaker is the transducer we are going to fit to the BBC Microbit to hear the sounds it makes.
Caveats are in order here before we delve in. Firstly watch your speaker specs. I used an 8 ohm quarter Watt mylar one for this game which I got for 80p from somewhere in Scotland. It does a job but doing it this way will not be loud. Remember that the Microbit only does 3V. We can address this problem later by sticking a few more bits on.
Caveat No. 2 is that it's not a good idea to solder things to your Microbit. Use some banana plugs or croc clips or, if you're anything like me, a couple of spade terminals connected to the Microbit I/O pins with some nuts and bolts................
A small loudspeaker such as the one shown above is designed to be compact and give a performance over a range of frequencies. It will have a frequency where performance is peak, and will, as it moves up and down the range, drop in performance. The frame here is attached to the basket (the big round part at the edge) and on the back you can see where the permanent magnet is fixed (the raised cylindrical bit) and the two lugs for attaching wires for passing the electrical signal. On the front you see the cone and in the centre a small, raised dimple. That's where the voice coil is. In the above example the cone is made of paper but you can get other materials; Mylar is common for lower-def speakers that may be operating in damp conditions and is used in those small headphone speakers that attach to mobile phones,
Most speakers are not polarised but just in case you have one that inexplicably is the positive end goes to pin 0 on the Microbit and the negative to ground.
Pin 0 is the only one that puts out sound notes at specified frequencies and sticking it anywhere else will not work.
Let's click together some code blocks to make it do something. Here we go:
So there are multiple ways to make a tone come out of pin 0 and the illustration above uses some of them. If you compile this into a hex file and sling it to your Microbit with the speaker attached (finished hex file can be got on the link on the left) what your Microbit will do is absolutely nothing. Until you press one or both buttons. If it's button A then it will make a beep at 1000 Hz (which is about two octaves up from middle C if you have a musical bent) for a quarter of a second (250 milliseconds = 0.25 seconds). If it's button B you press then it will beep at 500 Hz (an octave lower) for the same length of time.
If you press button A+B at the same time then it does a ring tone at a specified musical note, F#. Your problem with a ring is it does it until it's told to shut up. So I put a half second pause in there and told it to play another tone for 1ms which you can't hear but stops the fist tone.
You probably can barely hear this. Let's see to that issue with some electronic bits.Same code as above but we are going to attach some bits. It's not a good idea to solder anything to your Microbit and so I suggest you do this in one of two ways; either breadboard or stripboard the components and then attach them to the Microbit with leads terminating in croc clips or, like I did, some eye terminals soldered on to the ends of a wire and connected to the Microbit with nylon screws. Here's your circuit:
So here's the bits I put on. There's not much to it, it's three components worth costing at retail prices about £1.60 but you might get a lot cheaper than that if you look round. The speaker was a small mylar one with an 8 ohm impedance, the transistor I used was a TIP31c which is a bit meaty for this game but I shoved it in as I wasn't sure that a smaller one would be up to the job and I didn't want any pyrotechnics, and the resistor was a half-watter.
You'll notice the speaker is acting as a collector load, that is the transistor is acting as a switch between the speaker and its path to ground. Being NPN that transistor will not conduct without a voltage and current at the base. There is absolutely no bias to switch it on, the conductivity rests solely on the tones coming out of pin 0 and so does the current at the base. The current at the base controls that of the collector and as the hFe is about 100 for this kind that's what you get. The practical upshot is that the transistor reacts to whatever tone is coming out of pin 0 and recreates it through the speaker. In this way it is working much as a class D amplifier does and is more battery friendly. It's not going to win any awards from Hi-Fi magazines though.
If I run this project through the microbit with all the hardware above attached:
it allows me to press buttons and make things go beep whilst watching the temperature. If you read through the last bit before this you'll remember the temperature is inferred from the die on the processor chip. This allows me to watch the temperature on the chip and make sure I'm not pulling currents that are going to release magic smoke from the Microbit. Ten minutes beeping and booping did not increase the temperature by much, it went up one degree which might even have been due to the ambient. Either way I'm not worried about that. So, now for our next topic, let's see if we can make it do a bit of music.