Welcome to EV3 MakeCode

Learn to program the LEGO EV3 MINDSTORMS robot by assembling graphical control blocks.

Introduction

This tutorial is an introduction on how to program the LEGO MINDSTORMS EV3 robot with Microsoft MakeCode.

The language MakeCode is a graphical language where you assemble blocks to tell the robot what to do. All the programming is done online inside a navigator window (Chrome, Safari, Edge, Firefox) and does not need any installation of software.

To program the EV3 go to the following site: https://makecode.mindstorms.com/#editor

MakeCode also has a simulator which allows you to see the result of your program immeditely, even without downloading it to the EV3 brick.

Hello world

It is a tradition for most tutorials to start with a program which displays Hello world on the screen. Here it is.

_images/hello1.png

The program displays a line of text on the line you specifiy. Here we display it on line nummer 1, but you have up to 12 lines. When you run it, the simulator dispays this.

_images/hello1s.png

Show the mood

Now let’s use the center button to display the mood of the robot.

_images/hello2.png

To express a mood you choose from among 12 images showing different types of eyes. Each image has a different sound associated with it. Choose a mood and test it.

_images/hello2b.png

When you press the center button, the robot will display this image.

_images/hello2s.png

On start and forever

When you create a new project, you have already two blocks on the canvas:

  • the on start block,
  • the forever block.
_images/intro1.png

The on start block executes all blocks inside exactly once, when the program starts.

The forever block is an infinite loop and repeats all the blocks inside until you stop the program.

The following program plays a deep tone (Middle C) at the start, and then a higher toone (Tenor C) repeatedly every 1 second.

_images/intro1b.png

Download a program

So far, you could observe the result of your programs in the simulator. But it’s more fun to download them to your robot. Once you connect the EV3 via a USB cable to your computer, it appears as a hard-disk.

_images/ev3_hd.png

In order to download a program to the brick, click on the Download button. If you have a project called intro this will download a file called intro.uf2 to your download folder. To run the file on the EV3 you must transfer the file inside the EV3 disk icon.

_images/download.png

Inside the preferences of your navigater you can specify to which folder the files are downloaded. You can either

  • automatically save to the EV3 drive,
  • ask each time for the destination.

Upgrade the firmware

If the EV3 icon does not appear on your desktop, you must probably upgrade the firmware (FW) of your brick. You can check the firmware on the brick under

  • Settings top menu (all to the right)
  • Brick info sub-menu
  • Brick FW: V1.09H (H=Home, E=Education)

You need to have V1.10E or more.

Go to the online EV3 Device manager at the following adresse: https://ev3manager.education.lego.com

The message shows the outdated firmware and allows you to update.

_images/ev3_update.png

A second message appears

_images/ev3_update2.png

Go ahead with the update and follow the instructions. Once the upgrade is finished, you will get this screen.

_images/ev3_manager.png

Bluetooth

It is possible to download programs to your EV3 via Bluetooth. This is an experimental feature and works on the Google Chrome and the Microsoft Edge navigator, version 77 or higher.

You need to enable this feature by going to this address:

Then you have to link the EV3 with your computer via Bluetooth.

Once everything installed, click on the Download button. This window will open.

_images/bt1.png

Then click on the blue Bluetooth button then select the select indicated serial port. You must stop any running program on the EV3 before you can download via Bluetooth.

Sensors

The EV3 has multiple sensors such as:

  • touch sensor
  • color sensor
  • light sensor
  • gyro sensor
  • ultrasonic sensor

Touch sensor

Each time you press the touch sensor, the eyes with the hearts appear and a sound is played.

_images/touch1.png

You can test his inside the simulator. When you click with the mouse on the touch sensor icon, the image and the sound appear.

_images/touch1s.png

Connect the touch sensor to port 1. When you press the touch sensor, a heart is displayed. When you release the touch sensor, the screen is cleared.

_images/sensor1.png

Colour sensor

Connect the color sensor to port 3. When you place a blue LEGO brick in front of the sensor, it says blue. When you place a red LEGO brick in front of the sensor, it says red.

_images/sensor2.png

Show values on the screen

