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.

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.

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

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.

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

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.

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.

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.

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.

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.

A second message appears

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

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:
- chrome://flags/#enable-experimental-web-platform-features
- edge://flags/#enable-experimental-web-platform-features
Then you have to link the EV3 with your computer via Bluetooth.
Once everything installed, click on the Download button. This window will open.

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.

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

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.

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.

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

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.

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.

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

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

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.

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

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

Music¶
The EV3 brick can produce sounds.
Play a melody¶
Let’s program the musical score of Frère Jacques, a popular French song.

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

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

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.

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.

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

The last measure has long note again.

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.

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

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.

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

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.

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

You can download this file.
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.

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

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.

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%)

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

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.

The result on the screen will be this

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.

Stop at a line¶
We program the light sensor to make the robot stop when the light intensity goes down.

Stop at an obstacle¶
We program the ultrasonic distance sensor to stop the robot when an object is near.

You can download this file and import it into MakeCode.