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…