Getting started with Python on EV3

So I’ve been diving head first into LEGO’s newest Mindstorms generation, called EV3. But seeing as I’m a Linux geek and a programmer, using LEGO’s own graphical drag-and-drop programming language from their windows/mac software was completely out of the question! Now, the EV3 runs Linux out of the box, which is very cool, but it comes with some limitations, so to get going with a proper programming language, installing another Linux version was necessary. Luckily, the EV3 makes that so much simpler than the NXT did. Just pop a bootable OS on a micro SD card, stuff in the EV3, and voila, it will boot from the SD card! Pop the card back out, and the EV3 still has it’s factory firmware. No flashing needed! Neat!

So what programming language to use? Well, turns out some guy who calls himself Topikachu has already made a ready Linux image with Python installed, and even made Python libraries to interface with the EV3 sensors and motors. Does it get any better than that? He’s made a quick howto on how to install it, which you can read on his github. If you have a USB Wifi dongle too (currently only one is supported, but I’m sure more will be added in time), it was very easy to get it hooked up on wifi too. I just had to log in over usb as described, update the wpa_supplicant.conf and I was good to go without a cable. You may want to check out what IP your brick received before unplugging the usb cable though ;)

The Python API is really great, but I couldn’t find much documentation of it, so I had to read his code and look at his tests to figure out how to use it. Wasn’t too hard though, and here’s a little program I wrote just to test out that things were working (it doesn’t do much, basically just tries to crash :P)


import time

from ev3.rawdevice import motordevice
from ev3.rawdevice import analogdevice
from ev3.rawdevice import uartdevice

from ev3 import lego


A = 0x01
B = 0x02
C = 0x04
D = 0x08

right = A
left = D
both = A+D

touch = lego.EV3TouchSensor(0)
ir = lego.EV3IRSensor(3)


distance = 101

while True:
  if touch.is_pressed() == 1:
    motordevice.stop(both, brake=1)
    print "stopping\n"

  cur_distance = ir.get_distance()
  if cur_distance > distance:
    print "searching\n"
    motordevice.stop(both, brake=1)
    motordevice.speed(A, 20)
    motordevice.stop(A, brake=1)

  distance = cur_distance

motordevice.stop(both, brake=1)

That doesn’t look so hard, does it? Here’s some additional notes I made for myself to remember what the different functions do and how to use them.

A = 0x01
B = 0x02
C = 0x04
D = 0x08
PORTS = A+B # Access both A and B at the same time
from ev3.rawdevice import motordevice

motordevice.stop(B,brake=1) # brake makes it actually brake the motor, not just stop turning it

from ev3.rawdevice import analogdevice
touch = lego.EV3TouchSensor(0) # Note, the port numbering is 0-3, so the port marked 1 is 0, etc.

from ev3.rawdevice import uartdevice
color = lego.EV3ColorSensor(3)
color.color_to_string() # This only works for color_mode
color.set_ref_raw_mode() # raw values, -127 -> 127
color.get_value() # raw value from sensor sensor

ir = lego.EV3IRSensor(2)
ir.set_prox_mode() # proximity mode
ir.get_distance() # for prox mode
ir.set_remote_mode() # for using the remote control
ir.get_remote_command() # get remote control command
ir.set_seek_mode() # follow remote control when button pressed
ir.get_all_direction_and_distance #  for seek mode
ir.get_direction_and_distance(chan) #  for seek mode

# This is how I managed to read info from a Hitechnic accelerometer sensor (originally made for the NXT)
from ev3 import robot
from ev3 import sensor
iicsensor = IICSensor()
iicsensor = sensor.IICSensor(1, 0x02)
This entry was posted in LEGO, Linux, Scripts and tagged , , , . Bookmark the permalink.

4 Responses to Getting started with Python on EV3

  1. Pingback: Lego Mindstorms EV3 mit python | Bastian Kuhn

  2. thomas says:

    Thanks for sharing this interesting topic. You should consider contribute up to the python-ev3 project this few lines of code, after a small refactoring. Actually the python-ev3 supplies only one (buggy) example, which is not enough to get familiar with the API. BTW, you write “only one Wifi dongle is currently supported”. Which one is it ?

    • Cecilie says:

      Yes, I struggled a bit with it at first, I had to read through the code and the unit tests to figure out the basics on how to use the python libs. I’ll consider cleaning up my code and making a pull request.
      The supported wifi dongle is explained here:
      Basically it’s a Netgear WNA1100. That’s what the stock firmware supports anyway. I’m not sure about Lejos, or whatever modifications Topikachu has done to the kernel he’s using (as he doesn’t write anything about it). I only tried the Netgear as that’s what I had bought along with my EV3, and it worked out of the box with the python-ev3 kernel.

  3. Pingback: Lego Mindstorms EV3 mit python | IT Blog

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>