Controlling Motors and Read Sensors of LEGO MINDSTORMS with Python

A LEGO vehicle with two motors and an ultrasonic sensor that stops 30 cm in front of an obstacle and then reverses was one of the first programming exercises in the data acquisition practical that we developed in 2017. We used the MATLAB Support Package for LEGO MINDSTORMS EV3 Hardware to connect the LEGO vehicle with MATLAB Desktop on a computer to solve for Exercise #2. While this support continues to be offered for Windows, Linux and macOS (Intel), it is not available for macOS (Apple Silicon). Exercise #15 uses a Python solution for programming the LEGO vehicle – but do not expect too much!

Building a LEGO vehicle at the beginning of the one-week course on data acquisition in geosciences is always met with great enthusiasm by the master’s students at our institute. The Lego Mindstorms EV3 used for this is the third generation of the Mindstorms series of Lego robotics kits, which was released in 2013.

In fact, there was a successor in 2020 with the Robot Inventor (Hub No. 13), but this product was also quickly discontinued. Today, there is still the LEGO Education Spike series, which, like Mindstorms, offers motors and sensors. Mindstorms EV3 is no longer available, but is still used in many schools and universities. MathWorks offers hardware support for the EV3 set, but no longer for current Apple Silicon computers.

It seems that LEGO is not making any concessions, instead relying on (Micro)Python for programming. A very good introduction to LEGO programming is the open-source project Pybricks. Exercise #15 is intended to introduce students to Python programming of LEGO Mindstorms. Unlike in MATLAB in Exercise #2, a series of preparations are necessary here:

  1. Unfortunately, you can’t use the firmware and operating system of the Lego Brick. Instead, you boot from a microSD card with an alternative software.
  2. The connection between LEGO hardware and Python is done via a hardware support that is operated via Visual Studio Code.
  3. Python is operated via the pybricks package, which must be installed and kept up to date.

These are three steps, which are actually 19 steps if you look at the solution to the exercise in the password-protected Course Materials area of the blog. It took me several days to work out this solution. There are usually many solutions on the internet, most of which are outdated or incorrect, with the most reliable being the instructions from LEGO and Pybricks – and hopefully now also my instructions in the Course Materials section of my blog.

Please note that this solution worked for me on a Macbook Pro with an Apple Silicon M4 Max, on which several different versions of Python were installed (11 Mar 2025). I can help you if you also have an Apple Silicon Mac, but not if it is a different computer with a different operating system and you want to try Python.

References

Trauth, M.H. (2021) Signal and Noise in Geosciences, MATLAB Recipes for Data Acquisition in Earth Sciences. Springer International Publishing, 544 p., https://doi.org/10.1007/978-3-030-74913-2