Teach kids to code with Lego Mindstorms

Find us a child who doesn’t like Lego and we’ll show you an alien in a child’s costume.

There’s no better way to inspire children, and introduce them to several key programming concepts, than Lego Mindstorms, which lets them build a robot from Lego, and program it too.

There are some caveats. First up, the Lego Mindstorms set is expensive: the lowest online price we could find was about £230 inc VAT, and if you walk into John Lewis you’ll find it on sale at a cool £300. The second is that younger kids will need assistance to build the models.

If you put the effort in, however, the range of possibilities available from the base set is incredible. As with all Lego, the only real limit to what you can create is your imagination (and how many extra kits you buy). Helpfully, you’re given a choice of five robot types to build from the off, such as the Gripp3r, a “strong robot that can lift and carry heavy things with its Grasping Grippers”.

Lego Gripp3r

With only one set of printed instructions in the box, you’ll have to go online to find out how to build most of the robots. As a result, you’ll need a computer or tablet to hand while you’re working, but Lego has made the task of creation less daunting by cleverly splitting the building process into separate missions. For example, when making Gripp3r, you start by making the gripping hand, then add extra parts over three missions to create the full robot.

Once you’ve finished the quite technical process of building your model – which uses Lego’s more skeleton-like Technic range of building blocks, 594 of which are in the box – you’re ready to create your program.

Programming

Mindstorms is supplied with a graphical programming environment based on LabView. Lego’s version has been adapted to work with Mindstorms sets, but you don’t need to buy a model to use it for free; you can download it here.

Similarly to Scratch, you build your program using colour-coded modules: “action” modules are coloured green, sensors are yellow and “flow-control” commands are orange.

In our robotic hand example, we want the motor to activate so that the fingers open; the control to make an airbrake sound; the fingers to close and then – after a pause – to open again. That requires five modules as shown below. Once you’ve followed the instructions to create this, it’s time to experiment with settings; we lost a seven-year-old for half an hour as she played around with the different sound effects.

If that looks too simplistic, fear not. LabView includes many advanced operations, and by the time you finish building Gripp3r, that simple set of modules has quickly become much more complex, with loops and switches aplenty.

More advanced projects include data logging, and there’s a colour, touch and infrared sensor in the box. You can also buy gyro, ultrasonic, sound, compass and accelerometer sensors directly from Lego.
Nor are you restricted to LabView. For example, ROBOTC is designed to create more complex programs that work with Lego Mindstorms.

Once you’ve finished creating your program, it’s time to give it a go in the real world. The key to this is the Mindstorms EV3 intelligent brick (which you can see at the centre of the snake robot, below).

Lego Mindstorms Snake

First you need to connect this to the model you’ve built via one of the Ethernet-like cables. There are four ports available – A, B, C and D – and if you look at the screenshots on the walkthrough closely you’ll see that we attach the motor to port A.

You then connect the EV3 brick to your PC or Mac via the supplied cable and press play; the program will automatically run.

It will also be stored on the block for you to control the hand (in our case) without being connected to the PC.

In the classroom

As with Scratch, you may find that your local secondary school has already invested in Mindstorms for education. Supported by a huge range of resources and extra kits, these offer a great way for classes to work together on a project. For example, some sets include mats – such as a space landscape and a green city – so that students can work together to create something larger than single robots.

There’s a vibrant community of Mindstorms builders out there, too, with Lego making it easy to upload and share your creations. If you want to see how someone built a guitar from the kit, or an earthquake detector, or a dice-rolling machine, it’s as easy as downloading their project.

Below, we introduce the way Mindstorms works with a more modest project you can even follow without a physical set.

Step one: Motor on

Download the software from here.

Click File | New Project and Close Content Editor to remove the box. Drag Medium Motor from the green Actions area and click it in place next to the play button. Hit the circular arrow to choose how long it will activate for, by the number of seconds, degrees or rotations. Click “75” and you can control the movement: positive equals forwards; negative equals backwards.

Step one

Step two: Clenched fists

For the purpose of this walkthrough, we’re going to assume you’ve built the gripper hand described. If we sent this program to the hand right now, it would clench the grip.

Add a Wait block, as shown above, and change the value to 2, so that it will pause for two seconds. We then add another Medium Motor block, but change the value to -75. In effect, our program will now clench the gripper, wait for two seconds, and then unclench.

Step 2

Step three: Going loopy

To make things a little more interesting, add a loop function. Drag this next to the play button and then reposition the other commands inside it.

By default, the loop will carry on for an unlimited number of times, so click on the infinity symbol. On the dropdown menu, you’ll see a number of options. You could opt for a intensity reading from the colour sensor, a touch sensor, a timer or more. For simplicity, choose a count of three.

step 3

Step four: Good job

To finish, we’ll add a bit of audio. Drag the Sound module from the green actions area to the right of the loop. The folder icon is shown by default, which means when you click in the rectangle at the top right of the box it will show the available sound effects and voices.

Here, we choose “Good job”, to celebrate our hand clenching and unclenching three times. But you could play a note or a tone – or a series of them to create a tune.

Step 4

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.