Python language bindings for ev3dev
A Python3 library implementing an interface for ev3dev devices, letting you control motors, sensors, hardware buttons, LCD displays and more from Python code.
If you haven’t written code in Python before, you’ll need to learn the language before you can use this library.
This library runs on ev3dev. Before continuing, make sure that you have set up your EV3 or other ev3dev device as explained in the ev3dev Getting Started guide. Make sure that you have a kernel version that includes -10-ev3dev or higher (a larger number). You can check the kernel version by selecting “About” in Brickman and scrolling down to the “kernel version”. If you don’t have a compatible version, upgrade the kernel before continuing. Also note that if the ev3dev image you downloaded was created before September 2016, you probably don’t have the most recent version of this library installed: see Upgrading this Library to upgrade it.
Once you have booted ev3dev and connected to your EV3 (or Raspberry Pi / BeagleBone) via SSH, you should be ready to start using ev3dev with Python: this library is included out-of-the-box. If you want to go through some basic usage examples, check out the Usage Examples section to try out motors, sensors and LEDs. Then look at Writing Python Programs for Ev3dev to see how you can save your Python code to a file.
Make sure that you look at the User Resources section as well for links to documentation and larger examples.
To run these minimal examples, run the Python3 interpreter from the terminal using the python3 command:
$ python3 Python 3.4.2 (default, Oct 8 2014, 14:47:30) [GCC 4.9.1] on linux 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.
import ev3dev.ev3 as ev3
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 Python prompt. This little program will run the motor at 500 ticks per second, which on the EV3 “large” motors equates to around 1.4 rotations per second, for three seconds (3000 milliseconds).
m = ev3.LargeMotor('outA') m.run_timed(time_sp=3000, speed_sp=500)
The units for speed_sp that you see above are in “tacho ticks” per second. On the large EV3 motor, these equate to one tick per degree, so this is 500 degress per second.
If you want to make your robot speak, you can use the Sound.speak method:
ev3.Sound.speak('Welcome to the E V 3 dev project!').wait()
To quit the Python REPL, just type exit() or press Ctrl-D .
Make sure to check out the User Resources section for more detailed information on these features and many others.
Every Python program should have a few basic parts. Use this template to get started:
#!/usr/bin/env python3 from ev3dev.ev3 import * # TODO: Add code here
The first two lines should be included in every Python program you write for ev3dev. The first allows you to run this program from Brickman, while the second imports this library.
When saving Python files, it is best to use the .py extension, e.g. my-file.py. To be able to run your Python code, your program must be executable. To mark a program as executable run chmod +x my-file.py. You can then run my-file.py via the Brickman File Browser or you can run it from the command line via $ ./my-file.py
You can upgrade this library from the command line as follows. Make sure to type the password (the default is maker) when prompted.
sudo apt-get update sudo apt-get install --only-upgrade python3-ev3dev
As of the 2016-10-17 ev3dev image, the version of this library which is included runs on Python 3 and this is the only version that will be supported from here forward.