HBM004: An introduction to Robotics with LEGO® MINDSTORMS


An introduction to Robotics with LEGO® MINDSTORMS




Robotics mixed with our beloved bricks, can you ask for more?


Text and pictures by Koldo


Several times I have read in some forums or I have been asked about how to get started in robotics with LEGO® MINDSTORMS. For some people it is something attractive but at the same time it creates respect and fear: will I be able to do something? Or is it something frustrating and will I be overwhelmed?

In recent years, building and programming robots is changing from being an activity only undertaken by professionals to becoming a leisure activity and a hobby for many people looking at new technologies as something different from what a video game offers.

This article is intended for those without prior training in robotics programming who want to build and program their own robots with LEGO MINDSTORMS.

The first step in order to begin building and programming robots is to have a general idea of what a robot is, and what the tasks are that we will have to face in their development. That will be the main objective of this article, which will be followed by other articles that may be useful to have a closer look at building and programming.

What is a robot?

I was fortunate to be part of the technical jury of the of the FLL(1) qualification trials that took place in Pamplona in November last year. When we asked one of the teams what they had learned during the experience of participating in the FLL, the answer was that now they knew what a robot is. Till then, a robot was a machine with legs and arms that could carry out different actions (androids). Building and programming a robot taught them that a robot can be much more than just that. You can find different definitions, and one of them may be: A robot is an electronically controlled mechanical device capable of executing various actions according to a predetermined program and interacting with its environment. But perhaps the best way to learn about what robotics is the hands on approach, building and programming robots. First, let's see what the common elements that are we will find in all robots.

1. The sensory system.

One of the basic parts of a robot is the sensory system, and without it, it cannot perceive its environment, and therefore cannot respond to different stimuli. The robot receives information through the sensors, which allow it to measure distances, ambient light, noise levels, temperature...

The basic LEGO® MINDSTORMS (2) set has four sensors:

Touch sensor: it is basically a push button like what we use to activate a doorbell (more commonly called “bumper”). It has two positions: pressed and released. It can be used in a mobile robot to detect when it hits a wall.

Light Sensor: it measures the level of brightness converting the reading to a value between 1 and 100. Some basic examples are a robot that moves to a brighter area, a robot that detects if a light is switched on in a room and sounds an alarm, or the classic robot that follows a line on the floor.

Ultrasonic Sensor: It can measure the distance to an object that is no further than 250 cm. While the touch sensor needs to collide with an object, with the ultrasonic this is no longer needed. It´s like the system used by bats to avoid obstacles in the dark.

Sound Sensor: It measures the noise level in the environment and assigns a value to the reading between 1 and 100. It can be used in different ways, for example, to control a robot with claps, like some home lighting systems.

2. The motor system.

Apart from feeling, the robot must be able to respond to external stimuli, which means, it must be able to do something. In fact, it is possible to design a robot without sensors, but it makes no sense to not be able to do anything, even if only generates sounds.

To be able to do, we can use engines to move, open and close a gripper ... or whatever you want.

The motors of the LEGO MINDSTORMS system are designed in such a way that in addition to controlling the power and direction of rotation it is possible to control the magnitude of the rotation: either in time, number of turns or degrees (with an accuracy of 1 degree). This makes it a lot easier to control the movements.

With the NXT we can connect up to three motors simultaneously.

3. The brain.

To respond to external stimuli we must have the ability to make decisions, and that is what the "brain" or controller is for, which in the case of the new system is called LEGO MINDSTORMS NXT.

The NXT has three outputs (to which we can connect up to three motors) and four inputs (which allow you to connect up to four sensors). In addition to this there are other ways to communicate with the environment: a small speaker with limited power that can reproduce sounds, which are very useful in some programs, a display with a resolution of 64x100 pixels through which text or graphics can be shown, and a Bluetooth communication port that allows you to connect with other robots (send and receive messages), mobile phones or even connect with a GPS receiver.

The NXT can connect to the computer via USB cable or wirelessly via Bluetooth connection.

4. The skeleton.

Everything mentioned before needs a purposeadapted skeleton to support it. Depending on the desired goal, it could a wheeled vehicle, a biped or quadruped, a of brick organizer depending on their color... Especially in the case of mobile robots, it is important to have a strong structure. A mobile robot that can hit a wall must be built in such a way that is not disassembled during use. Both the Commercial and Educational LEGO MINDSTORMS sets offer a big enough parts set to start building robots, which can later on be completed further with LEGO TECHNIC parts.

