MicroPython in PyCharms: Basic Setup

Andy Muehlhausen
7 min readFeb 13, 2020

--

In the last article, I got you up and running with Python on the ESP32. But that article ends by pointing to Thonny as the editor for starting to write your python code onto your microcontroller. It is a fantastic little tool for basic learning and quickly getting off the ground, but it fails to provide all the sugar of a true modern IDE, and if you’re doing something “real”, you’re going to want all the help you can get. In this article, we get going with PyCharms. In the NEXT article, we’re going to be fully automating the process so we can work with incredible efficiency .

JetBrains PyCharm, the best your python can get

also, the sexiest. fullscreen background images, great themes, powerful plugins, slick UI….

Enter JetBrains PyCharm. Easily the best IDE for coding python today. It offers an impressive array of out-of-the-box features to help you unleash your full potential without hassling too much with the setup. (VSCode with Python extension is also great, but considerably less beginner friendly, in my opinion.) Download the Community edition here. Do it now.

Install MicroPython Plugin for PyCharm

After installing PyCharm and opening, go to File>Settings>Plugins>Marketplace and search for MicroPython. Install it.

you probably don’t want the XBee version. maybe you do. no judgement here.

Start New Project, make it Micro

After it installs (does it require you to restart PyCharms? If so, they’ll certainly tell you that), we’re ready to start our MicroPython Project! Goto File>New Project and name your project whatever you feel like. I chose the wonderful name MicroTest, but it literally doesn’t matter. Have some fun, perhaps. If you expand the only expandable thing on this page, the project interpreter, you’ll (hopefully) see the automatic detection of the python installation from before. You don’t need to do anything else except marvel at your computer prowess, and then click CREATE. You made a python project!

Let’s make it micro so we can run it on our beautiful $3 WiFi dual-core computer. In PyCharm, goto File>Settings>Languages & Frameworks>MicroPython. Check Enable MicroPython support. Select ESP8266 from the drop-down for device type. And, man alive, wouldn’t it be great to hit that DETECT button? Well, turns out that it’s not so great, since it will likely fail to detect your device. Try it out, maybe you’re a lucky fella. If not you need to open Device Manager and find the port yourself. If you forget how to do this, refer to my last article under the section “Make sure your board can be seen on your USB”. Type that COM and # under Device path. Mine is COM4. Press Apply, but leave this window open because….

STILL IN SETTINGS, goto Project:YourName>Project Stucture. Right-click .idea and mark it as Excluded. The files in this .idea folder are specific settings just for PyCharm, and Excluding it prevents these files from being copied to your ESP32, where they would be worthless and take up space. Allow them the honor of purpose and leave them behind.

NOW, you may press OK and close settings.

Add to your project, you crazy fool

Check out the structure of your project. If you don’t see a file explorer, click on the 1:Project button below and to the left of File. Or press alt+1 and see it magically appear. See that folder that has the same name as your Project? Mine is MicroTest. That’s where you want to make files that you’re going to send to your ESP32. Let’s do it.

Right-click on your folder name, goto New>Python File. Call it main.py

This specific file will automatically run every time the ESP starts. This is your main big boy, your juevos, your samba partner, your squeeze master 2k30. Create it now. It opens in your editor.

What’s this warning now? Packages required? Here (and forever and always amen) PyCharm holds your hand and guides you to the promised land. Click Install Requirements on the right side of that warning. You’ll see it progressing on the bottom of your editor window until a pop-up says its done.

thanks bebe

Upload a program

Let’s put print(“Hello charming world”) inside of this file and save. We made a program. How to move to device?

In the Project browser on the left (alt+1 if it’s hidden), right-click on your main.py file, and go down to Run ‘Flash main.py’.

You’ll see it output that it’s connected, uploading, and soft rebooting. You just put your program on your device!

If you make a friend give you a dollar RIGHT NOW, you can call yourself an embedded devices professional.

Connect to your device, see your program doing its computery stuff

In PyCharm, goto Tools>MicroPython> MicroPython REPL. This opens a serial connection at the bottom of your screen. What you’re interacting with here is literally the MicroPython running inside of your ESP32. You can type stuff here and actually see it do stuff on the device. If you have an LED and know the GPIO pin, you can type that stuff in here and see it flash.

.

Since you’re connected, if you have a reset button on your device, you can press it now and see all kinds of boot “garbage” stuff show up. This is called a Hard Reboot, because you’re literally pulling the power for a moment to restart it.

Usually, this is more hardcore than you need, and a CTRL-D will do a lighter and faster soft reboot.

In both cases, you will see your amazing prose printed to the screen. Hello charming world, indeed.

A couple incredibly important notes about this serial connection

  1. Like Highlander, there can only ever be one serial connection per COM port.

This is really important to remember. Try to open another MicroPython REPL like you did at the beginning of the last step. Throughout embedded programming life you will see messages like this a million times:

This almost always means that some other thing, somewhere, is still connected to the device. PyCharm has a great feature for smashing this. Simply right-click and say Close All. Try again and it’s going to connect successfully. EZPZ.

2) This terminal has some issues.

You’ve probably already seen this. It can’t really handle your backspaces. Well, it actually IS handling them, but you’re seeing some nonsense garbage instead printing ?[. This is annoying, but we’re rarely ever going to be typing in here, so just deal with it when you have to.

Good to go, but not fully powered up

This is basically the standard setup for MicroPython in PyCharms, and you’re able to do everything, but there are definite glaring issues with the workflow that are still preventing us from iterating and developing as efficiently as possible. In the next article, I’m going to show you my personal scripts, configurations, and tips to allow you to have the most effective workflow possible for MicroPython today, as we create a one-button system for closing all COM ports, flashing all files to device, and opening a console to see our debugging output.

--

--