I got a gig to teach MicroPython in a summer camp over the summer and I documented the experience and decided to compile a quick cookbook of the code I used, here it is.

Preference

Ok I going to assume you already visited ev3dev.org link thru the LEGO link and got the MicroPython environment setup. Assuming too that your using more then one robot, so you need to have renamed your Brickman OS with the /usr/sbin/ev3dev-config program as well. I also used MICROSOFT’s Visual Studio Code [MVSC] app with the LEGO mindstorms extension to write the code. You don’t have too, but it’ll make your life easier if you do. Finally I prefer to open a separate terminal window, thru which I made a SSH connection to the brick to run my code. Again its optional, you can open a terminal window thru MVSC, but I like to use a separate terminal connection.

Lesson I

Ok. You got everything loaded and you’re logged in, let’s start coding. Here our first template. Type it in and make sure you give it a name with an extension of “.py”. This script lets you run different code snippets when you press different keys on the brick. In our case we’re just printing out the name of the button your hitting.

Brick Buttons

Just 8 lines, what does it all mean. The first line tells Brickman it is a python script. It is line you’re going to need in every script you write. The second and third lines import two libraries from the pybricks classes. The fourth line an endless loop. Most of the scripts in this code snippets will run in endless loops.

A feature of MicroPython when your script ends, it stops everything, we want it to run forever so that we can see what is happening when we run it. Assuming you run this script in your terminal, you need to use control-C to exit it.

The remaining lines are the code that brings it all together.

A final word on this. There are in fact ten button combinations you can look for, all defined in Button class. Here is a list of other keys you can match in an if statement. Note some of these combinations are several keys being pressed at the same time.

Type it in and run it. Assuming you got no typos it should work perfectly, too well in fact. It fires many more times than once, which for most purposes isn’t useful. Add these two lines to the code to fix this, an additional class import.

Image for post
Image for post

And a call to the wait class within the if statements, so they all look like this. It calls the brick.buttons function, but then waits half a second before it does so again.

And that’s it. You have written your first MicroPython script. A few gotchas to mention perhaps. Upper and lowercase isn’t ignored, its very significant; get it wrong and it won’t work. And the tabs are also significant, get them wrong and it might compile, but won’t do things the way you intended.

Written by

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store