Installation guide: EV3 brick with Python and MicroPython

| |

Step by step guide for LEGO MINDSTORMS EV3 to install and run both Python and MicroPython. It’s really easy and requires no hacking or weird codes.

Setting up the EV3 for Python and MicroPython

The easiest way to get started is to head over to LEGO Education and download their new python software and image. They packaged the community built firmware with some extra tools to get started easily. Here are the steps I used to run it.

Time needed: 10 minutes.

How to run MicroPython and Python on LEGO MINDSTORMS EV3

  1. Get a 16Gb Class 10 Micro SD card

    I like Sandisk Extreme. I believe a Class 4 8GB would work too, but it’s slower.

  2. Download the EV3 MicroPython image from LEGO Education

    Do NOT unzip the downloaded file.

  3. Burn the image to your Micro SD Card

    I use Balena Etcher to write the image. It will ask for an admin password because it needs low level access to your SD card.

  4. Insert your micro SD card into the SD card slot on the EV3 Brick and turn it on

    It will show a lot of scrolling text and take a while. Go on to the next step while it’s starting.

  5. Download and install the free Visual Studio Code editor

    It’s hosted by Microsoft.

  6. Install and activate the LEGO Education EV3 extension

    Launch VS Code and click the extension marketplace icon in the left icon bar. Search for LEGO EV3.

  7. Connect the EV3 brick with USB to your computer

    It has probably fully booted by now.

  8. Connect to the EV3 in the file browser

    Go to the file browser in the left icon bar in VS Code. In the bottom part of column with the files there’s a bar called ‘ev3dev device browser’. Click it and then click ‘Click here to connect….’. Select your ev3 device.connect to the ev3 brick in Visual Studio Code

  9. Create an empty EV3 micropython project

    Select the EV3 icon in the left sidebar in VS Code. Click ‘create new project’. Open the ‘main.py’ file

  10. Press F5 to run the demo program

    The EV3 brick will beep, because that’s the only command in the default main.py file. It worked!

Your first EV3 MicroPython Program

Once you have set up VS Code and the LEGO and ev3dev extensions you can your own code. Make a new empty project with the EV3 extension if you haven’t yet done so. Remove all of the text from the main.py file and replace it with the program below.

The first line – the shebang – tells the software which Python to use. The default is micropython, but you can replace it by #!/usr/bin/env python3 to run Python 3.

The next few line import handy classes for controlling the brick. In my program below, we’re just going to show hello world.

Now pressing F5 on the keyboard runs this code on the brick. Neat, huh? It’s pretty fast too. So you can quickly develop and debug.

What next?

Now that it’s running you can go on and program a Lizard in python. Or connect a gamepad and remote control your robot.

Previous

How to connect a PS3 Sixaxis gamepad to an EV3 brick via Bluetooth

How to use a PS3 Gamepad with MicroPython on the EV3 brick

Next

15 thoughts on “Installation guide: EV3 brick with Python and MicroPython”

  1. Hi Anton,

    I’ve just discovered your website. I love your creations – not just great engineering but stylish too! I’m playing around with EV3 MicroPython on my Brick and am a bit stumped by a problem. I wondered if you might have any ideas? The issue seems like it ought to be simple to resolve since the functionality must already be there but I can’t seem to find the right commands to make it work. All I want is to is pass data (in plain text files or just as output from the MicroPython code) to and from the Brick on the fly. I can do the file reading/writing on the brick no trouble and using Visual Studio Code can upload files from the Brick and send files to it, but I want to be able to do this during a program. For example I might want to record the output from one of the Mindstorm devices such as the colour sensor, send this to my computer and plot it on a graph or feed it into Excel or some other software or script. I can write such ouputs into lists, tuples or strings and they will display in the VSC output window, so clearly the information is already making the journey from Brick to PC anyway, I just need to be able to capture it.

    Many thanks for your time.

    • If you use VS Code and micropython, you can just use the print command from pybricks. It will print to the vs code console. Otherwise I suggest you use a micropython logging module. I thinks it’s called ulogging. Or just the plain python open() method to open a new file and write your data to it.

  2. Nice article, really great.
    I followed the steps you provided, but I dont get import-resolved hence no autocompletion in vscode.
    Am I supposed to install other stuff to make it work?

  3. Any update on getting autocomplete to work? I’ve got a dozen 9-12 year old kids (3 fll teams) that are struggling a bit with this. I’d love to get something up and running before school starts.

    My current backup plan is to just go in and code my own class that ‘wraps’ all the documented functionality, then i’ll get autocomplete for the wrapper and I can just hand that off to the kids. I’m not the best at python though so I’m really hoping you guys pull through.

    • Hang in there! Great job with the kids! We are in the process of setting a wrapper up in pypi, so you can pip install it soon. It will take a month or so because of some logistical issues here. Nice work on the wrapper in the mean time. Are you setting up the kids with version control to collaborate on the code?

  4. Hello Anton,

    I am getting error with both this and the gamepad example.

    On this example I am getting an invalid syntax error from this code:
    # Wait until a button is pressed
    while not brick.buttons()
    wait(10)

    On the gamepad example I am getting an error at:
    while event:
    (tv_sec, tv_usec, ev_type, code, value) = struct.unpack(FORMAT, event)

    Any ideas?

  5. My first error was being caused by a missing colon after the while line.

    I am using a generic p3 gampad. Perhaps that is the issue?

    Thanks,
    Patrick

  6. I am new to working with the EV3. I am using “https://education.lego.com/en-us/support/mindstorms-ev3/python-for-ev3” software.

    I am looking to identify if motors from previous Lego Education product NTX can be used with the newer hardware and micro python software.

  7. Please fix pythin script of that article. This fragment:

    # Wait until a button is pressed
    while not brick.buttons()
    wait(10)

    should be corrected to:

    # Wait until a button is pressed
    while not brick.buttons():
    wait(10)

Leave a Comment