dark mode light mode Search Menu

Code Your Legos

David Luders on Flickr

Bringing Legos to Life with Code

Have you ever wished that you could bring your favorite Lego creations to life? Maybe not to life in the way that a plant or animal is alive, but at least to see them move across the room on their own, react to their environment, or even just to make a little noise? The Lego EV3 set allows us to do just that.

What Distinguishes EV3 from other Lego Sets?

For starters, the home EV3 set contains over 600 pieces, mostly similar to those in the Lego Technic line. However, it also includes a set of sensors (infrared sensor, color sensor, and touch sensor) and a “brain”. The brain is actually a small computer that connects to a laptop or PC with a USB cord and allows programs written on your computer to be downloaded to any EV3 project!

How Does it Work?

Through code, we can program motors to rotate at varying speeds, obstacles can be detected, and beginners as well as experienced coders can transfer their programs from a computer to a Lego robot. Although Lego created its own drag and drop language for the EV3 set, it shares many of the features of a standard, text-based, language. As such, when we program an EV3 robot we develop algorithms that include looping, conditional logic, variables, and math, and we can even write our own functions.

We use a loop in our code when we want to repeat a process over and over.

Conditional logic is used to specify a condition and have the robot react one way if the condition has been met, but another way if not.

We use variables to store numbers, words (AKA “strings”), and arrays.

We can add, subtract, multiply, and divide the numbers stored in variables or read in through sensors

Formula EV3 Race Car – Design by Lego Mindstorms EV3 Discovery Book; Laurens Valk

A Program With It All

In the past two issues of Beanz we explained a bit of the basics of the EV3 programming language. This month we decided to make use of a few more of the tools available to us. To do so, we built a race car robot that incorporates two sensors and three motors. All of the pieces for the body of the car come in the EV3 set. The infrared sensor sits on top which allows us to control the car with the infrared remote. The color sensor in the back gives the car the ability to detect and react to colors behind it. But first it must all be programmed!

Just about anytime steering is involved in a project, calibration is required. If we want our race car to travel straight, we must first program it to recognize the position of the wheels when the car travels forward. Similarly, we need to define “left” and “right” in order to turn the car. Each of these three directions requires a small program of its own, and they will all be used later to program the remote.

Putting these three programs together as is, in a loop that allows the remote to communicate with the infrared sensor, would make our program messy and challenging to read. Fortunately we can “cover up” the details of much of this code by building our own blocks and storing it inside. This is similar to writing a function in text-based languages such as Python or Java. If we name our blocks wisely, a person reading our program will look at the name of the block and have a sense of what it does without seeing the details of the code inside of it.

Below is the final program to calibrate the wheels and enable the infrared remote. It might look too easy to be true, however there is quite a bit of code stored in the calibrate block as well as in the remote block. In fact we created these blocks on our own so that our final program is clean and easy to read.

For those who are curious about how the wheels are calibrated and the remote is programmed, take a look at the code below:

Calibrate – The Reset, Left, Right, and Center blocks are our own creations as well. Each one of these has even more code hidden within it:

Code Inside the Remote Block:

And with that, our race car is ready to roll!

Learn More

The Lego Mindstorms Discovery Book


The Art of Lego Mindstorms EV3 Programming


The Lego Mindstorms EV3 Idea Book


Collection of online video tutorials