Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (2023)

Learn how to get started with MicroPython firmware on the ESP32 and ESP8266. We’ll introduce you to MicroPython, show you the differences between MicroPython and regular Python, and how to program your ESP based boards with MicroPython using uPyCraft IDE. After completing this guide, you’ll have your first LED blinking using MicroPython.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (1)

What is MicroPython?

MicroPython is a re-implementation of Python 3 targeted for microcontrollers and embedded systems. MicroPython is very similar with regular Python. So, if you already know how to program in Python, you also know how to program in MicroPython.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (2)

Python vs MicroPython

Apart from a few exceptions, the language features of Python are also available in MicroPython. The biggest difference between Python and MicroPython is that MicroPython was designed to work under constrained conditions.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (3)

Because of that, MicroPython does not come with the full standard library. It only includes a small subset of the Python standard library. However, it does include modules to access low-level hardware – this means that there are libraries to easily access and interact with the GPIOs.

Additionally, devices with Wi-Fi capabilities like the ESP8266 and ESP32 include modules to support network connections.

Why MicroPython?

Python is one of the most widely used, simple and easy-to-learn programming languages around. So, the emergence of MicroPython makes it extremely easy and simple to program digital electronics. If you’ve never programmed digital electronics before, MicroPython is a good starting point.

MicroPython’s goal is to make programming digital electronics as simple as possible, so it can be used by anyone. Currently, MicroPython is used by hobbyists, researchers, teachers, educators, and even in commercial products. The code for blinking an LED on a ESP32 or ESP8266 is as simple as follows:

# Complete project details at https://RandomNerdTutorials.comfrom machine import Pinfrom time import sleepled = Pin(2, Pin.OUT)while True: led.value(not led.value()) sleep(0.5)

View raw code

One great feature of MicroPython is that it comes with an interactive REPL (Read-Evaluate-Print Loop). The REPL allows you to connect to a board and execute code quickly without the need to compile or upload code.

MicroPython – Boards support

MicroPython runs on many different devices and boards, such as:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (4)

  • ESP32
  • ESP8266
  • PyBoard
  • Micro:Bit
  • Teensy 3.X
  • WiPy – Pycom
  • Adafruit Circuit Playground Express
  • Other ESP32/ESP8266 based boards

For more information about other boards that support MicroPython, take a look at the following links:

In our projects, we’ll use MicroPython with the ESP32 and ESP8266 boards.

ESP32 is the successor of the ESP8266. So, at the moment, not all features are available in MicroPython to take the most out of the ESP32 – it’s still an ongoing project. However, it’s very usable and you can make a lot of projects with it.

ESP32 and ESP8266 boards are similar, and you won’t feel almost any difference programming them using MicroPython. This means that anything you write for the ESP8266 should also run with no changes or minimal changes on the ESP32 (mainly changing the pin assignment).

Installing uPyCraft IDE

Before continuing with this tutorial, you should install uPyCraft IDE in your computer. Follow one of the next tutorials to install uPyCraft IDE:

  • Install uPyCraft IDE – Windows PC
  • Install uPyCraft IDE – Mac OS X
  • Install uPyCraft IDE – Linux Ubuntu

Note: if you’re having trouble installing or using uPyCraft IDE, we’ve also created an alternative guide on how to program the ESP32/ESP8266 using Thonny IDE.

(Video) How to Setup ESP32 Microcontroller for Arduino and Micropython

Flashing MicroPython Firmware to ESP32/ESP8266

Unlike other boards, MicroPython isn’t flashed onto the ESP32 or ESP8266 by default. That’s the first thing you need to do to start programming your boards with MicroPython: flash/upload the firmware. Follow the next tutorial to flash MicroPython firmware on your board:

  • Flash/Upload MicroPython Firmware to ESP32 and ESP8266

Getting Started with uPyCraft IDE

In this section we’ll give you an overview of the uPyCraft IDE software, so that you can start programming the ESP32/ESP8266 with MicroPython.

The IDE is a software that contains tools to make the process of development, debugging and upload code easier. There are many ways to program your ESP board with MicroPython. We’ve chosen uPyCraft IDE because it is simple and intuitive to use and works great with the ESP boards.

At this point, we assumed that you have:

  • uPyCraft IDE installed on your computer
  • ESP32/ESP8266 flashed with MicroPython firmware

