MicroPython on ESP32: The Setup

Andy Muehlhausen
5 min readFeb 10, 2020

I love python and I love Arduino, and I especially love Espressif’s ESP line of WiFi SOC. Why am I just now getting into MicroPython on an ESP32? Historically (and still) I have been mostly interested in tiny, battery powered, highly optimized devices, and for such a task you’re still going to want to use c++ (which I also love).

But recently I’ve started considering how I would teach a beginner to love coding, and so I’ve been testing some python lessons on a friend. The big finale I’m teasing for him is “when you’re done, you can program whatever LEDs, fans, security systems, and iguana-tank automation you want using the knowledge you already have!”

Well, maybe the promise was premature. I’m starting to play with MicroPython on ESP32 and I’m finding that it’s still not a very complete platform. It presents a lot of obstacles that a true beginner is seriously going to struggle to overcome. Lucky you, I’ve smashed through them all to present to you my “current best-case setup.”

Install Python 3

Depending on your level of experience, this might be a whole can of worms. I’m not going to go into detail here because there are literally thousands of articles out there already covering this. You need to install python3 on your computer, and you need to be able to access its package manager, pip to install some other things. Your best bet is just to Google install pip <your operating system>. By the way, pip and pip3 are interchangeable for the sake of this article. At some point, you’re going to be able to type pip into a prompt and see this:

and you’re good to go. remember that we’re considering pip and pip3 to be the same thing atm

Make sure your board can be seen on your USB

When you plug your ESP into your computer via USB, it needs to recognize the serial connection interface. On Windows, press the Windows Key ⊞ and type Device Manager, and open her up. Check under Ports, and look for something with the word SERIAL in it. If you don’t see it in this list, you need to install the driver. You will almost certainly be able to find this information on site where you purchased your device, or, again, by googling install serial driver <your device name>. It’s also very important to note the port currently being used by your computer for this connection. In my photo above, it’s using COM4. We’ll need to reference this later.

Flash your ESP32 with MicroPython

You can kind of think of micropython as the operating system on your ESP, so you have to install it before you can run any programs, etc… Head on over to https://micropython.org/download/esp32 and grab the latest stable release, which is the top one in the list that doesn’t have crazy numbers at the end:

Your release numbers might be different than mine

Browse on into your Downloads folder and open a terminal there. POWERTIP: you can type cmd right into the address bar to get a terminal in that folder.

cmd right in the address bar
.. grants your wish of a cmd at that location

Use pip to download the python tool that will flash our esp. Type all the next lines into our command prompt:

pip install esptool

Now erase our device (NOTICE that we use the COM port from above. Mine is COM4 but yours is whatever was listed in Device Manager):

esptool.py --chip esp32 --port COM4 erase_flash

Now flash our Micropython “OS” onto our device, again using our COM port, but also using our specific filename that we downloaded, mine is esp32-idf3–20191220-v1.12.bin, and because we opened our terminal inside of the downloads folder, the file should be accessible from here:

esptool.py --chip esp32 --port COM4 --baud 460800 write_flash -z 0x1000 esp32-idf3–20191220-v1.12.bin

You’ve successfully installed Micropython onto your ESP32!!

HELLO WORLD and FLASH THAT LED

For the moment, we’re just going to get you flashing a LED as quickly as possible. If you’re a true noob, I strongly recommend following this tutorial to get off the ground with the Thonny, the made-for-micropython IDE that simplifies a lot of things at the expense of user power. If you’re NOT a noob, skip ahead to Part 2 (coming soon) where we set up PyCharm as our MicroPython IDE. True noobs will want to follow this tutorial from randomnerdtutorials.com to just see their device working:

And you’ll be saying hello in no time!

To flash the LED, you need to know that every device has them on a different GPIO pin, and some devices have none. Mine is on GPIO 22, and I flash it by running this.

Done for now, but better tooling ahead

I hope you’re up and running with Micropython on the ESP32. But this isn’t a great setup. Mostly, the IDE is lacking a lot of modern features that make us great coders. So in the next tutorial we’re going to get up and running on PyCharm, my far-and-away favorite IDE for coding python, and you’ll be able to blast your genius more easily into existence.

Here’s Part 2

https://medium.com/@andymule/micropython-in-pycharms-basic-setup-9169b497ec8a

--

--