0-ESP32-MicroPython ESP32 ESP8266 MicroPython Project Tech

Getting Started with MicroPython on ESP32 and ESP8266

Getting Started with MicroPython on ESP32 and ESP8266

Discover ways to get began with MicroPython firmware on the ESP32 and ESP8266. We’ll introduce you to MicroPython, present you the variations between MicroPython and common Python, and how one can program your ESP based mostly boards with MicroPython utilizing uPyCraft IDE. After finishing this information, you’ll have your first LED blinking utilizing MicroPython.

What’s MicroPython?

MicroPython is a re-implementation of Python three focused to microcontrollers and embedded methods. MicroPython could be very comparable with common Python. So, should you already know tips on how to program in Python, you additionally know how you can program in MicroPython.

Python vs MicroPython

Aside from a number of exceptions, the language options of Python are additionally out there in MicroPython. The most important distinction between Python and MicroPython is that MicroPython was designed to work beneath constrained circumstances.

Due to that, MicroPython doesn’t include the complete commonplace library. It solely features a small subset of the Python normal library. Nevertheless, it does embrace modules to entry low-level hardware – which means there are libraries to simply entry and work together with the GPIOs.

Moreover, units with Wi-Fi capabilities just like the ESP8266 and ESP32 embrace modules to help community connections.

Why MicroPython?

Python is among the most generally used, easy and easy-to-learn programming languages round. So, the emergence of MicroPython makes it extraordinarily straightforward and easy to program digital electronics. In the event you’ve by no means programmed digital electronics earlier than, MicroPython is an effective start line.

MicroPython’s aim is to make programming digital electronics so simple as attainable, so it may be utilized by anybody. At present, MicroPython is utilized by hobbyists, researchers, academics, educators, and even in business merchandise. The code for blinking an LED on a ESP32 or ESP8266 is so simple as follows:

One nice function of MicroPython is that it comes with an interactive REPL (Learn-Consider-Print Loop). The REPL lets you hook up with a board and execute code shortly with out the necessity to compile or add code.

MicroPython – Boards help

MicroPython runs on many various units and boards, comparable to:

  • ESP32
  • ESP8266
  • PyBoard
  • Micro:Bit
  • Teensy three.X
  • WiPy – Pycom
  • Adafruit Circuit Playground Categorical
  • Different ESP32/ESP8266 based mostly boards

For extra details about different boards that help MicroPython, check out the next hyperlinks:

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

ESP32 is the successor of the ESP8266. So, in the mean time, not all options can be found in MicroPython to take probably the most out of the ESP32 – it’s nonetheless an ongoing challenge. Nevertheless, it’s very usable and you can also make a number of tasks with it.

ESP32 and ESP8266 boards are comparable, and also you gained’t really feel virtually any distinction programming them utilizing MicroPython. Because of this something you write for the ESP8266 also needs to run with no modifications or minimal modifications on the ESP32 (primarily altering the pin task).

Putting in uPyCraft IDE

Earlier than persevering with with this tutorial, you need to set up uPyCraft IDE in your pc. Comply with one of many subsequent tutorials to put in uPyCraft IDE:

Flashing MicroPython Firmware to ESP32/ESP8266

In contrast to different boards, MicroPython isn’t flashed onto the ESP32 or ESP8266 by default. That’s the very first thing you could do to start out programming your boards with MicroPython: flash/add the firmware. Comply with the subsequent tutorial to flash MicroPython firmware in your board:

Getting Began with uPyCraft IDE

On this part we’ll offer you an summary of the uPyCraft IDE software program, in an effort to begin programming the ESP32/ESP8266 with MicroPython.

The IDE is a software program that incorporates instruments to make the method of improvement, debugging and add code simpler. There are numerous methods to program your ESP board with MicroPython. We’ve chosen uPyCraft IDE as a result of it’s easy and intuitive to make use of and works nice with the ESP boards.

At this level, we assumed that you’ve:

  • uPyCraft IDE put in in your pc
  • ESP32/ESP8266 flashed with MicroPython firmware

uPyCraft IDE Overview

Open uPyCraft IDE, a brand new window opens as follows:

Let’s take a better take a look at every part of uPyCraft IDE:

  1. Folder and information
  2. Editor
  3. MicroPython Shell/Terminal
  4. Instruments

1. Folder and information

This part exhibits a number of folders and information. The gadget folder exhibits the information which are at present saved in your ESP board. When you’ve got your ESP32 or ESP8266 related by way of serial to uPyCraft IDE, whenever you increase the system folder, all information saved ought to load. By default, it is best to solely have a boot.py file. To run your principal code, it is suggested to create a important.py file.

  • boot.py: runs when the system begins and units up a number of configuration choices;
  • major.py: that is the primary script that accommodates your code. It’s executed instantly after the boot.py.

The sd folder is supposed to entry information saved on SD playing cards – that is solely works with boards just like the PyBoard that include an SD card slot.

The uPy_lib exhibits the built-in IDE library information.

Lastly, the workspace is a listing to save lots of your information. These information are saved in your pc in a listing outlined by you. This can be a specifically helpful to maintain all of your information organized at hand.

When utilizing uPycraft for the primary time, to pick your working listing, click on the workspace folder. A brand new window pops up so that you can selected your workspace path. Create a brand new folder or choose an present folder to be your working listing.

Then, go to File > Reflush Listing to replace the listing.

