Introduction

    Version 2 is out!    

This site, which can be reached at www.ev3python.com, is intended to help users of the Lego EV3 robot who are already familiar with the basics of programming in Python to begin using the EV3dev Python programming language (called EV3 Python on this site) to control their robot. There are various ways of programming the EV3 with EV3dev Python – this site focuses on what is arguably the best way, especially for beginners:
  • use the free code editor Microsoft Visual Studio Code with the EV3 extension. Note that the extension is only compatible with 'Stretch' versions of EV3 Python, and not with 'Jessie' versions
  • use the new version (v2) of EV3 Python. That's right: version 2 of EV3 Python is out! It's included on the latest 'Stretch' image, so may already have it! To be sure, download the latest Stretch (not Jessie) image from HERE.
The new, much improved version of EV3 Python includes features like MoveSteering and MoveTank that mirror the corresponding blocks in Lego's icon-based EV3 programming system (sometimes called EV3-G). Version 2 also includes... version 1, so rest assured: all the scripts that were written for library 1 should continue to work with the latest Stretch image. But you are strongly advised to modify your old version 1 scripts to take advantage of the new features of library 2. If you want to continue working with version 1 functions then know that the old version 1 site is still available HERE

If you a new user of EV3 Python or if you are an older user ready to made the switch to the new library then you should use this site. To learn more about the differences between the old Python library and the new one and how to modify scripts that were written for the old Python library to take advantage of the great new features of the library then click HERE.

Both this site and the site for the older Python library are focused on using Microsoft Visual Studio Code (VS Code) with the EV3 extension since this is by far the easiest way to write and run EV3 Python scripts. VS Code is a free code editor that runs on Windows, macOS and Linux. It should not be confused with Microsoft Visual Studio which is not free and which runs only on Windows. To learn more about working with VS Code click HERE. Note that the EV3 extension for VS Code is compatible only with 'Stretch' versions of ev3dev and not 'Jessie' versions so when you download ev3dev be sure to download the latest Stretch version and not the Jessie version. The Jessie versions do not contain the new EV3dev Python library (v2) to which this site is devoted. Note that when you download the latest Stretch image it contains EV3dev Python library v2 and that the v2 library includes the  v1 library so both the old functions and the new functions are available to you. Whether you use old v1 functions or new v2 functions is determined only by the code that you write. In particular, the import statements indicate whether the old library should be used (from ev3dev ....) or the new library should be used (from ev3dev2 ...).

The Lego Mindstorms EV3 robot is a very popular educational / entertainment robot that is normally programmed using an icon-based programming system that resembles Scratch. Read more about icon-based Lego EV3 programming on my site mind-storms.com. But the EV3 has a Linux computer at its heart and can therefore be programmed using a number of other programming systems such as Python, C++, Java or EV3 Basic (see my site ev3basic.com). Running these more advanced programming languages allows you to get your EV3 robot to do things that would not be possible with the standard EV3 programming system, such as making the EV3 speak whatever words you want or making this drawing robot which uses EV3 Python:


This site is not intended to show you how to make programs as complex as the one above, only to help you get started with EV3 Python programming by working with very short, simple, easy-to-understand programs, or 'scripts'. But maybe one day you will be able to make your own programs as impressive as the one above.

In addition to empowering you to write programs that can do things that cannot be achieved with the standard icon-based EV3 programmings system, another great reason to learn EV3 Python is that EV3 Python is a textual programming language, the kind professional programmers use. Learning a textual programming language (especially Python) can boost your career prospects enormously, whereas learning to program the EV3 with the standard icon-based system will have much less value professionally.

If you are not sure which programming system you should be using to program the EV3 then follow this link. If you are sure that you want to use Python to program the EV3 then read on...

You're probably curious about what an EV3 Python program looks like. Here is an example which uses a color sensor to help a rover-type vehicle to follow a black line. This is just to give you a glimpse of an EV3 Python script - the full explanation is on THIS PAGE. This program does however include comments (preceded each time by an '#' sign) which are there solely to help you understand the code. The very first line is a special line called a  'shebang' which tells the EV3 to use the Python 3 interpreter to run the program and where to find Python 3 in the EV3's file hierarchy.

#!/usr/bin/env python3
# An EV3 Python (library v2) solution to Exercise 3
# of the official Lego Robot Educator lessons that
# are part of the EV3 education software

from ev3dev2.motor import MoveTank, OUTPUT_B, OUTPUT_C
from ev3dev2.sensor.lego import ColorSensor
from ev3dev2.button import Button
from time import sleep

btn = Button() # we will use any button to stop script
tank_pair = MoveTank(OUTPUT_B, OUTPUT_C)

# Connect an EV3 color sensor to any sensor port.
cl = ColorSensor()

while not btn.any():    # exit loop when any button pressed
    # if we are over the black line (weak reflection)
    if cl.reflected_light_intensity<30:   
        # medium turn right
        tank_pair.on(left_speed=50, right_speed=0)
    
    else:   # strong reflection (>=30) so over white surface
        # medium turn left
        tank_pair.on(left_speed=0, right_speed=50)
    
    sleep(0.1) # wait for 0.1 seconds

EV3 Python needs to run with the support of the EV3dev operating system. The official site of EV3dev and EV3dev Python is www.ev3dev.org and I will refer you to that site for the details of the installation and the documentation of the various EV3 Python functions. The developers of EV3dev and EV3dev Python have done a fantastic job making available a stable operating system and robot-related Python commands with the simplest possible syntax.

Unlike some of the developers of EV3dev, I'm not a professional coder, but I do have decades of experience teaching computer science to teens, including Python and EV3. I therefore feel well qualified to help people who already know the basics of Python and who want to start using EV3 Python to control their Lego EV3 robot. This site is in no way endorsed by Lego Corporation.

Since I'm a professional teacher, I'm especially keen to help other teachers begin using EV3 Python in the classroom. That's why there's a strong emphasis on coding on this site as opposed to building different models, fun though that may be. This site adopts the official 'Educator Vehicle' (also called the 'Driving Base') so nearly all the scripts on this site are compatible with that model. Note that although the Educator Vehicle was originally designed to be assembled from the education version of the EV3 kit, I explain on this site how a very similar vehicle can be built with the home version of the EV3 kit.

I am a Windows man, so this site will be oriented towards users of Windows, but users of other platforms should also find the site useful. This site is designed to be compatible with both types of EV3 robot kit: the home edition and the education edition. This site will only discuss what can be achieved with the standard Lego hardware - there will be no discussion of third party hardware. Having said that, it's good to know that it should be possible to use EV3dev and EV3 Python with third-party sensors and motors and even run EV3 Python on other platforms in addition to the EV3, such as Raspberry Pi with either the BrickPi shield or the PiStorms shield. These 'shields' plug into the Raspberry Pi and act as an interface between the Raspberry Pi and Lego EV3 motors and sensors. This means you can use the extra power of the Pi compared to the EV3 and in the case of PiStorms you also get a nice color screen. See HERE.

Now that you understand what this site is about, the next step is for you to install EV3Dev and EV3 Python.
Subpages (2): News Why EV3 Python?
Comments