uPyCraft IDE Overview

Open uPyCraft IDE, a new window opens as follows:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (6)

Let’s take a closer look at each section of uPyCraft IDE:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (7)

  1. Folder and files
  2. Editor
  3. MicroPython Shell/Terminal
  4. Tools

1. Folder and files

This section shows several folders and files. The device folder shows the files that are currently stored on your ESP board. If you have your ESP32 or ESP8266 connected via serial to uPyCraft IDE, when you expand the device folder, all files stored should load. By default, you should only have aboot.py file. To run your main code, it is recommended to create a main.pyfile.

  • boot.py: runs when the device starts and sets up several configuration options;
  • main.py: this is the main script that contains your code. It is executed immediately after the boot.py.

The sd folder is meant to access files stored on SD cards – this is only works with boards like the PyBoard that come with an SD card slot.

The uPy_lib shows the built-in IDE library files.

Finally, the workspace is a directory to save your files. These files are saved in your computer in a directory defined by you. This is a specially useful to keep all your files organized at hand.

When using uPycraft for the first time, to select your working directory, click the workspace folder. A new window pops up for you to chose your workspace path. Create a new folder or select an existing folder to be your working directory.

Then, go to File > Reflush Directory to update the directory.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (8)

Note: to change your user directory, simply go to Tools >InitConfig and click the workspace directory folder to chose a different path.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (9)

2. Editor

The Editor section is where you write your code and edit your .py files. You can open more than one file, and the Editor will open a new tab for each file.

3. MicroPython Shell/terminal

On the MicroPython Shell you can type commands to be executed immediately by your ESP board without the need to upload new files. The terminal also provides information about the state of an executing program, shows errors related with upload, syntax errors, prints messages, etc…

(Video) Introduction to ESP32 - Getting Started

4. Tools

The icons placed at the rightmost side allow you to quickly perform tasks. Each button is labeled in the figure below:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (10)

  • New file: creates a new file on the Editor;
  • Open file: open a file from your computer;
  • Save file: saves a file;
  • Download and run: upload the code to your board and execute the code;
  • Stop: stop the execution of the code – it’s the same as entering CRTL+C on the Shell to stop all scripts from running;
  • Connect/Disconnect: connect or disconnect to your board via Serial. You must select the serial port first in Tools>Serial;
  • Undo: undo last change in the code Editor;
  • Redo: redo last change in the code Editor;
  • Syntax check: checks the syntax of your code;
  • Clear: clear the Shell/terminal window messages.

Running Your First Script

To get you familiar with the process of writing a file and executing code on your ESP32/ESP8266 boards, we’ll upload a new script that simply blinks the on-board LED of your ESP32 or ESP8266.

Establishing a communication with the board

After having the MicroPython firmware installed on your board and having the board connected to your computer through an USB cable, follow the next steps:

1. Go to Tools > Board and select the board you’re using.

2.Go to Tools > Port and select the com port your ESP is connected to.

3. Press the Connectbutton to establish a serial communication with your board.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (11)

4. The >>> should appear in the Shell window after a successful connection with your board. You can type the print command to test if it’s working:

>>> print('Hello')Hello>>>

It should print the “Hello” message. Only if you see that message, you can continue with this tutorial. Otherwise, make sure you have established a serial communication with your board or that you’ve flashed successfully the MicroPython firmware on your board.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (12)

Creating themain.py file on your board

1. Press the “New file” button to create a new file.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (13)

2. Press the “Save file” button to save the file in your computer.Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (14)

3. A new window opens, name your file main.py and save it in your computer:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (15)

4. After that, you should see the following in your uPyCraft IDE (the boot.py file in your device and a new tab with the main.py file):

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (16)

5. Click the “Download and run” button to upload the file to your ESP board:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (17)

(Video) Micropython on an ESP32-S2 mini

6. The device directory should now load the main.py file. Your ESP has the file main.py stored.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (18)

Uploading the blink LED script

1. Copy the following code to the Editor on the main.py file:

# Complete project details at https://RandomNerdTutorials.comfrom machine import Pinfrom time import sleepled = Pin(2, Pin.OUT)while True: led.value(not led.value()) sleep(0.5)

View raw code

2.Press the “Stop” button to stop any script from running in your board:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (19)

