EV3 Basic programming

EV3 Basic is Microsoft Small Basic with the EV3 extension. Before you learn how to work with the robotic functions that the extension adds to Small Basic, you should spend some time familiarising yourself with Small Basic programming. Small Basic is a Microsoft product that has been around since 2008 so it's well supported - it has its own website (smallbasic.com) and there are abundant learning materials out there to help you get familiar with the language. Because this is the case, this site does not teach you the basics of textual programming - it is assumed that you will learn the basics elsewhere, such as by working through this PDF guide, before you continue working through this site.

For all the sample programs I propose, I will assume that you are working with a robot similar to the one below. Notice that the wheel motors are attached to motor ports B and C




The above model is the official Lego 'driving base' or 'educator vehicle' model that is used in the lessons that accompany the education version of the EV3. Building instructions are available HERE. The above photos include a sound sensor that is not part of the standard driving base, and the gyro is placed in an unconventional (better!) location.

Don't worry if you have the 'home' version of the EV3 set because the education version and the home version of the EV3 set are very similar. The lessons on this site can nearly all be completed with either set, though you may need to make occasional modifications if you have the 'home' version of the EV3 set. For example, if you have the home version, you will need to substitute a different castor wheel at the back of the model and you will need to substitute the infrared sensor for the ultrasonic sensor. You will not be able to include the gyro sensor.

Here are some general tips about writing programs in EV3 Basic:

Important note: all settings dialogs and applications such as 'port view' must be closed on the brick in order to be able to run programs from Small Basic or EV3 Explorer. If a dialog box is open in the brick it will still be possible, however, to use other functions of EV3 Explorer such as compiling programs to the brick.

Recall that EV3 Basic programs can be run in two different modes: PC mode and brick mode.
  • In PC mode you run the programs in Small Basic by clicking the Run button or pressing F5. The program then runs on the PC, exchanging some data with the brick as necessary in order to control the motors and obtain sensor readings. In PC mode you can make use of Small Basic features such as the graphics window or the possibility of playing sounds on the PC speaker.

  • In brick mode the program runs on the brick. To get the Small Basic program onto the brick you must use the companion program EV3 Explorer which will 'compile' your SB program into the RBF format the brick understands, and will store the RBF program on the brick, in the folder of your choice. Programs on the brick can be run either at the moment they were compiled, by clicking the 'Compile and Run' button in EV3 Explorer, or later, from the brick's menus or using Explorer. In brick mode you cannot make use of Small Basic features such as the Small Basic graphics window or the possibility of playing sounds on the PC speaker.
Compared to PC mode, running programs in brick mode imposes additional rules about writing and saving your programs. Unless you are using Small Basic features such as the graphics window, you should aim to make your programs compatible with both PC mode and brick mode by observing these additional rules, which include:
  • Save program files with a maximum length of 25 characters and use only these characters in the program names: ABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdefghijklmnopqrstuvwxyz0123456789 -_    (includes space).
  • Don't use this method of making arrays: Colors="0=UNKNOWN;1=BLACK;2=BLUE;3=GREEN"
  • Put subprogram definitions BEFORE the main program.
  • Assign values to variables before you use them.
  • For additional rules, see the Compiler Hints page.
Here are the lessons!