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…
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…