LEGO-Mindstorms-EV3-Move-101-V4

Lego Mindstorms EV3 Programming 101: Moving your Robot with Examples

In Lego Mindstorms by Glenn TurnbullLeave a Comment

The most basic thing we need to do with our Lego Mindstorms EV3 robot is to make it move. In this post I’ll take you through the moving basics outlined below: 

To help you get started I’ve compiled all the example programs outlined within this post into a single Lego EV3 Programming Software Project which can be downloaded by clicking this link.

Getting Started

Before we delve into moving the robot, let’s start by building the same robot so that we will get the same results. The robot I chose is the Explor3r as it’s a quick and simple EV3 robot to build and it can be built using either the Education or Retail Lego Mindstorms EV3 kit. The build instructions from robotsquare.com can be found here.

EV3-Explor3r-Robot-Angle
Explor3r Robot

The Build should only take about 20 minutes for the base model. Don’t worry about adding the touch sensor or color sensor yet. We’ll cover that later in the post.

EV3-Explor3r-Robot-Front

Programming Options

Moving the EV3 robot can either be done with the Move Steering or Move Tank green programming blocks. The difference between the two is covered later in the article.

We’ll be using the Move Steering block within this article as it’s the easiest block to use for robots which use 2 large motors (where one controls the left side and the other controls the right). The Move Steering block is simpler as it will decide how to control both the motors at the same time depending on the turn or direction.

Move Steering Programming Block Basics

The Move Steering programming block can not only be used to move the robot in any direction, but it can also be used to make the EV3 robot stop. Here’s an explanation of the Move Steering programming block features:

EV3-Move-Steer-Programming-block-Overview
  1. Mode Selector
  2. Steering (direction)
  3. Power (Forwards or Backwards)
  4. Number of rotations or duration or degrees (depending on the mode)
  5. Brake at End: Coast or Stop
  6. Port Selector (for the 2 large motors)

Move Steering: Mode Selector

There are 5 different modes to choose from within the Move Steering block, each are outlined below:

EV3-Move-steering-modes

Off

Off mode will turn both motors off and therefore stop the robot. This is typically used in conjunction with On mode.

The only input available when the mode is set to Off is “Brake at End”. If the Brake at End input is set to coast then the robot will coast (roll) until it stops, if it’s set to brake then the robot will come to an immediate stop.

EV3-Move-Steering-Stop-Brake-Coast

On

The On mode will turn both motors on and move to the next block in the program. The motors will run until they are stopped by using the Off mode.

When On mode is selected the Steering and Power inputs are the only available inputs.

On for Seconds

On for Seconds will turn both the motors on for the number of seconds set within the seconds input and once the number of seconds has been reached, it will turn the motors off.

When this mode is used, the following inputs are available: Power, Steering, Seconds, Brake at End.

Note that the number of seconds input supports up to 4 decimal places, so it can be used if a precise measurement of time is required. It can also be wired into the programming block if it needs to come from a variable.

The example below will move the robot forward for 1 and a quarter seconds with a power level of 75:

EV3-Move-Steering-on-for-seconds

On for Degrees

On for degrees will turn both motors on and will wait for one of them to turn the number of degrees specified within the number of degrees input. 360 degrees of rotation is one full turn of the motor and the number of degrees is measured at the motor hubs.

This can be used to move or turn the robot a specific amount. The distance the robot will move can vary depending on the underlying gear ratios used on your specific robot i.e. wheel size, gearing, motor spacing etc.

The following inputs are used when On for degrees is selected: Power, Steering, Degrees, Brake at End.

Note that the degrees input will take a decimal point if an exact move is required.

The example below will move the robot backwards (note the power is a negative value) for 720 degrees i.e. 2 full rotations of the motors:

EV3-Move-Steering-on-for-degrees-backwards

It’s also worth noting that if the robot is turning then one motor will be turning faster than the other. In this case the number of degrees is measured from the motor that turns fastest.

