Attila Vago

Writer of codes, blogs and things that live on the web. Loves craft beer!

EV3 Robotics with Python (Mac)

EV3 Python logo on monochrome Mindstorms projects

Aaaand I am back to writing. Now bare with me, this is absolutely going to be worth your time, and you will get python running on your EV3 brick, but I feel like I have to rant about something first: developers are terrible at sharing knowledge and/or writing documentation that is complete. I don’t know why this is, but 9 times out of 10 whenever I find myself trying out something new, I end up using a large number of sources of information because none of the developers took the time to do a proper job. Aaaargh!

Anyway… back to EV3 and Python. Once I proved myself and the rest of the EV3 community out there that writing Swift code for the intelligent brick is definitely an exciting option and an admirable effort, it is still a half-baked one, awaiting improvements. Naturally, I had to find a worthy substitution, so back to scavenging I was — my goals still being more or less the same: program the brick in a non-visual way, but in any other language than Java. I don’t know… Java is like, just not my thing. I’m too cool for Java and all that Oracle nonsense. I ended up looking at Basic and Python.

Basic flew out the window right away (pun intended) because it runs on … Windows! And I ain’t no PC guy. Mac all the way, so Python it is. The website — as usual — is another prime example and reason why youngsters tend not to go for robotics, or software development in general. Don’t get me wrong, the content is mostly there and it’s mostly accurate, but the presentation is so uninviting that if I wouldn’t have known that ugly sites are a trademark of otherwise brilliant developers, I would have considered it some ancient, poorly-maintained 90's site. But it’s not, and it led me to the EV3Dev site which is a somewhat more inviting project. There is a lot of information there, so much in fact that some might find it overwhelming, which is why I decided to focus on one scenario and get you from zero to hero, which basically means you’ll start with nothing, but by the end of this you will:

  • have an EV3 intelligent brick running EV3Dev operating system (alongside Lego’s OS).
  • have an SSH enabled WiFi connection between your brick and your Mac.
  • be able to copy wirelessly your Python files to the brick (the info on how to achieve this is so hidden inside the cracks of the internet, that this post may actually be the only other place you’ll find it).
  • be able to write (on your Mac) and run basic programs (on the brick) — this is where the EV3Dev site gets it a bit wrong; will go into details a bit further on.

Let’s gather the herd…

What you will need is fairly minimal, though, of course as time goes by and you develop more and more complex robots, the list will grow, but here’s the bare minimum that you will need, almost all of which you can find in the retail Mindstorms set:

  • 1 intelligent EV3 brick
  • 1 touch sensor
  • 1 connection lead (for the touch sensor)
  • 6 AA batteries (preferably rechargeable) — Eneloop is your best bet, but any other brand will do.
  • 1 WiFi nano USB dongle — nano or micro, so that it doesn’t stick out too much. I am using the D-Link DWA-121 Wireless N 150 Micro USB Adapter without any issues.
  • a microSD card between 2 and 32GB. Make sure it’s NOT microSDXC. I am using an old 8GB Samsung Speed class 6 card I got 4 years ago as an expansion card for a phone. Works like a charm. I say going beyond 8GB is pointless and a waste of money, so in all likelihood, you already have a card that’s suited for your robotics needs.

And that is it! You’re set and ready to roll. The first 3 items in the list come with the set, the last 3 you’ll have to get hold of yourself.

Note: I could have opted to go for a setup that does not need a WiFi connection, but I opted not to do so, as having it connected to the network wirelessly and SSHing into the brick has much more educational and practical value than connecting it to your Mac via the USB lead.

Getting started with EV3Dev…

One thing I absolutely love about the EV3 intelligent brick is the fact that you can set up a dual-boot type situation without setting anything up. Yes, it just works as soon as you plug in a microSD card. Any bootable — in our case EV3Dev — Linux based OS will just run, if it senses the presence of a microSD card. As soon as you eject it, the brick is ready to run its own operating system. Magic!

So how do you get EV3Dev onto that microSD card? Usually I would have instructed people to do this from the command line, but some really cool guys and gals at resin.io have developed a nice app called Etcher. It’s dead-simple to use and it’s absolutely free.

To get the EV3Dev OS from the internet onto your microSD card you’ll have to do the following in the exact sequence of steps as below:

  1. Download the latest EV3Dev OS image.
  2. Download and install Etcher.
  3. Plug your microSD card into your Mac (you will likely need a microSD to SD card adaptor, that is usually sold together with microSD cards). Note: all data on the card will be forever erased, so if there’s anything worth keeping, back it up onto another card or your hard drive.
  4. Start Etcher, and select the EV3 Dev .zip file you just downloaded.
  5. Your drive (the microSD card) will in all likelihood already be selected automatically. If you have no other card readers on or connected to your machine, leave it as is, if not change it with the link below the “Select Drive” button and choose the appropriate drive. Note: Etcher is smart enough not to choose your actual hard drive or SSD, so you can’t do too much damage.
  6. Hit the “Flash” button. This will start formatting your microSD card, copying the OS onto it and then verifying it for errors. Once that is complete, Etcher will automatically unmount the drive for you. Do make sure you eject it safely though.