3.Click the “Download and Run button” to upload the script to the ESP32 or ESP8266:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (20)

4. You should see a message saying “download ok” in the Shell window.

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (21)

Testing the script

To run the script that was just uploaded to your board, you need to follow these steps:

1. Press the “Stop” button

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (22)

2. Press the on-board ESP32/ESP8266 EN (ENABLE) or RST (RESET) button to restart your board and run the script from the start:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (23)

If you’re using an ESP32, your Terminal messages should look something as shown in the following figure after a EN/RST button press:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (24)

Your ESP32 or ESP8266 on-board LED should be blinking every 500 milliseconds. Here’s where the ESP32’s on-board LED is located:

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (25)

Here’s the ESP8266 on-board LED:

(Video) Getting Started with ESP32 - Step-By-Step Tutorial

Getting Started with MicroPython on ESP32 and ESP8266 | Random Nerd Tutorials (26)

Troubleshooting Tips

We’ve discovered some common problems and error messages that occur with uPyCraft IDE. Usually restarting your ESP with the on-board EN/RST button fixes your problem. Or pressing the uPyCraft IDE “Stop” button and repeating your desired action. In case it doesn’t work for you, read these next common errors and discover how to solve them.

Error #1: You get the following message:

>>>Select Serial Port could not open port 'COM4': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)

Or an equivalent message:

>>>could not open port 'COM4': PermissionError(13, 'A device attached to the system is not functioning.', None, 31)

Unplug, and plug back your ESP board. Then, double-check that you’ve selected the right serial port in the Tools > Serial menu. Then, click the “Connect/disconnect” button to establish a serial communication. You should now be able to upload a new script or re-run new code.

This error might also mean that you have your serial port being used in another program (like a serial terminal or in the Arduino IDE). Double-check that you’ve closed all the programs that might be establishing a serial communication with your ESP board. Then,unplug and plug back your ESP board. Finally, restart the uPyCraft IDE – try to select the serial port in theTools > Serialmenu.

Error #2:Trouble uploading a new script.

>>> already in download model,please wait.

Press the “Stop” button in uPyCraft IDE (1 or 2 times) to make sure any code that was running stops. After that,press the“Download and run” button to upload the new script to your ESP board.

Error #3: After uploading a new script, if you see the following message:

>>>Ready to download this file,please wait!... download okos.listdir('.')Traceback (most recent call last):File "<stdin>", line 1, in <module>NameError: name 'os' isn't defined

Or this message:

>>>Ready to download this file,please wait!... download okos.listdir('.')OSError: [Errno 98]

It means the new file was uploaded to your board successfully. You can notice that it printed the “download ok” message. Press the ESP on-board “EN/RST” button to restart your board and re-run the new uploaded script from the beginning.

Error #4: Problem restarting your ESP board, running a new script or opening the serial port:

>>>Brownout detector was triggered

The “Brownout detector was triggered” error message means that there’s some sort of hardware problem. It’s often related to one of the following issues:

  • Poor quality USB cable;
  • USB cable is too long;
  • Board with some defect (bad solder joints);
  • Bad computer USB port;
  • Or not enough power provided by the computer USB port.

Solution: try a different shorter USB cable (with data wires), try a different computer USB port or use a USB hub with an external power supply.

Important:if you keep having constant problems or weird error messages, we recommend re-flashing your ESP board with the latest version of MicroPython firmware: Flash/Upload MicroPython Firmware to ESP32 and ESP8266.

Error #5:When I try to open a serial communication with the ESP32/ESP8266 in uPyCraft IDE, sometimes it prompts the “Burn Firmware” window asking to re-flash the MicroPython firmware.

Basically, we think this is what’s happening: when you’re running a script in your board, sometimes it’s busy running that script and performing the tasks. So, you need to try opening the COM port multiple times or restart the ESP to catch it available to establish the serial communication with uPyCraft IDE.

If you’re running a script that uses Wi-Fi, deep sleep, or it’s doing multiple tasks, I recommend trying 3 or 4 times to establish the communication. If you can’t, I recommend re-flash the ESP with MicroPython firmware.

Wrapping Up

We hope you’ve enjoyed learning how to program the ESP32 and ESP8266 boards using MicroPython firmware. If you want to learn more about MicroPython, take a look at our eBook: MicroPython Programming with ESP32 and ESP8266.

FAQs