On for Rotations

On for rotations will turn both the motors on until one of them has turned the specified number of rotations and then it will turn them off. This can be used to move or turn the robot a specific amount.

One rotation equates to 360 degrees, and it’s really just the same as “On for Degrees” but it’s a different unit of measurement. 

The following inputs are used when On for Rotations is selected: Power, Steering, Number of Rotations, Brake at End.

Note: The number of rotations input does accept up to 2 decimal places.

The example below will move the robot for 4.5 rotations forward with 100% power and then coast:

EV3-Move-Steering-on-for-4-rotations-forwards

It’s also worth noting that if the robot is turning then one motor will be turning faster than the other. In this case the number of rotations is measured from the motor that turns fastest.

Move Forwards

To move the robot forward, the power must be a positive number. A negative number will move it backwards and zero will not make it move at all.

By default, the Move Steer block is set to move the robot forward in a straight line for 1 motor rotation. Let’s get started by simply moving the robot forward in a straight line for 3 rotations at 80% power:

  1. Open a new project.
  2. Drag the Move Steering block from the green palette up to the right of the Start block on the programming canvas:
Mindstorms-EV3-Move Steering-Block-onto-Canvas
  1. Once the block is on the canvas update the Power value to 80 and the Number of Rotations to 3:
EV3-Move-Steering-on-for-3-rotations-forwards

Note: You can update the power by either using the slider or typing the number 80 directly into the input value:

EV3-Move-Steering-Power-Control
  1. Click the Download and Run button in the bottom right corner to send the program to your robot and test it out.
EV3-Download-and-Run-button

Congratulations! Your robot should have moved forward for roughly 40cm / 18 inches.

Move Backwards

Moving backwards is pretty much the same as moving forwards, however the Power input is a negative number.

Let’s give it a go by moving the robot back to the original starting position (that is if you have just completed the Move Forward instructions):

  1. Open a new project.
  2. Drag the Move Steering block from the green palette up to the right of the Start block on the programming canvas:
Mindstorms-EV3-Move Steering-Block-onto-Canvas
  1. Update the Power input to -80 and set the Number of Rotations to 3:
EV3-Move-Steering-on-for-3-rotations-backwards
  1. Click the Download and Run button in the bottom right corner to send the program to your robot and test it out.
EV3-Download-and-Run-button

Turning Basics

Now that we have moving forwards and backwards in a straight line sorted, let’s start to turn the EV3 robot. Turning is done via the steering input:

EV3-Move-Steering-steer-input

The steering input accepts numbers between -100 and 100 to control the direction:

  • 0 = Straight ahead
  • 1 to 100 = Turn Right
  • -1 to -100 = Turn Left

The steering numbers may be entered directly into the block or set via by wire input from a variable or something else within your program. If this number is not zero (0), one of the motors will be slowed down in order to make the robot turn. 

You may also notice that the arrow for the steering input will change to indicate the direction and type of turn as the number is updated. The closer the value gets to 100 or -100 the tighter the turn will be. Here are the turn ranges:

Steer Input ValueTurn Type
100 to 95EV3-Move-Steering-steer-100
94 to 63
62 to 37EV3-Move-Steering-steer-62
36 to 6EV3-Move-Steering-steer-36
5 to 0EV3-Move-Steering-steer-0
0 to -5EV3-Move-Steering-steer-0
-6 to -36EV3-Move-Steering-steer--6
-37 to -62EV3-Move-Steering-steer--37
-63 to -94EV3-Move-Steering-steer--63
-95 to -100EV3-Move-Steering-steer--95

If the number is close 100 or -100, the robot will turn on the spot and in this situation, one of the motors will be reversed in order to make the tight turn.

Turn Right

To turn the EV3 robot right a positive number must be entered into the steering input of the Move Steering block.

Let’s start by doing a gentle right turn:

  1. Open a new project.
  2. Drag the Move Steering block from the green palette up to the right of the Start block on the programming canvas.
  3. Set the Steering input to 6 and the number of rotations to 5:
EV3-Move-Steering-steer-input-gentle-right-turn
  1. Click the Download and Run button in the bottom hand corner to send the program to your robot and test it out.
EV3-Download-and-Run-button

Next let’s try make the EV3 robot turn a full 360 degrees to the right on the spot:

  1. Open a new project.
  2. Drag the Move Steering block from the green palette up to the right of the Start block on the programming canvas.
  3. Set the Steering input to 100 and the number of rotations to 3.3. With a bit of experimenting I found 3.3 rotations with the Explor3r robot was spot on for a full turn
EV3-Move-Steering-steer-input-full-right-turn
  1. Click the Download and Run button in the bottom right corner to send the program to your robot and test it out.

Turn Left

To turn the EV3 robot left a negative number must be entered into the steering input of the Move Steering block.

Let’s start by doing a gentle left turn:

  1. Open a new project.
  2. Drag the Move Steering block from the green palette up to the right of the Start block on the programming canvas.
  3. Set the Steering input to –6 and the number of rotations to 5:
EV3-Move-Steering-steer-input-gentle-left-turn
  1. Click the Download and Run button in the bottom right hand corner to send the program to your robot and test it out.
EV3-Download-and-Run-button

Next let’s try make the EV3 robot turn a full 360 degrees left on the spot:

  1. Open a new project.
  2. Drag the Move Steering block from the green palette up to the right of the Start block on the programming canvas.
  3. Set the Steering input to –100 and the number of rotations to 3.3. With a bit of experimenting, I found 3.3 rotations with the Explor3r robot was spot on for a full turn.
EV3-Move-Steering-steer-input-full-left-turn
  1. Click the Download and Run button in the bottom right corner to send the program to your robot and test it out.

Move Until Color

If you have been following this post from the top and using the Explor3r robot, it’s now time to add the color sensor to the front of the robot. Use the instructions from robotsquare.com here and scroll down to the last section – Building the Color Sensor Attachment. It’s very easy to add and should only take 5 minutes.

If you are using your own robot, make sure you have the color sensor attached to the front and it’s pointing downwards sitting about 2-3mm from the ground like this:

EV3-Explor3r-Robot-with-color-sensor

To move the EV3 robot until a particular color is detected we need to use a loop programming block from the orange palette, which looks like this:

EV3-Loop-programming-block
EV3 Programming Software: Loop Block

The loop block will continue to execute the code blocks we place in the middle until a condition is met. In this case, we’ll set it to use the color sensor to look for a particular color. When it detects that color, it will exit the loop and stop the EV3 Robot:

  1. Open a new project.
  2. Drag the Loop block from the orange palette up to the right of the Start block on the programming canvas:
EV3-Loop-programming-block-on-canvas
  1. Drag a Move Steering block from the green palette into the middle Loop and set the mode to On, direction to 0 and power 75:
EV3-move-until-color-step-1

If the program was to be run at this stage it will loop forever and hence drive forward forever. The next step is to set the loop condition to check the color sensor for a particular color. I’m going to use Red for my example:

  1. Click the infinity symbol on the loop condition and select Color Sensor | Color:
EV3-move-until-color-step-2
  1. Click the Color value on the loop and select the color you wish to detect. I’m going to use red.
EV3-move-until-color-step-3
  1. I’m going to stop the robot (not let it coast) so I’m adding another Move Steering block to the end of the program outside the loop, and I’ll set it’s mode to Off and Brake at End to True:
EV3-move-until-color-step-4
  1. Now it’s done! Place your robot on a course with your color in front somewhere. Click the Download and Run button in the bottom right corner to send the program to your robot and test it out.

I used the map that came on the inside of the retail Mindstorms kit and had the V3 Robot move to the first red line it finds:

EV3-move-until-color-robot test
EV3 Robot – Stopped at the first red line

