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…