5. Behavior.

But, what is a brain without intelligence? Nothing, so through programming we must define how the robot should behave when facing different stimuli.

To this end, LEGO MINDSTORMS offers a graphicalinterface software known as NXT-G. Series of blocks sorted according to what you want the robot to do generate a program that is transferred to the NXT, which executes it without the help of the computer.

A step by step example

Our goal is to build and program a robot that movesMINDSTORMSthrough a bedroom, corridor... and which when it crashes into a wall or another object is able to rotate and continue after avoiding the obstacle.

The basic process of designing, building and programming a robot is a trial-error-improvement procedure. In Figure 1 you can see an outline of the procedure.


1.- Design and building of a mobile robot.

When building a mobile robot, first of all we must define what kind of steering system we are going to use: a four-wheeled robot, with two steering wheels and two driven wheels, or a robot with a differential steering system, that means two wheels connected each one to a motor and a third supporting point, which could be a ball or a castor wheel. Moving the motors at the same or different speeds will make the robot go straight or turn.

For an easy start it is better to use differential steering. Doing otherwise complicates both the building and programming.

In this case, the best thing to do is to use the Tribot (one of the models included in the LEGO® MINDSTORMS kit).


Before beginning with the software, no matter which one, we must write the algorithm. What is an algorithm? It´s the step by step description of what the robot is going to do in a human language. In this case it might be:

1. Move in a straight line until you bump into something

2. Go back in a straight line for 1 second

3. Rotate 90 degrees to the left

4. Start the cycle again

Now that we have the algorithm we can turn it into a language understood by the robot. We will do that through LEGO MINDSTORMS NXT-G. One of the characteristics of NXT-G, unlike other programming languages, is that from the very beginning we can write fully functional programs, without the need of a process of prior learning, and it is aimed at all audiences.

A NXT-G program is a sequence of programming blocks attached to a link that simulates a liftarm. The blocks are dragged to the desired position and are linked automatically. In Figure 2 we can see the program for the previous algorithm.




Each programming block has a panel that allows its configuration. In the case of the “Move” block (Figure 3) we can change the speed, the time or if it should do it indefinitely, how to stop ... or if it should simply execute a turn of a specific number of degrees.

In this program, as shown in Figure 2, only two programming blocks and one control structure are needed. One of the blocks (Move) is the one that makes the robot go forwards, backwards, turn ... that is, it makes it move, and the other makes the program wait until the robot collides. The control structure determines the flow of the program. In this case, the Loop block repeats the sequence of blocks inside it again and again.

Once we've written the program, we only need to transfer it to the NXTLEGO MINDSTORM. You have to turn it on and connect it to the computer via USB cable (the fastest and easiest way) or via Bluetooth (sometimes the configuration is hard and it is slower). In Figure 4 you can see the buttons used for the transfer.

3. Testing.

Now that we have a robot with the program, we need to test it and see if it operates in the desired way. Probably the decisions taken concerning the timing of the back up and turning times did not give the desired result, so after testing we should adjust these times to reach the optimal results.

In many cases after testing the robot we should not only change the program but also the assembly: if the robot moves too slowly we may need to change the gear train or the size of the wheels. There may be parts that need to rotate or move more fluently...

Getting started

For beginners, the best thing to do is to take the guide in the NXT-G software as a starting point. It offers a series of models with their respective programs described step by step. This will help us understanding the different elements that make up the system, the use of the program editor and will help us to acquiring basic programming skills.

Starting from here, one of the recommended ways is to copy other people´s creations (books (3), Internet (4) ...) and after building the robot, read the program, analyze it and edit it. Simple copying wouldn´t help us to learn.

Another way, more complicated at the beginning, is to develop our own creations. The ideas can be found around us, on the Internet, or models from other people that may serve as a basis for developing our own.



1 FLL: FIRST LEGO® League. An international robotics competition organized by FIRST and LEGO. More information at http://www.roboteca.org/.

2 All the parts mentioned in this article are present in both the Retail and Educational version.

3 At http://wiki.lrobotikas.net you can find an English bibliography about LEGO MINDSTORMS. In a later article I will write about the most significant ones.

4 At http://lrobotikas.net you can find some of the creations of the author of this article and hi sons, some of them with step by step building instructions. A very interesting source is http://www.nxtprograms.com/. In all the projects it offers only the pieces contained in a Mindstorms set are used and NXT-G is used as the programming language. ■