Notice: to vary your consumer listing, merely go to Instruments >InitConfig and click on the workspace listing folder to selected a special path.

2. Editor

The Editor part is the place you write your code and edit your .py information. You possibly can open multiple file, and the Editor will open a brand new tab for every file.

three. MicroPython Shell/terminal

On the MicroPython Shell you possibly can sort instructions to be executed instantly by your ESP board with out the necessity to add new information. The terminal additionally supplies details about the state of an executing program, exhibits errors associated with add, syntax errors, prints messages, and so forth…

four. Instruments

The icons positioned on the rightmost aspect permit you to shortly carry out duties. Every button is labeled within the determine under:

  • New file: creates a brand new file on the Editor;
  • Open file: open a file out of your pc;
  • Save file: saves a file;
  • Obtain and run: add the code to your board and execute the code;
  • Cease: cease the execution of the code – it’s just like sort CRTL+C on the Shell to cease all scripts from operating;
  • Join/Disconnect: join or disconnect to your board by way of Serial. It’s essential to choose the serial port first in Instruments > Serial;
  • Undo: undo final change within the code Editor;
  • Redo: redo final change within the code Editor;
  • Syntax verify: checks the syntax of your code;
  • Clear: clear the Shell/terminal window messages.

Operating Your First Script

To get you acquainted with the method of writing a file and executing code in your ESP32/ESP8266 boards, we’ll add a brand new script that merely blinks the on-board LED of your ESP32 or ESP8266.

Establishing a communication with the board

After having the MicroPython firmware put in in your board and having the board related to your pc via an USB cable, comply with the subsequent steps:

1. Go to Instruments > Board and choose the board you’re utilizing.

2. Go to Instruments > Port and choose the com port your ESP is related to.

three. Press the Join button to determine a serial communication together with your board.

four. The >>> ought to seem within the Shell window after a profitable connection together with your board. You’ll be able to sort the print command to check if it’s working:

>>> print(‘Whats up’)
Good day

It ought to print the “Good day” message. Provided that you see that message, you possibly can proceed with this tutorial. In any other case, be sure to have established a serial communication together with your board or that you simply’ve flashed efficiently the MicroPython firmware in your board.

Creating the most important.py file in your board

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

2. Press the “Save file” button to save lots of the file in your pc.

three. A brand new window opens, identify your file most important.py and reserve it in your pc:

four. After that, it is best to see the next in your uPyCraft IDE (the boot.py file in your system and a brand new tab with the primary.py file):

5. Click on the “Obtain and run” button to add the file to your ESP board:

6. The gadget listing ought to now load the primary.py file. Your ESP has the file major.py saved.

Importing the blink LED script

1. Copy the next code to the Editor on the primary.py file:

2. Press the “Cease” button to cease any script from operating in your board:

three. Click on the “Obtain and Run button” to add the script to the ESP32 or ESP8266:

four. You must see a message saying “obtain okay” within the Shell window.

Testing the script

To run the script that was simply uploaded to your board, you’ll want to comply with these steps:

1. Press the “Cease” button

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

For those who’re utilizing an ESP32, your Terminal messages ought to look one thing as proven within the following determine after a EN/RST button press:

Your ESP32 or ESP8266 on-board LED ought to be blinking each 500 milliseconds. Right here’s the place the ESP32’s on-board LED is situated:

Right here’s the ESP8266 on-board LED:

Troubleshooting Ideas

We’ve found some widespread issues and error messages that happen with uPyCraft IDE. Often restarting your ESP with the on-board EN/RST button fixes your drawback. Or urgent the uPyCraft IDE “Cease” button and repeating your required motion. In case it didn’t be just right for you, learn these subsequent widespread errors and uncover how you can clear up them.

Error #1: You get the next message:

Choose Serial Port couldn’t open port ‘COM4’: FileNotFoundError(2, ‘The system can’t discover the file specified.’, None, 2)

Unplug, and plug again your ESP board. Then, double-check that you simply’ve chosen the best serial port within the Instruments > Serial menu. Then, click on the “Join/disconnect” button to determine a serial communication. You need to now be capable of add a brand new script or re-run new code.

Error #2: Hassle importing a brand new script.

already in obtain mannequin,please wait.

Press the “Cease” button in uPyCraft IDE (1 or 2 occasions) to ensure any code that was operating stops. After that, press the “Obtain and run” button to add the brand new script to your ESP board.

Error #three: After importing a brand new script, in the event you see the next message:

Able to obtain this file,please wait!

obtain okay
Traceback (most up-to-date name final):
File “”, line 1, in
NameError: identify ‘os’ is not outlined

It means the brand new file was uploaded to your board efficiently. You possibly can discover that it printed the “obtain okay” message. Press the ESP on-board “EN/RST” button to restart your board and re-run the brand new uploaded script from the start.

Essential: should you maintain having fixed issues or bizarre error messages, we advocate re-flashing your ESP board with the newest model of MicroPython firmware: Flash/Add MicroPython Firmware to ESP32 and ESP8266.

Wrapping Up

We hope you’ve loved studying tips on how to program the ESP32 and ESP8266 boards utilizing MicroPython firmware. Extra tutorials with MicroPython can be posted quickly, so be sure to subscribe to the RNT weblog and obtain our free electronics eBooks.

In the event you choose to program the ESP32/ESP8266 with Arduino IDE, you may need to learn certainly one of these tutorials as an alternative: