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.

18 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

  4. Stas says:

    Any tips regarding the wifi dongle ?
    I have the 8192cu and it is working in Lejos (java) but when I do ifconfig on the Python-ev3 there’s no interface found.

    • Cecilie says:

      Sorry, I don’t know. I’ve only tried with the one LEGO says works with the EV3, and that worked out of the box for me. You’re probably better off asking the guy who made the Python-ev3.

    • Eric says:

      Stas, have you got 8192cu working in your side? I am going to give a try for this small wifi dongle. Would be good to know your progress if any.

    • Alan says:

      I figured out how to add 8192cu support on this image:

      You can get the driver from here:

      install the driver:
      cp 8192cu.ko /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/

      load driver on system startup:
      echo 8192cu >> /etc/modules
      depmod -a

      load driver:
      insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/cp 8192cu.ko


      auto lo
      iface lo inet loopback

      allow-hotplug eth0
      iface eth0 inet dhcp
      hwaddress ether xx:xx:xx:xx:xx:xx #hardware address ev3

      auto wlan0
      iface wlan0 inet dhcp
      wpa-conf /etc/wpa_supplicant.conf

      reboot or /etc/init.d/networking restart

      it should get an ip address now, if not check your settings and the /var/log/syslog

      good luck!

      • Alan says:

        sorry for the typo 😉

        load driver:
        insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/8192cu.ko

      • Alan says:

        hm the html layout messed up my code :)
        vi /etc/wpa_supplicant.conf

        vi /etc/network/interfaces
        auto lo…
        …wpa-conf /etc/wpa_supplicant.conf

  5. Great post, it’ll help me a lot – many thanks!

    Quick question: how long did your first boot take? (On the GitHub page it says “It takes a long time on first boot”, but it’s been hours now, so I worry it’s just stuck.)

    • Cecilie says:

      It does not take hours. It may take like a minute or two. But you’ll never get anything in the display, so it might have booted fine but you just don’t know it. Did you try to connect to it? I think the only thing that happened was a light came on or off or changed color or something, I don’t remember, it’s been some time since I played with it now. If you can’t get it to work, try to reflash the memory card, in case it got a corruption the first time.

      • The display kept showing that initial “mindstorms – starting…” message. It got stuck like that for over 10 hours. I was about to give up (I just cloned the ev3sources source code and was experimenting with sending C++ code via Bluetooth), but I’ll try reflashing the card, as you suggested.

        Thanks for the help, Cecilie!

  6. We merged Python 2.7 into the leJOS sd card image and use a TCP/IP gateway between Python and leJOS. For more information consult

  7. alex says:

    can we use it for mac

  8. Rick Ferrante says:

    So you said you “read his code” because there was no documentation. Where are you finding all the code? Are you referring to the code in topikachu/python-ev3/ev3 directory? where do you find all the code on the EV3?


Leave a Reply to Cecilie Cancel 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=""> <s> <strike> <strong>