beanz Magazine

Lego Mindstorms and MicroPython

NTNU – Norwegian University of Science and Technology on Flickr

New to physical computing? MircoPython may be perfect or you!

If your school has a Lego Mindstorms Club or if you have your own set at home, you have a new option to program your robot: MicroPython! MicroPython is a more condensed version of Python that is intended to be used on a variety of DIY boards and is perfect for those new to physical computing. You may have heard about it if you have used BBC micro:bits. Now, it is available to use with Lego Mindstorms!

Photo of student programming her robot

To start, you can build a simple robot such as Lego’s Robot Educator – the Ultrasonic Sensor Driving Base version will be used in this example.

To get started with programming your EV3 with MicroPython, gather a Windows 10 or Mac computer (must have a SD card slot and Visual Studio Code installed), a microSD card with the EV3 MicroPython image loaded, a mini-USB cable and of course, your robot.

After installing MicroPython onto your microSD card, place tape on the end of the microSD card (to make a pull tab to easily remove it later) and insert it into your robot’s microSD card reader (the robot should be off). After the card is inserted, you may turn on the robot. It will take a minute or two for the robot to boot.

Comparison of code that moves a robot forward – in MicroPython and Lego’s drag and drop visual code builder

Next, you’ll want to create the program that will be run on the microSD card. Open Visual Studio Code on your computer and install the EV3 MicroPython extension.  After doing that, open the MicroPython extension tab and click on Create a New Project. Give your project a name and choose a location for it to be saved. You’ll notice it contains a file named which already has code needed to run your program. Do not alter or delete these lines. You can start writing your own code after those lines.

Lego provides some sample code in their MicroPython documentation that you can run or remix. Try typing the following code. The first four lines defines the motors being used and the Robot Educator’s specific wheel diameter and axle track (in millimeters). The fifth line creates the DriveBase object, used to specify the values needed to move the robot at the desired speed and distance. Do you think the code on line six will move the robot forward or backward? How long will it drive?

left_motor = Motor(Port.B)

right_motor = Motor(Port.C)

wheel_diameter = 56

axle_track = 114

robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track)

robot.drive_time(-100, 0, 2000)

To transfer your code, connect the EV3 Brick to your computer using the mini-USB cable and search for and select your brick. Once you’re connected to your brick, simply press the F5 key to run your program!

For more details and complete sample code for the Ultrasonic Sensor Driving Base model, click on Open User Guide and Examples under Create a New Project (while in the EV3 MicroPython extension in Visual Studio Code).

Learn More

Lego Education: Python for EV3

How to Run Python on EV3 Brick

Getting started with python for Mindstorms

Lego Education: Building Instructions for Robot Educator


SD Card Slot on Mac

Also In The April 2020 Issue

As students reach the age of 13 the importance of them understanding their rights and privacy online becomes crucial.

The iDTech summer camp recently posted 102 questions. Here are a few with links to the full list.

Being well-read is essential in everything in life, and coding is no exception! Here are some book recommendations to make you a coding master.

The circus is in town, but they're missing one of their colourful balls. Let's make one for them!

Sundials were one of the first ways people kept track of time. But how did they work?

New to physical computing? MircoPython may be perfect or you!

It's project time! In this article we go over how the same processes used in big factories can be used to control a simple LED.

What do bubbles, pancakes, and spaghetti all have in common? They're all great for sorting!

Yee-ha! In the wild wild west of the internet, antivirus software is a must-have partner.

Exploring the concept of RAM and how it helps your MInecraft game run better.

Breaking down big problems into smaller ones is a great way to solve them. Let's see how recursion helps us do this!

How did this pale blue dot that we call Earth first begin? The answer is even more fascinating than imagined.

Have you ever wondered why your computer's mouse is called that? Well it all started with a fellow named Douglas and a block of wood...

How do you power devices at the top of mountains and the bottom of oceans? Let's find out!

Links from the bottom of all the April 2020 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for April 2020.