Is MicroPython good for ESP32? ›

MicroPython is supported on many different microcontroller platforms, and more are being added all the time. The ESP32 is a great tool for learning MicroPython, as it has a powerful controller (240 MHz) with lots of RAM (520 kB).

How to use ESP32 with MicroPython? ›

How to Load MicroPython on a Microcontroller Board
  1. Install FTDI Drivers. To communicate with the ESP32, the Thing board has an FTDI FT231x USB-to-Serial chip. ...
  2. Download the Latest MicroPython Firmware. ...
  3. Install Python. ...
  4. Install esptool. ...
  5. Flash MicroPython. ...
  6. Test It.

How do I get started with MicroPython? ›

Get Started with MicroPython on Raspberry Pi Pico
  1. Set up your Raspberry Pi Pico and start using it.
  2. Start writing programs using MicroPython.
  3. Control and sense electronic components.
  4. Discover how to use Pico's unique Programmable IO.
  5. Make a reaction game, burglar alarm, temperature gauge, and many more.
Jan 21, 2021

How long does it take to learn MicroPython? ›

On average, it can take anywhere from five to 10 weeks to learn the basics of Python programming, including object-oriented programming, basic Python syntax, data types, loops, variables, and functions.

Is MicroPython slower than Arduino? ›

One disadvantage of interpreted code and MicroPython vs. Arduino is that there's less performance and sometimes more memory usage when interpreting code.

Which chip is best for MicroPython? ›

Raspberry Pi Pico

This is probably the most used MicroPython-supported board out there.

What are the disadvantages of ESP32? ›

ESP32-S2 Bugs & Limitations
  • I2C at 100 kHz bus frequency runs slowly. The default I2C bus clock speed is 100 kHz (100000) . ...
  • No DAC-based audio output. Current versions of the ESP-IDF SDK do not have the required APIs for DAC-based audio output. ...
  • Deep Sleep & Wake-up sources.

Can ESP32 run a Python script? ›

You can write Python scripts that will be directly executed on an ESP32 board. To do so, you have to flash the ESP32 board with MicroPython on it and use an IDE (for example, Thonny IDE ) to code Python scripts and send them to the ESP32. The MicroPython firmware is available on the official website .

Is MicroPython same as Python? ›

It is safe to say that Python and MicroPython are both similar as programming languages, but they contrast as they are different in their syntax. MicroPython is used in more constrained environments with lesser power, such as a microcontroller, while Python is used in larger environments, such as the desktop.

Can we use ESP32 in industry? ›

This ESP32 PLC it has been designed for Industrial applications. It allows the use of WiFi, Bluetooth LE, Ethernet IP protocol and other industrial Ethernet protocols like Modbus TCP and others.

Which is better ESP32 vs ESP8266? ›

The ESP32 is much more powerful than the ESP8266, comes with more GPIOs with multiple functions, faster Wi-Fi, and supports Bluetooth. However, many people think that the ESP32 is more difficult to deal with than the ESP8266 because it is more complex.

What is the default password for MicroPython ESP8266? ›

The password for the WiFi is micropythoN (note the upper-case N). Its IP address will be 192.168. 4.1 once you connect to its network.

Which is better MicroPython or CircuitPython? ›

In Micro Python, you can have different files running at the same time and sharing the same state. Where in Circuit Python there's no sharing of States, so when one thing's running it's the only thing running and that can make it a lot easier for someone new to understand what's happening if something goes wrong.

Is CircuitPython better than MicroPython? ›

CircuitPython code is gonna look a lot like MicroPython because they're based on the same Python implementation. There are some minor differences because CircuitPython is focused on beginners, education, and getting folks started super fast. If you already know MicroPython, 95% of what you know will work just the same!

What is the hardest programming language? ›

7 Hardest Programming Languages to Learn for FAANG Interviews
  • C++ C++ is an object-oriented programming language and is considered the fastest language out there. ...
  • Prolog. Prolog stands for Logic Programming. ...
  • LISP. LISP stands for List Processing. ...
  • Haskell. ...
  • Assembly Language (ASM) ...
  • Rust. ...
  • Esoteric Languages.
Mar 25, 2021

Can I learn Python in 3 months and get a job? ›

