Robotics: Programming Ev3 Robots with Python Using ev3dev


· ·

Robotics are increasingly becoming a common part of our daily lives. This can be observed in the popularity of things like drones and IoT devices that perform some form of automation. The robots of today are capable of performing very complex tasks, and programming these tasks is made easier by using a simple and powerful programming language like Python. This post will show you how to write code for the LEGO Ev3 robot using a special Linux-based operating system known as ev3dev. This operating system comes with a Python package that allows you to program the robot with native Python.

Installing ev3dev

Installing the ev3dev operating system on the LEGO Ev3 requires the use of a memory card. About two gigabytes of space should be enough, given that the image is about 200 megabytes. Once you have access to a memory card with enough space, head over to the official ev3dev website and download the operating system. (At this point you should note that the memory card will be formatted in the next step, meaning you will lose any data you currently have on it.)

The next thing to do is to download Etcher on your computer. This will allow you to flash the ev3dev operating system onto your memory card, making it possible to boot it on your robot. After installing and opening Etcher, click on Select Image and navigate to the ev3dev image you downloaded previously. Next, click on Select Drive and select your memory card, click on Flash, and wait for the process to complete. Once complete, insert the memory card into the robot and turn it on. If everything went well, you will notice the light on the robot will begin to flash orange with a lot of text running on the screen.

Connecting to the Robot

Once the system is done booting, you will need to connect to it before you can start programming. To do this, connect the robot to your computer via USB. Then, navigate to Wireless and Networks on your robot’s interface, and select it. Now, select All Networks and select Wired. Finally, select the Connect option and check the connect automatically option. (You will not have to go through these steps again once complete.) An IP address should show at the top of your robot’s interface once the connection has been made.

Next, download MobaXterm and install it on your PC. This will allow you to establish an SSH connection with the robot, making it possible to program it. After installing MobaXterm, run it, click on Session at the top left and click on SSH. Next, enter the IP address shown on your robot’s interface in the Remote host text box. Check the specify username box, then type “robot” into the text box next to it and click OK.

If this does not work, type “ev3dev” instead of the IP address in the Remote host text box. (Note that the username is predefined and will remain the same until you change it in the robot’s terminal.)

After clicking OK, a terminal will show up after a few moments requesting a password. Type “maker” as the password (note that no characters will show while typing the password) and hit Enter. If the connection is successful, you’ll see the ev3dev terminal interface as shown in the image below.

Programming the Robot

You’re finally ready to start writing code to control your robot with Python. If you are familiar with the Linux terminal and terminal text editors, you can go ahead and create your Python files. Otherwise, click on the SFTP tab on the left of the screen, right-click in the area with the files, and then click on New empty file and give it a name. I named mine (Note that your file must have a .py extension because it will be run with Python.)

Right-click on the file and click on Edit with default editor to open the file with the default editor for MobaXterm. You can also click on open with after right-clicking the file, select your favorite text editor, and paste the following code into the editor and save it. Confirm that you want to save when the MobaXterm dialog box shows up.

#! /usr/bin/python3

import ev3dev.ev3 as ev3

leftMotor = ev3.LargeMotor('outA')   #Connects ev3 to robot A
rightMotor = ev3.LargeMotor('outB')  #Connects ev3 to robot B

leftMotor.run_to_rel_pos(position_sp = 360, speed_sp = 500)
rightMotor.run_to_rel_pos(position_sp = 360, speed_sp = 500)


The code connects to two large Ev3 motors on ports A and B and makes them move. The last two lines block the progress of the code until the motors have stopped running. You should have two motors connected to ports A and B to use this program—otherwise it will throw an error.

To run the file, you must make it executable by typing the following command in the terminal:

$ chmod +x

You can also right-click the file, click on Permissions, and check the executable boxes. Finally, navigate to your file browser and select your Python file to run it. You should see the motors move after a short while.

Stephan is a third-year student at Ashesi pursuing an undergraduate degree in computer engineering. Stephan is passionate about technology and is focused on continuous learning to gain new skills. He has worked in software development, and has worked on IoT projects and designing products for the health sector.


Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Skip to toolbar