You can use a forever loop to write the sensor values to the screen.

_images/sensor3.png

In the simulation in the navigator window you can set different values for the sensor and display the values on the first three lines of the screen.

_images/sensor3s.png

Motors

Motors let you move your robot.

Display a welcome screen

Let’s write some explanations to the display. It is always good to give the user some hints about what the program is doing. Let’s explain how to use the 3 buttons to control the large motor on port A.

_images/motor1.png

Move a motor

Now let’s use the buttons to make the motor move.

_images/motor1b.png

Before downloading the program to the brick, you can test it in the simulation.

_images/motor1s.png

Change the motor speed

Make a new program called motor2. This time we will include that name in the welcome screen.

We are going to use the up/down button to increase or decrease the speed. For this we will need a variable speed.

_images/motor2.png

When pressing the up button we increase the speed variable by 10. When pressing the down button we decrease the speed variable by 10.

_images/motor2b.png

You can try this program in the simulator before you download it to the brick.

_images/motor2s.png

motor2.uf2

Music

The EV3 brick can produce sounds.

Play a melody

Let’s program the musical score of Frère Jacques, a popular French song.

_images/jacques.gif

We start playing the first measure when pressing the center button.

_images/music1.png

To select the note to play, just click on the name, for example Middle C, and select another note on the piano scale keyboard.

_images/music1b.png

Repeat a melody

To repeat a part of the melody, place it inside a loop. For exemple the first measure needs to be repeated twice.

_images/music1c.png

Longer and shorter notes

So far our notes had a duration of one beat. We can increase to the duration to two beats, which is required in the second mesure.

_images/music1d.png

The next measure has 4 notes with only half a beat.

_images/music1e.png

The last measure has long note again.

_images/music1f.png

Change the tempo

The next part allows you to change the tempo. The tempo of the music is measured in beats per minute (bpm). Let’s make a program to display the inital tempo.

_images/music2a.png

When you run the simulation you see that the temp is 120 bpm.

_images/music2b.png

Now let’s program two buttons to increase and decrease the tempo.

  • The up button makes the tempo faster by 20 bpm (beats per minute).
  • The down button decreases it by 20 bpm.
_images/music2c.png

Change the volume

To keep track of the volue we need to create a variable.

_images/music3.png

We can set this variable to 5 in the start block. The we set the volume using this variable. And finally we display its value on line number 4.

_images/music3b.png

Finally we program the left/right buttons to increase or decrease the volume.

_images/music3c.png

You can download this file.

lego-music.uf2

Driving

The EV3 has special commands for controlling the two large motors for moving the robot around.

Use the buttons to drive

First we display a small user guide on the EV3 display.

_images/drive1.png

At this point we can test the display and the presence of the two large motors.

_images/drive1s.png

Move forward and backward

The robot moves forward if both motors turn forward (50%) at the same speed. The robot moves backward if both motors turn backward (-50%) together.

_images/drive2.png

Turn left and right

The robot turns left if

  • motor B goes backwards (-50%)
  • motor C goes forward (50%)

The robot turns right if

  • motor B goes forward (50%)
  • motor C goes backward (-50%)
_images/drive3.png

Display a start screen

It’s always good practice to display on the screen what the robot is doing. We place that code inside the on start block.

This robot can execute 3 behaviors:

  • drive a square
  • advance till it reaches a dark line or the border of the table
  • advance till the distance sensor detects a nearby object
_images/drive_start.png

Display the sensor values

It is useful to display the measured sensor values on the screen. We place this code into the forever block, as we want to do these measurements continously.

_images/drive_forever.png

The result on the screen will be this

_images/drive_screen.png

Drive along a square

We repeat 4 times inside a loop a straight segment and a 90-degrees turning segment. The robot will execute a square trajectory.

_images/drive5.png

Stop at a line

We program the light sensor to make the robot stop when the light intensity goes down.

_images/drive_light.png

Stop at an obstacle

We program the ultrasonic distance sensor to stop the robot when an object is near.

_images/drive_light.png

You can download this file and import it into MakeCode.

lego-drive.uf2

Indices and tables