If you're looking for a general answer, here it is: If you just want to learn the Python basics, it may only take a few weeks. However, if you're pursuing a data science career from the beginning, you can expect it to take four to twelve months to learn enough advanced Python to be job-ready.

How much Python do I need to know to get a job? ›

How much Python is needed to get a job? 3 months is enough if you want to start with a basic job. A basic job only requires you to know the basics of python. After learning the basic python programming, you will have to learn some advanced topics to be professional in it and have a job.

What are the drawbacks of MicroPython? ›

Disadvantages. MicroPython can do almost anything an Arduino does. One of the differences that can be noticed is that in MicroPython the codes are not as fast, and that they could take up more memory compared to C or C ++ languages. This is particularly not so relevant when used in normal projects.

Which is the fastest IDE for Python? ›

Top Python IDEs
  • IDLE. IDLE (Integrated Development and Learning Environment) is a default editor that accompanies Python. ...
  • PyCharm. PyCharm is a widely used Python IDE created by JetBrains. ...
  • Visual Studio Code. Visual Studio Code is an open-source (and free) IDE created by Microsoft. ...
  • Sublime Text 3. ...
  • Atom. ...
  • Jupyter. ...
  • Spyder. ...
  • PyDev.
Feb 7, 2023

What do professionals use instead of Arduino? ›

Contents
  • Teensy 4.1.
  • Esp8266 DevKitC.
  • Launchpad MSP430.
  • BBC Micro:bit V2.
  • SparkFun Thing Plus.
  • Esp32 DevKitC-VIE.
  • Adafruit Feather Huzzah.
  • BeagleBoard PocketBeagle.
Dec 24, 2022

Which MicroPython board is fastest? ›

The fastest C boards are the ESP32 and the two Pycom boards that share the same hardware, Interestingly in Python, they are no longer equal, It is worth noting that the ESP32 has the least software features of the three and the LoPy4 has the most when running python.

What is the smallest MicroPython board? ›

TinyPICO is the world's smallest, fully-featured ESP32 development board, designed to unlock the power of the ESP32's dual-core 240MHz and internet connectivity, in a package smaller than your thumb! Your very own TinyPICO V3 with USB-C and with MicroPython pre-installed.

What programming language does ESP32 use? ›

ESP-IDF is Espressif's official IoT Development Framework for the ESP32, ESP32-S, ESP32-C and ESP32-H series of SoCs. It provides a self-sufficient SDK for any generic application development on these platforms, using programming languages such as C and C++.

Does ESP8266 support MicroPython? ›

The MicroPython software supports the ESP8266 chip itself and any board should work. The main characteristic of a board is how much flash it has, how the GPIO pins are connected to the outside world, and whether it includes a built-in USB-serial convertor to make the UART available to your PC.

How to upload MicroPython code to ESP8266? ›

How to upload . py-files onto an ESP8266 running MicroPython
  1. Create a new virtual environment.
  2. Install ampy with pip.
  3. Disable debug output on the ESP8266 MicroController.
  4. List the files stored on the ESP8266 microcontroller.
  5. Download files stored on the ESP8266 to your computer.
  6. Modify boot.py to include esp.
Mar 9, 2019

What IDE do you use for MicroPython? ›

uPyCraft IDE: this IDE was specifically designed to be used with MicroPython. It provides tools to upload code to the board and also to flash MicroPython firmware. This last tool is very useful because it provides an intuitive, easy and quick way to burn MicroPython firmware on your boards [uPyCraft IDE Webpage].

How do I run a MicroPython script? ›

Use the ampy run command with the --no-output option to run the script on the MicroPython board. Edit and run the script as much as you need for it to work the way you expect. When you want the code to automatically run on boot use the ampy put command to save the script as a /main.py file on the board.

Videos

1. tinkering with ESP32, Micropython and Arduino
(pileofstuff)
2. ESP-NOW with ESP32 EXPLAINED: Easiest Wireless Communication Between Boards (ESP8266 Compatible)
(Rui Santos)
3. How to start neural networks on ESP32
(Anton Maltsev)
4. Getting Started with Amazon AWS IoT Core using ESP32 || Creating Thing, Policy & Certificates
(How To Electronics)
5. ESP32 Web Server Tutorial with a BME280 Sensor
(educ8s.tv)
6. Getting started with esp32 in HINDI
(The Random Nerd)

References

Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated: 03/10/2023

Views: 6431

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.