Over the past week I’ve been helping Kirklees Libraries host some Micro:Bit sessions in the local area. They’ve been loaning out Micro:Bits in their libraries for a fair few months now and the idea is starting to spread further, with the Micro:Bit Foundation having recently provided a few Libraries authorities around the country with Micro:Bits in order to run a pilot lending scheme. I’ve had a lot of fun helping lead the sessions and it’s been a fantastic, rewarding experience. If you get the chance I can heartily recommend volunteering to help out at one. We’ve had a great response rate to an offer of further support by email, and I’ve included a further worksheet to compliment the Code Club project we based the session on. (There’s a link toward the end of this post, and please feel free to use it as you see fit.)
More than anything else, the sheer enthusiasm of the young people attending has blown me away. Most of the children attending had never seen a Micro:bit before so there’s been some understandable hesitance to begin with. But without fail they’ve all grown in confidence and understanding at tremendous speed. For me though, the magical moment has been that in almost every case there’s been a point where you can practically see a lightbulb go on over their heads where it all starts to click, and they start anticipating what comes next in the code before the worksheets tell them. It’s clearly a cause of excitement for the learner, and it’s genuinely wonderful for me to see as a tour guide on their coding adventure!
I believe smaller, local libraries can be fantastic venues from which to run these introductory sessions. Having fewer young people in the groups enables a more direct rapport with the children than you might get in a school classroom, and the immediate answers and explanations they can get pays off in spades in terms of keeping the momentum of their enthusiasm going. Every single child has been so eager to borrow a Micro:bit at the end of the session to continue at home. Honesty – one of the attendees even set me homework and has asked me for email help in taking his project further
Running The Sessions
For running the sessions, this seemed to work out really well for us:
The project chosen for the session was a 2 player reaction game that’s available on Code Club’s fantastic Projects pages – Number 6 on THIS PAGE. All in all I think this is a wonderful project to use at an introductory session. The children have universally loved making a physical electronic game they can hold in their hands and play together, and it’s got a great challenge at the end of the exercise. It should take your group nicely up to the 1.5 hour mark if you encourage them to try it, and it really is worth encouraging the children to try the “Keeping Score” challenge, although I do recommend being prepared with a solution beforehand so you’re better equipped to help puzzle it out.
There’s actually a solution available under the “Project Materials” section of the project webpage, although I came up with an alternative way of doing it during the first session which I rustled up into a worksheet extension the evening after the first session.
Actually, that worksheet extension has rather grown in size and scope since then. I gave out my email details to all the parents at the end of the sessions and asked them to get in touch if their children wanted any help or a few pointers. What’s been really fantastic is that they have been in touch in the days following. So I added a bit more structure to the worksheet and sent it out – a solution for the challenge, some alternative methods for parts of the code, and also included a way of doing a great idea one of our attendees had about making the project into a complete game by declaring a winner after 10 points. We’ve also had some wonderful feedback from parents saying how much their children enjoyed the session, in one case telling us their daughter hadn’t stopped talking about it since the day!
I’ve included the extra worksheet covering the score challenge plus a few other things. I’ve been emailing it out to parents on request, and please feel free to distribute it yourself to anyone you feel might be interested.
All in all I’d say the sessions were a great success. I really do believe they illustrate how lending Micro:Bits out through libraries can be a tremendous way to help young people learn about coding and digital making. I’ll certainly be running further sessions in our local libraries in the near future and I’d love to hear any comments you have from similar sessions yourselves.
Update – The code for this can be found HERE if you’re interested in having a look. It’s very much a first pass at time of writing, but it works well enough to get the idea.
Yup, that’s 1980s throwback gaming sensation Uridium. On a Micro:bit!!!
Well, no not really. I was trying to use an old Nokia 5110 LCD screen as a Micro:bit display in Micropython, so I could get it to output more info without plugging it into a PC. But it turns out it was so slow that I pretty much would rather have beat myself to death with my own spleen than use it. So I thought I’d have a quick try at making it run a little better.
Anyway, it worked rather well as it happen – the frame rates jumped up by about 3000%. Although I should point out they were less than 1fps to start with so let’s not go mad here. And I’ve now got the reverse problem – I had to put in a 25ms delay between screen updates to stop the LCD from blurring or ghosting or whatever the cool kids are calling it today.
I’ll do a proper write up of this when I’ve got a little more time, as I’ve done a series of commands for working with characters (including a decent font) that could also be useful. Which is what I was trying to do in the first place before I lost the plot and started shoving 80s video games onto it.
Of course, now I’ve got a decent set of character tools, I’m probably going to have to do a Roguelike…
Last time we looked at using a potentiometer to dim the LEDs on a Micro:bit. Today’s project shows how you can use potentiometers to control Stuff attached to a Micro:bit, so we’ll be using one Thing connected to the Micro:bit to control another attached Thing. This also seems like a good time to find out how to control the other kind of Lego Technics motor – the Lego Power Functions Servo Motor (part 88004). So far we’ve controlled Lego motors using the C1 and C2 wires of a Power Functions cable (have a look HERE if you need a quick reminder) and that works great for standard Lego Power Functions motors – the M, L and XL motors can all be controlled that way. The servo motor doesn’t work the same though, so we have to find a new way to make it work.
So what does the Servo motor do? Well, it’s a motor that starts at it’s center position and can move to any point 90 degrees left or right of that point, and hold its position. That makes it very useful for doing stuff like steering, and that’s what we’re going to do here.
This one is slightly longer, but it’s not really any more difficult.
What you will need
A few crocodile clip wires and / or jumper wires.
Optional – You might also want to start using a breakout board for your Micro:bit to make it easier to access the connector pins on it – Kitronik do an excellent one HERE – and it’s also worth buying a prototyping breadboard. Kitronik (again) have a nice connector and breadboard combo HERE, or it might be worth buying one of their Micro:bit Inventor’s Kits which also has some handy electronic components and a little book of projects with it.
This bit is actually a little easier than standard motors as we don’t have to use any kind of extra motor controller. I used a prototype Breadboard and Micro:bit breakout board to make connecting Things easier but you can actually do it just using crocodile clip jumper wires instead. A word of warning though – this Micro:bit project is about as complex as you can get without using a breadboard and breakout board, so it’s worth getting them and using this project to practice!
If you are using Crocodile Clip Jump wires, just make sure that
Now we need to connect the Micro:bit to the Lego Power Functions Extension Cable that we prepared earlier. Its probably easiest to use Crocodile Clip wires for this – if you plugged your Micro:bit into a breakout board just plug a couple of jumper wires onto the breakout board pins, the connect then crocodile clips to them. We need to connect
Let’s put all that into one table so it’s easier to read…
|Micro:bit Pin||Connects to…|
|0||Center potentiometer pin|
|1||Extension Cable C1|
|2||Extension Cable C2|
|3V||Left or Right potentiometer pin|
|GND||Left or Right potentiometer pin|
Finally connect the 9 Volt wire of the Extension cable to the 9 Volt of the Lego Battery Box, and the Ground Wire of the Extension cable to the Ground of the Battery Box. You can do this using crocodile clip wires to connect the ground and 9 volt wires of this half of the extension cable to the same wires on the unused half, then connect that to the battery box. You don’t have to use the Lego battery box for this actually – any battery holder that uses 6 AA batteries will do as long as you connect 9 volt to positive (+) and Ground to Negative (-) but if you do that you’ll have to connect and disconnect one of the battery box wires to turn it on and off.
Aaand connect your extension cable to the Lego Power Functions Servo Motor!
So you should now have something like this…
You’ll notice two things there. Firstly I’m not using the Lego battery box – I’m just using a 6 AA battery box (on the far left). I’ve also built a little contraption onto the servo motor to demonstrate it steering – you can do something similar if you like, or just carry on to see the motor work on its own.
The important idea to see in what we’ve built here is that
As usual, we’ll set up the variables we need first – there’s just one of them this time, called Steer.
Next we’ll get the Micro:Bit to display an Icon when it starts. You don’t have to do this, but it’s nice visible sign that its powered up (and a reminder to turn it off if you’re anything like me)…
And now for the main part of the code – the Forever Loop. Most of this is held inside a If…Then…Else block found in the Logic part of the blocks menu. Also look in the Advanced part of the menu to find the section about pins. Drag some blocks and put it together to make the following code.
Once you’ve done all that, save it, transfer it to your Micro:bit, turn on your battery box and you’re ready to go. You can now control the position of the Lego Power Functions Servo Motor by twiddling the potentiometer connected to your Micro:Bit!
The key to understanding this project is that
So at the beginning of the Forever Loop, we do an analog read of Pin0 to see where our potentiometer is turned to, and we store that number in the variable called Steer.
How do we figure out if the potentiometer is turned to the left or to the right? Well, we know the Micro:bit will read it as some number between 0 and 1023, which means that the center point must be halfway between these two numbers – 512. So if it reads as less than 512 if must be turned somewhere to the left, and if it reads as more than 512 if must be turned somewhere to the right. In our code we check this at the beginning of the If…Then…Else… loop using blocks saying If Steer > 512
Moving on to the Then section of our loop – this bit of code runs if steer is greater than 512. You can think of the analog set period block as setting how quickly the signal is transmitted. All we really need to know is the servo works well with it set at 1200 microseconds.
We know that if Steer is over 512 it’s turned to the right and we need to convert that number into something that says how far to the right as a number between 0 and 1023, which the micro:bit will send down one wire as our PWM signal. So we need to work out how far over the center point the potentiometer is, which we do by subtracting 512 from Steer (this is in the code as set Steer to Steer – 512). So this gives us a number between 0 and 512, which we can multiply by 2 in the next row of blocks to tell us how far to the right the potentiometer is turned using a nuber between 0 and 1023.
The last two blocks of the Then bit of our loop send the two signals the servo is expecting – one with the amount to steer, and one with a ground signal. Digital write P2 to 0 sends an “Off” signal down the C2 wire, which will do as our Ground signal. Analog write P1 to Steer sends our PWM signal down the C1 wire to say how far to steer.
So because the servo has a ground signal on C2 and a PWM signal on C1, it knows you want to turn right by the amount the potentiometer is turned past it’s center point.
To get the servo to turn left, we need to figure out how much less than 512 Steer is. BUT we also need to swap which wire has the PWM signal and which one has the Ground signal – this is what lets the servo know we want it to turn the other way.
In our code we don’t check directly if Steer is less than 512. We’ve already checked if it is more than 512, so the only other thing it could be is less than. What our If… Then… Else loop does overall is look at If Steer is over 512 – if it is more than 512 Then do the code in the Then section, otherwise (Else) is has to be less than 512 so do the code in the Else section.
The maths in the Else section is a little different – because the number is already below 512 we multiply it by 2 to get a number between 0 and 1023 straight away. Also the servo thinks that the lower the number the closer to the center point it is, but the potentiometer reading goes the other way (the closest number left of center is 511, then it turns further left through 510, 509, 508 and so on) so we take our doubled Steer value (something between 0 and 1023) and subtract it from 1023, which has the effect of reversing the value.
So now we’ve got out PWM value for left we send a digital 0 signal out Pin1 and down wire C1 to the servo, and the PWM analog signal out of Pin2 and down C2.
The Lego Power Functions Servo Motor is handy if we want to turn something by a precise amount, so while this project might seem a bit longer it’s worth doing. It’s not as long as it seems actually, and if you’ve followed the other projects so far you’ll have done a fair bit of all this before. But now you know how to control all the different kinds of Lego Power Functions motors using your Micro:bit – cool, huh?
The great thing about using a Micro:bit to control the servo though is that it can do it more precisely than the lego IR Remote Controls can. The standard lego remote turns the servo either completely to the left/right, or center, without being able to turn it to anything in between (no good for precise movements such as delicate steering). The Lego IR Speed Controller has dials that let you set positions between all the way left/right center, but I’ve found it doesn’t seem to keep up very well if you turn it more than fairly slowly (no good for faster steering). But if we build our own transmitter using a second Micro:bit and use it’s Radio function so the Micro:bits can talk to each other, we can have a Lego Servo transmitter/receiver combo that’s both accurate and fast.
We’ll come on to that very shortly…
Just a quick one here – I was going to go on to making tanks and buggies and cool stuff like that, but I decided I’d better explain a little bit about these Things first…
Thrilling, huh? Useful though, and we’ll be using them a lot although they won’t always look like that. That thing is a potentiometer, also known as a pot, a variable resistor, or a twiddly dial thing if you like. Whatever you call it, it’s job is to basically let you adjust how much voltage goes to something by turning the knob on it. Light dimmer switches use them, and little versions of them are what let the thumbsticks on joypads do analog stuff in games rather than just what you get on the direction pad.
So, let’s do a quick project so you can see how they work!
Very simple this. What we’re going to do is connect a potentiometer to a Micro:Bit, then use it to make an icon on the LEDs brighter and dimmer.
What you will need
1 x Microbit
1 x 10K Ohm Potentiometer (You can get a bag of 5 of them here for less than £3, or pick them up for about 60p each)
3 wires with crocodile clip (This sort of thing)
Nice and easy this bit. Using your crocodile clip jumper wires, connect the
Make sure that none of the crocodile clips are touching eachother!
It’s traditional to use red wires for positive connedtions (3V in this case) and black for negative (GND), and it’s worth getting into the habit of sticking to that. Your should end up with something like this. (right-click an open it in another tab if you need a clearer view)
First up, we need to set up one variable, called dial
Then put together the following bit of code (you can use whichever icon you like best)
(If you’re having trouble making the second bit of the Set Brightness block, what you need to do is drag a divide block from the maths section, drag that into the brightness block, drag dial from variables to the right position, then change the 2nd number to a 4.)
Once you’ve put that together, transfer it to your Micro:Bit, twist the knob on your potentiometer so it’s about halfway round, and once it powers up you’re ready to go!
You should now see the icon you chose on your Micro:Bit’s LED display. Now try turning the potentiometer left and right – as you do that you should notice the LED lights on your Micro:Bit get brighter and dimmer.
So what’s going on then? Well, it’s pretty simple really. When you tell your Micro:Bit to show things on its LEDs it usually does it at maximum brightness, but you can actually set it to be any level of brightness between off and on. In the Block Editor we use the Set Brightness block found in the LED section to do this (it’s actually in the more… part of the LED blocks if you cant find it). To set the brightness you change its variable to anything from 0 (off) up to 255 (full brightness) – so 128 would be about half brightness for example.
Now, the way we’ve set things up, the Micro:Bit finds out what the potentiometer is set to by using Analog read pin P0, and it sets the dial variable to whatever that happens to be. If you remember from earlier posts an analog value for the pins on a Micro:Bit is usually a number between 0 and 1023. But that means that that if you twist the potentiometer all the way round, the dial variable will be 4 times bigger than the LEDs maximum brigtness value (255, remember?) So what we do is add in that little maths block to the set brightness block, which tells the Micro:bit to set the brightness to the dial value divided by 4.
Basically the Forever loop really says “Read an analog value from the potentiometer on Pin0, and call that number Dial. Then set the brightness of the LEDs to the Dial number divided by 4″
So there you have it – that’s how we can use potentiometers as an analog Input device. This means that instead of having buttons that are either on or off (like the ones on the face of the Micro:Bit) we can have much more control over things like lights, or motors. Or you could use potentiometers to make a musical instrument where they change the volume and the pitch of note being played. Loads of stuff really – this little project is just the very start of a bagful of new possibilities being opened up. I used two of them to make a remote control for a tank, for example.
So, Lego Mindstorms. That’s a thing. And if you want an entry point into robotics you could do a lot worse. It’s easy to use, well supported, and customisable / reconfigurable in that way that pretty much only lego is. It’s also about £300 on the Lego website, which might price it a little out of the range of someone interested in dipping their toe into robotics.
One thing I’ve been wanting to do for a while is to figure out a cheaper alternative – something that gives you the same amazing range of lego parts to use, but for a cost that’s a bit more wallet friendly. As it happens Lego make a range of electric parts called Power Functions designed to motorise Lego Technics kits, and they’re relatively cheap. And I already had a Micro:bit, which has a load of input/output options available. So I decided to put the two together to see if it will work as a lego-compatible alternative to Lego Mindstorms.
I’m hoping this will open up some new options for both Micro:bit and Lego users. Let me know if you come up with anything – I’d love to hear about it!
What you will need
Also a few wires with either small crocodile clips or banana plugs, and batteries.
Lego Power Functions cables and plugs are wired up as follows (right-click the picture and open it in a new tab for a clearer view)
Contrary to what you might expect Lego Motors don’t use the 9v / Ground wires – they actually use the C1 and C2 wires. so as far as we’re concerned for this project…
C1 = + (the positive electric wire)
C2 = – (the negative electric wire)
OK, so it’s time to put your Stuff together!
Look at this picture – it’s the part of the motor controller with the pins on it. We need to connect the Micro:bit up to it as follows:
Do this whichever way you like best for connecting things – I just connected some of the jump wires to the motor controller, then used crocodile clip wires to connect those to the Micro:bit
MAKE SURE IT IS WIRED UP CORRECTLY – if it isn’t it might damage your Micro:bit!
First you’ll need to set up 3 variables called
Next, drag and drop some blocks to make the following code:
Once you’ve put all that together, transfer it to your Micro:bit, plug the last extension cable plug into the Lego Battery Box, turn it on by pushing the switch away from the connector plug, and the motor should start up! Pushing the A button on your Micro:bit will change direction – you should also see either an “F” or a “B” on the LED display depending on whether the motor is going Forward or Backward.
Just a few things here really. The way the motor controller works is by checking which one of IN1 or IN2 is being powered – if it’s IN1 it drives the motor one way, if it’s IN2 it goes the other way. The speed of the motor is set using ENA.
If we look at the “forever” loop of out code, we can see the Micro:bit sends
So the motor controller reads signals from the Micro:bit, decides which way to turn the motor by checking if Pin1 or Pin2 is switched on, then makes the motor go as fast as Pin0 tells it to.
The “on button A pressed” loop really just checks which way the motor is going, then changes which of Pin1 and Pin2 is turned on by swapping the motorA1 and motorA2 variables.
And finally, if you want to change the speed of the motor, just put in a different value for the motorASpeed variable.
So that’s the basic idea of using a Micro:bit to control Lego Power Functions motors. It’s probably cheapest to order the Lego parts directly from their website, and if you do that the total cost of this project will probably be around £25 (assuming you already have a Micro:bit).
You can easily add another motor to the controller using the connectors on the opposite side to the first motor, and it will be controlled using the IN3, IN4 and ENB pins on the controller. If you want to do that I’d recommend getting an edge connector breakout board to plug your Micro:bit into, as that will make it much, much easier to wire things up to it. I use one from Kitronic.
So now you can make lego do all kinds of things with your Micro:bit – drive a car, make a robot arm, connect sensors so your creation avoids obstacles. You could even use a second Micro:bit as a remote control for your creation, which is exactly what I’m going to talk about in the next post when I introduce you to this monstrosity…
For those of you who don’t know, a Micro:bit is a tiny computer. They’re about 4cm
by 5 cm and have Things on them which you can program to do Stuff. I realise this level of technical jargon may be off-putting for some, but that’s the gist of it and we’ll break things down a little as we go. They look like this, and they’re frankly brilliant.
So, as mentioned, it’s got Things on it.
That’s where you come in really. Basically you’re limited by your imagination and what you can find to hook up to it’s edge connector. You can control things with it, take readings with it, transmit messages and signals with it. All kinds of stuff, and I’ll talk about some Stuff I try later on.
As a final word I think it’s important to note that the Micro:bit isn’t the same as a Raspberry Pi. The Raspberry Pis of this world are more powerful, but they were designed to provide a cheap general-purpose computer for people to learn with and as such they’re a lot more like desktop PCs when you start using them. The Micro:bit is more specifically geared at providing a simple, pick up and go device for kids starting at around 11 years old, and it’s got a lot of great stuff in place to support their learning. But I really can’t stress enough how simple it is to get one, put some code together using the Block Editor in your web browser, then transfer that code to the Micro:bit to get it doing Stuff. We’re talking minutes from taking it out of the box here. Basically it’s borderline Sorcery.