And that’s the way the cookie crumbles! You now have a ready to run EV3Dev OS on the card. No additional OS setup is necessary. It is possibly the easiest Linux OS setups you and I have ever done. Plug the card into the brick and turn it on. After about 30–60 seconds you’ll see the OS running in all its glory.

Note: running EV3Dev from a microSD card does not affect your intelligent brick in any way, therefore it does not void warranty either. Your original Lego OS stays intact on the brick ready to run just as before, as soon as you remove the microSD card and restart the brick.

Getting your WiFi and SSH connection running…

As previously stated, there are other ways to connect to your brick, but showing you how the “big boys” do it, makes the most sense in the long-run. It is surprisingly simple and those of you who have experience with the Raspberry Pi, will find lots of similarities, I know I did.

Once you have plugged in the WiFi dongle, you can use Brickman’s (the name of the user interface in EV3Dev) “Wireless and Networks > Wi-Fi” menu to connect to a network. Make sure you check the “Powered” box so that it starts searching for Wi-Fi networks, and then choose the one you want from the list that appears. Type the password in, and you’re good to go. Note: Officially EV3Dev only supports a few dongles, but in my experience any WiFi dongle that is supported by Linux (plug & play), will work just fine, like my D-Link one for instance.

Getting your SSH connection going is equally simple and painless. What’s really important to know is that your brick has a username and a password assigned to it by default.

Username: robot
Password: maker

Open up your terminal and type:

ssh robot@ev3dev.local

You will get asked for the password so type it in now. If you have never connected before, you will be prompted to confirm the authenticity of the host, so type yes when prompted. To test that things worked as expected, type the fortune command, and it should randomly output a fun quote like:

My brick, just like myself, has an excellent and somewhat dark sense of humour!

Creating a SFTP connection to the brick…

The above step concludes everything that had to be done in order to connect to the brick, so naturally, you’ll be wondering how you actually get programs onto your brick. Well, this is the bit where all other tutorials seem to stop and/or suggest you should write your programs directly in the command line, in Vi or some other similar ugly text editor. From experience I can say, the real world doesn’t quite work like that, and what will seem more familiar to you is writing your program on your machine in a text editor like Sublime Text or Visual Studio Code and then deploy that saved file onto the brick. Not finding any info on how to do this, I used my previous web development and Raspberry Pi server experience and defaulted to FileZilla Client.

Download FileZilla and install it. Then go to “File” > “Site manager” It should look something like this:

FileZilla client connection setup for EV3 brick

Hit the “New Site” button and plug into the fields on the right, your details.

Important: the host information, namely the IP address may not be the same. I have about 8 devices connected to my WiFi, but you may have less or more, so check the screen of the brick as it will show at all times the IP address. It will definitely be a local address.

Set the protocol to SFTP and the user to robot . Hit connect and you will be asked for the password which is of course maker . Guess what? You’re now connected to the brick and will be able to see the files and copy files to it via FileZilla:

Connected to the EV3 brick via FileZilla

Creating and running a python program

This is a fairly simple task if you don’t follow blindly all the information that’s out there, like I did the first time. What saved me was my previous python experience, which you may not yet have, so I am going to make sure your first ever Python program does in fact run without issues. The code I am using, is partially from the EV3Dev documentation with one added line (on line 3).

  • Connect the touch sensor to the brick and turn it on, making sure the WiFi icon on the brick is showing.
  • Once you have created yourfilename.py file and copied it (via FileZilla) to the brick’s home/robot folder, the following command in the terminal will run it:
python3 yourfilename.py

Give it a couple of seconds to initiate, and then by simply pressing the touch sensor, you will change the left side LED colour from green to red. When releasing the button, it will go back to green.

Touch sensor code demonstration video

I believe the above steps and instructions demonstrate the lack of “rocket-science” needed to work with the EV3 brick, a bit more like an engineer and less like a novice. I have always preferred code-based over visual programming. While the official Lego programming environment may be useful to kids, I found it always very cumbersome to use. Code makes a lot more sense to me, and Python just adds to the fun, it being a fairly semantic language that gets you going quickly. Now, don’t get me wrong, no programming language is easy to master, but now — with these instructions — you have the means to make that happen without the frustration if initial setup issues.

Happy coding. Over and out!

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

More by Attila Vago

Topics of interest

More Related Stories