Category: Projects

April 9th, 2017 by Matneee

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.


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




Posted in Projects Tagged with: ,

April 6th, 2017 by Matneee

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!

Dimming Micro:Bit LEDs

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)

Putting it together

Nice and easy this bit. Using your crocodile clip jumper wires, connect the

  • 3V terminal of your Micro:Bit to the left potentiometer pin
  • GND terminal of your Micro:Bit to the right potentiometer pin
  • the 0 terminal of your Micro:Bit to the center potentiometer pin

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)

Making it do Stuff

Just a little bit of coding to do here, using the JavaScript Blocks Editor

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.

The explanation

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″

Wrapping Up

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.


Posted in Projects Tagged with: ,

Bodgebot 1
March 21st, 2017 by Matneee

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!

Micro:bit brains with Power Functions Brawn

What you will need

  • 1 x Micro:bit
  • 1 x Lego Power Functions M-Motor (part 8883)
  • 1 x Lego Power Functions Battery Box (part 8881)
  • 1 x Lego Power Functions Extension Wire (part 8886)
  • 1 x L298N Dual H-Bridge Motor Controller
  • A few jump wires (3 male – female, 1 male – male – I use these ones, but there’s plenty of similar choices out there )

Also a few wires with either small crocodile clips or banana plugs, and batteries.


  1. First things first – Power Functions generally runs at 9v, which is more than the 3.3v that the Micro:bit supplies, so we need something called a Dual H-Bridge Motor Controller. I used this one which cost about £6, although there are quite a few others out there that are basically the same. What the motor controller does is take instructions from the Micro:bit about how fast you want a motor to go, then gives power to that motor from it’s own electricity supply.
  2. You know that Power Functions extension cable you just bought? I’m going to need you to cut it in half. A pair of decent scissors at the mid-point of the wires should do the trick – they’re not particularly tough to get through.A stripped power functions extension cable
  3. Carefully separate the 4 wires out on the extension cable to about 1cm or so from where you cut (a craft knife will probably help here).
  4. Carefully strip the middle two wires about 0.75cm from the end, and twist the strands of wire together on each one. You should end up with something like the picture on the right.
  5. Repeat this for the second half of the extension cable, so you’ve got two cables with plugs.

The Power Functions Cable

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)Lego Power Functions pinout

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)

Putting It Together

OK, so it’s time to put your Stuff together!

  1. Have a look at your motor controller. If you bought the same one as me it should look like the picture below – if you bought a different one it should look similar, but refer to the instructions that came with it to identify the parts labeled.
  2. There should be 2 little plastic clips on the pins, called jumpers – take them off. You can still control motors if you leave them on, but what taking them off allows us to do is connect an extra wire to the motor controller telling it how fast we want the motor to go.
  3. Have a look underneath the Extension Cable plugs – one of them (usually the dark one) will probably have some metal connectors on it. Connect wire C1 to +12v on the motor controller, and C2 to Gnd. Later on we’ll connect this plug to the Lego battery box
  4. Connect the other cable up with the C1 wire going to C1 on the motor controller, and C2 going to C2. Connect your Power Functions motor to this plug. (Actually, it doesn’t matter too much which wire goes to which connector to the motor – if you find it’s going the wrong way when you use it just reverse the wires, or change the direction in your program)
  5. Now we need to connect this all up to your Micro:bit!

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:



Motor Controller


1 IN1
2 IN2


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!

Making it Do Stuff

Now all we need is a little code magic to make it all work! I’ve done the following in the JavaScript Blocks Editor on the Let’s Code page.

First you’ll need to set up 3 variables called

  • motorA1
  • motorA2
  • motorASpeed

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.

The explanation

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

  • motorA1 variable to Pin 1 of it’s edge connector. This is connected to IN1 on the motor controller and is always 0 or 1 (Off or On, basically)
  • motorA2 variable to Pin 2 of it’s edge connector. This is connected to IN2 on the motor controller and is always 0 or 1
  • motorASpeed variable to Pin 0 of it’s edge connector. This is connected to ENA on the motor controller, and is an analog signal, meaning it’s a number between 0 and 1023. This can also be called PWM or Pulse Width Modulation.

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.

Wrapping up

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…

Bodgebot 1

Posted in Projects Tagged with: ,