dark mode light mode Search Menu
Search

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

https://www.amazon.com/LEGO-MINDSTORMS-EV3-Discovery-Book/dp/1593275323/ref=sr_1_2?ie=UTF8&qid=1506329047&sr=8-2&keywords=ev3+programming+books

The Art of Lego Mindstorms EV3 Programming

https://www.amazon.com/LEGO-MINDSTORMS-Programming-Full-Color/dp/1593275684/ref=sr_1_1?ie=UTF8&qid=1506329047&sr=8-1&keywords=ev3+programming+books

The Lego Mindstorms EV3 Idea Book

https://www.amazon.com/s/ref=nb_sb_ss_i_1_11?url=search-alias%3Daps&field-keywords=ev3+programming+books&sprefix=ev3+program%2Caps%2C194&crid=25KLEYR0YKV17

Collection of online video tutorials

http://www.drgraeme.org/EV3/EV3.html