Tip: You can select more than one color on the loop condition which means the program can exit if any one of the selected colors is detected.

Move Until Touch

If you have been following this post from the top and using the Explor3r robot, it’s now time replace the color sensor and add the touch sensor to the front of the robot. Use the instructions from robotsquare.com here and scroll down to the last section – Building the Touch Sensor Attachment

If you are using your own EV3 robot, make sure the touch sensor is on the front and facing forward and plugged into port number 1, similar to the Explor3r below:

EV3-Explor3r-with-touch-sensor

Just like all the “move until” type movements, to move the EV3 robot until it bumps something – we are going to use a loop block to keep the EV3 robot moving forward until the Touch Sensor’s state changes i.e. it’s pressed:

  1. Open a new project.
  2. Drag the Loop block from the orange palette up to the right of the Start block on the programming canvas:
EV3-Loop-programming-block-on-canvas
  1. Drag a Move Steering block from the green palette into the middle Loop and set the mode to On, direction to 0 and power 75:
EV3-move-until-color-step-1
  1. Click the infinity symbol on the loop condition and select Touch Sensor | State:
EV3-move-until-touch-step-4

  1. Click the State input value on the loop and select the number 1 (Pressed)
EV3-move-until-touch-step-5
  1. Once the Touch Sensor is pressed I’m going to stop the robot (not let it coast) so I’m adding another Move Steering block to the end of the program outside the loop, and I’ll set it’s mode to Off and Brake at End to True:
EV3-move-until-touch-step-6
  1. Now it’s done! Place your robot on a course where it can hit something. Click the Download and Run button in the bottom right corner to send the program to your robot to test it out.

Move Until Object Detected

To a Lego EV3 robot move until an object is detected we are going to use the infrared sensor (if you have the retail kit) or the ultrasonic sensor (if you have the education kit). They will both achieve the same thing, they just do it using different technology.

As with move until touch and color we will need to use the Loop programming block to keep the robot moving forward until a conidtion is met. In this case the condition is that the robot detects an object is close.

  1. Open a new project.
  2. Drag the Loop block from the orange palette up to the right of the Start block on the programming canvas:
EV3-Loop-programming-block-on-canvas
  1. Drag a Move Steering block from the green palette into the middle Loop and set the mode to On, direction to 0 and power 75:
EV3-move-until-color-step-1

The next step is to set the loop condition to check the infrared or ultrasonic sensor to see if an object is close:

  1. Click the infinity symbol on the loop condition and select Infrared | Proximity Or Ultrasonic Sensor | Distance in Centimeters:
EV3-move-until-object-step-3
  1. On the Loop Block’s conditions set the Compare type to Less Than (4) and the Threshold Value to 30.
EV3-move-until-object-step-4
  1. I’m going to stop the robot (not let it coast) so I’m adding another Move Steering block to the end of the program outside the loop, and I’ll set it’s mode to Off and Brake at End to True:
EV3-move-until-object-step-5
  1. Now it’s done! Place your robot some distance away from a wall / object and Click Download and Run

Have fun and experiment with the distances.

EV3-move-until-object-detected-robot-test

Move Tank Vs Move Steering

Even though the Move Tank and Move Steering blocks achieve similar actions, the difference between Move Tank and the Move Steering programming blocks is that the Move Tank block allows greater control of the direction and turn angle as it has inputs to control each motor individually. As per the image below the Move Tank block allows the left and right motor direction and power to be independently set:

LEGO-MINDSTORMS-EV3-Move-Tank
Move Tank – Left and Right Motor Inputs

Where the Move Steering block is an estimate of the power and direction of each motor based on the single steering input outlined in the previous section:

LEGO-MINDSTORMS-EV3-Move-Steering
Move Steering – Steering Input

As you can see, the Move Steering block is definitely easier to use as it takes the complication out of steering. If it’s more control you’re after then the Move Tank block will do the job for you.

If you have any questions feel free to leave a comment below.

Leave a Comment