Python language bindings for ev3dev Documentation Status

A Python library implementing unified interface for ev3dev devices.

Example Code

To run these minimal examples, run the Python interpreter from the terminal like this:

robot@ev3dev:~$ python
Python 2.7.9 (default, Mar  1 2015, 13:52:09)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

The >>> characters are the default prompt for Python. In the examples below, we have removed these characters so it’s easier to cut and paste the code into your session.

Load the ev3dev-lang bindings:

import ev3dev.ev3 as ev3

Now let’s try our first program. This code will turn the left LED red whenever the touch sensor is pressed, and back to green when it’s released. Plug a touch sensor into any sensor port and then paste in this code - you’ll need to hit Enter after pasting to complete the loop and start the program. Hit Ctrl-C to exit the loop.

ts = ev3.TouchSensor()
while True:
    ev3.Leds.set_color(ev3.Leds.LEFT, (ev3.Leds.GREEN, ev3.Leds.RED)[ts.value()])

Now plug a motor into the A port and paste this code into the terminal. This little program will run the motor at 75% power for 3 seconds.

m = ev3.LargeMotor('outA')
m.run_timed(time_sp=3000, duty_cycle_sp=75)

If you want to make your robot speak, then paste this code into the terminal:

ev3.Sound.speak('Welcome to the EV3DEV project!').wait()

To quit Python, just type exit() or Ctrl-D.

User Resources

Getting Started with ev3dev
If you got here as the result of looking for “how to program LEGO MINDSTORMS EV3 using Python” then you might not be aware that this is part of a much larger project called ev3dev. Make sure you read the Getting Started page to become familiar with ev3dev first!
Connecting the EV3 to the Internet
You can connect to an EV3 running ev3dev using USB, Wifi or Bluetooth. The USB connection is a good starting point, and the ev3dev site has detailed instructions for USB connections for Linux, Windows, and Mac computers.
Demo Robot
Laurens Valk of robot-square has been kind enough to allow us to reference his excellent EXPLOR3R robot. Consider building the EXPLOR3R and running the demo programs referenced below to get familiar with what Python programs using this binding look like.
Demo Code
There are demo programs that you can run to get acquainted with this language binding. The programs are designed to work with the EXPLOR3R robot.

Developer Resources

Python Package Index
The Python language has a package repository where you can find libraries that others have written, including the latest version of this package.
The ev3dev Binding Specification
Like all of the language bindings for ev3dev supported hardware, the Python binding follows the minimal API that must be provided per this document.
The ev3dev-lang Project on GitHub
The source repository for the generic API and the scripts to automatically generate the binding. Only developers of the ev3dev-lang-python binding would normally need to access this information.

Python2.x and Python3.x Compatibility

The ev3dev distribution comes with both python2 and python3 installed and this library is compatible with both versions.

Note that currently, the source is only installed in the default Python 2.x location - this will be addressed in the next package we release.

For Python 2.x programs, you import the binding like this:

from import *

For Python 3.x the easiest way to work around the problem is to get your EV3 connected to the Internet and then:

  1. Update the package lists
  2. Install the python3-pil package
  3. Use easy-install install python-ev3dev
sudo apt-get update
sudo apt-get install python3-pil
sudo python3 -m easy_install python-ev3dev

You will be asked for the robot user’s password to get sudo access to the system - the default password is maker.

Please be patient - a typical apt-get update will take about 10 minutes - there’s a LOT going on under the hood to sort out package dependencies.

And now you can use ev3dev-lang-python under Python 3.x.

from import *


Indices and tables