Micro:bit brains with Power Functions Brawn

Bodgebot 1

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.

Preparation

  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:

Micro:bit

connector

Motor Controller

Connector

GND GND
0 ENA
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 Micro:bit.org 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

March 21st, 2017 by