Micro:bit vs Lego Power Functions Servo

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.

Steering a Lego Servo Motor with a Micro:bit

This one is slightly longer, but it’s not really any more difficult.

What you will need

  • 1 x Micro:bit
  • 1 x Lego Power Functions Servo Motor (part 88004)
  • 1 x Lego Power Functions Battery Box (part 8881)
  • 1 x Lego Power Functions Extension Wire (part 8886)
  • 1 x 10K Ohm potentiometer

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.

Preparation

  1. First off, take your extension wire and cut it in half. Then trim and strip the wires on each half (or modify ones used in the standard Lego Motor project) until you have something like the picture on the right. Note that the outside two wires are a little shorter than the other two – while it’s not strictly necessary to do this, it makes it a little easier to work with the cable without the wires touching and shorting out the project.
  2. If you remember from the normal power functions motor project, they use the C1 and C2 wiresLego Power Functions pinout of the cable to send power to the motor, and that is all it needs to set the speed. The Power Functions servo motor works a little differently. This time we need to send power to the servo using the GROUND (-) and 9 Volt (+) wires, and we use the C1 and C2 wires to control the position. Depending on the signal we send down C1 and C2, we can set the servo anywhere from 90 degrees left of center through to 90 degrees right of center. Well, almost – the Power Functions servo actually only has 7 positions either side of center, so that’s 15 in total (Have a look at THIS VIDEO for an example).
  3. To control the servo position we’re going to send a Pulse Width Modulation (or PWM) signal from the Micro:bit, along the C1 and C2 wires. The position changes depending on something called the Duty Cycle of the signal – the simple explanation of that is the duty cycle is how many of the pulses are ‘on’ compared to ‘off’ (it’s a bit more complicated than that, but that explanation will do for our project). So a duty cycle of 100% means that all the pulses are ‘on’, a duty cycle of 50% means half the pulses are ‘on’, and half are ‘off’, and a duty cycle of 0% means that all of the pulses are ‘off’.
  4. To control the servo direction, we need to switch the wire that the PWM signal goes down. To turn in one direction the servo expects C1 to have a PWM signal and C2 to have a Ground (or 0 volt) signal. To turn the other way C2 will have the PWM signal, and C1 will have the Ground signal.

Putting It Together

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!

  • Connect your Micro:bit to the 10K Potentiometer. If you are using a breadboard, connect it up like the diagram on the right, and the wires going off the top of the diagram connect to the pins on the Micro:bit. One thing about breadboards – the two rows of holes at the top (with a + and – symbol next to them) are called power rails. All the holes on the + row connect to eachother, and all the ones on the – row connect to eachother. This means that if we plug the 3V connector of a Micro:bit into the + row, we can connect lots of of different things to that connector at the same time by plugging them into another hole on the + row. Don’t worry about it too much now as I’ll explain how to use breadboards in another post.

If you are using Crocodile Clip Jump wires, just make sure that

  1. The CENTER PIN of the potentiometer connects to CONNECTOR 0 of the Micro:bit
  2. One of the other pins on the potentiometer connects to 3V on the Micro:bit, and the last potentiometer pin goes to GND (it doesn’t matter too much which way round the 3v and GND go – you can swap them if you want to change which way the potentiometer turns.)

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

  1. PIN 1 of the Micro:bit to WIRE C1 of the extension cable
  2. PIN 2 of the Micro:bit to WIRE C2 of the extension cable

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

  • The SERVO takes POWER from the BATTERY BOX
  • The SERVO takes INSTRUCTIONS from the MICRO:BIT (on the C1 and C2 wire)
  • The MICRO:BIT takes INSTRUCTIONS from the POTENTIOMETER
  • The BATTERY BOX and MICRO:BIT are NOT CONNECTED BY ANY WIRES! It only powers the servo and nothing else.

Making it do Stuff

Open up your JavaScript Blocks Editor (PXT) and lets start getting your creation doing something!

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 Explanation

The key to understanding this project is that

  • When we use the analog read pin Micro:bit block to read our 10K Potentiometer, it gives us a number between 0 and 1023 (see HERE for a reminder)
  • The servo is waiting for the Micro:bit to send a PWM signal sent down either the C1 or C2 wire from either Pin1 or Pin2. It expects the wire without the PWM signal to be the same as Ground
  • The direction of the servo depends on which wire carries the PWM signal – if it’s C1 it turns right of center, if it’s C2 it turns left of center.

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.

Wrapping up

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…

 

 

 

April 9th, 2017 by