In this getting started guide on MicroPython with ESP32 and ESP8266, we will learn how to use program and flash firmware to ESP32 and ESP8266 development boards. Firstly, we will see the difference between ESP8266, ESP32, and Arduino to see why ESP32/ESP8266 should be your first choice for embedded application development using MicroPython. Secondly, we will see how to download and install uPyCraft IDE that we will use to write firmware and flash programs to ESP boards. At the end of the tutorial, you will be able to write and flash your first program to ESP boards using uPyCraft IDE.
ESP32/ESP8266 for MicroPython
Esp32 and Esp8266 are Wi-Fi modules which are extremely cost convenient. They are used in multiple projects including IoT and Automation. Unlike other microcontrollers like Arduino, these modules have wireless networking included which makes it possible to use and monitor devices through Wi-Fi or Bluetooth making it a great cost-effective and useful tool. Esp32 and Esp8266 both come with GPIOs. Thus, many protocols including PWM, I2C, and ADC, etc. are supported through it.
Comparison between Esp32 and Esp8266 with Arduino Mega
|Digital I/O pins||36||11||54|
|ADC||12 bit||10 bit||10 bit|
MicroPython is the reimplementation of the software Python3 which is specially designed for microcontrollers and embedded systems. It is very similar in use to that of Python. For example, if someone knows how to write simple programs in Python then it is very easy to work in microPython as the programming language is the same. The only major difference is that it does not come with a full standard library, but it can be easily used to access lower-level hardware because it has those modules present in it.
Why do We use MicroPython ?
The reason for Micro-python being used so readily in the embedded fields is because it is a simple and easy to learn software development language even for beginners than C and C++. Another important is that the Python programming language is easier to learn and all beginners find it very easy when comes to the learning curve for Python as compared to C and C++. Because MicroPython is similar to python. On top of that, we can use it to program embedded boards. Therefore, the main objective of MicroPython developers is to make embedded programming as simple as possible so that hobbyists, researchers, teachers, educators, and beginners can easily learn and dive into the embedded field.
Last but not the least important reason to use MicroPython is that it contains REPL (Read-Evaluate-Print Loop) which let us run the code on ESP boards without going through program compilation process.
As compared to C and other embedded proramming languages, the LED blinking code for ESP32 and ESP8266 can be written in few lines as shown:
import time from machine import Pin led=Pin(2,Pin.OUT) #create LED object from pin2, Set Pin2 to output while True: led.value(1) #Set led turn on time.sleep(0.5) led.value(0) #Set led turn off time.sleep(0.5)
There are several devices which can be run through this software but for now, we will focus on esp32 and esp8266. As these two boards are similar they are programmed in the same way in micro-python.
MicroPython Supported Board
There are many MicroPython supported board available in market. We have mentioned name of some of them below:
- Adafruit Circuit Playground Express
- Micro: Bit
- Teensy 3 Serial Boards
- WiPy – Pycom
- All types ESP32/ESP8266
You can look in these guides to find more information on supported boards for MicroPython
Installing uPyCraft IDE
Now we will learn how to use ESP32 and ESP8266 in Micro-python firmware. In order to do that we need to first install an IDE. There are many integrated development environments available that can be used to write programs for ESP32 and ESP8266.
We will be using uPyCraft IDE as it can be run in any major operating system and is always extremely interactive and easy. We will be installing uPyCraft IDE in Windows.
Before we start the installation of the uPyCraft IDE IDE, make sure you have the latest version of Python 3.7 or the latest installed on your windows-based. If you don’t have the python 3 package installed on your Windows, follow these steps to install one:
- First of all go this link here and download latest version of Python3.
- After you click the Download Python 3.9.2 button an .exe file will start downloading. After the download is completed click on it and the following appears.
- Press the run button. The following screen appears. Make sure to tick Add Python 3.9 to a path and then click Install Now.
- After the installation is complete the following screen appears showing that the download was successful.
Download and Install uPyCraft IDE
As we mentioned earlier, we will use uPyCraft IDE to program ESP32 and ESP8266 development boards. The reason we are using uPyCraft IDE is that it is simple and easy to use IDE among other micropython based IDE’s available in the market. Now we will move onto the next step. Now follow these steps to download and install uPyCraft IDE:
- In order to download the uPyCraft IDE, go to this link here and download the .exe file for windows as shown below. You can also download for Linux and Mac if you are using Linux or Mac operaing system.
2. After that click on the uPyCraft_VX.exe which you download in the previous step:
3. After installing the IDE, click on its icon and the following screen will appear.
Till now we have downloaded and installed uPyCraft IDE. We will use this IDE to write firmware and flash firmware to ESP32 and ESP8266.
uPyCraft IDE Introduction
Now lets explore different windows and components of uPyCraft IDE. First open uPyCraft IDE, you will see a window like this:
uPyCraft IDE is a integrated development environment which is used to program development boards in MicroPython language. It makes the steps for firmware development, code debugging and flashing code to ESP board very easier under a single package.
Now we will learn about the different sections which are found on the IDE. The picture below shows the four main sections found in the IDE.
- Folder and files
- Micro-python shell terminal.
On the furthest right side, you can see the different icons which can perform multiple tasks. It is very convenient to use and each function can be achieved with ease. The picture below shows each icon labeled.
- New file: By clicking on this icon a new file will be created in the editor.
- Open file: This icon opens up previously saved files.
- Save file: By clicking on this icon the file will be saved automatically.
- Download and run: This button uploads the program code written in the editor onto your module and runs it.
- Stop: By clicking on this icon the execution of the program code stops.
- Connect/Disconnect: Tools>Serial is used to connect your module through the serial.
- Undo: By clicking on this icon the last change done to the program code is erased and it goes back to its previous state.
- Redo: This icon restores the program code which was erased by the undo button.
- Syntax Check: This is a very helpful feature that helps us check any syntax mistakes in the program code in the editor.
- Delete: Clears the messages in the Shell Micro-python terminal.
In the editor section, we write our program code which is then executed and run onto the esp32/esp8266 module serially. The extension of files is .py. We can open and create multiple files for a single project here in the editor window.
3. Folder & Files
This section is found at the extreme left hand side of the screen. Four different folders can be seen namely:
Device: In this folder, you can view all the files which are saved on your module. When you click the device folder all the files already stored would be visible. boot.py file is the default file that runs when the program is started and the main.py file contains your main program code
sd: In this folder you can view files which are already stored in your sd card. This option is available for those modules which support it.
uPy_lib: This is the in-built uPyCraft library. You can view the individual files by clicking on this.
Workspace: This is a very handy folder which helps you organize your files and save them in the particular directory you choose. In order to set your path double click on the workspace folder and choose your path where you want to save all the program code. In order to update your current directory go to File>Reflush Directory as shown in the picture below.
After that go Tools >InitConfig and select the workspace directory where you want to store project files.
4. Micro-Python Shell Terminal
All messages are displayed in this section including errors that are found at the bottom of the screen. If you don’t want to upload new files you can directly write the command in the terminal so that it can be performed quickly.
Flashing MicroPython Firmware on ESP32/ESP8266
After installing uPyCraft IDE and getting its overview, before we run a sample MicroPython program on ESP32 and ESP866, we should know that how to flash the MicroPython program to ESP development boards using uPyCraft IDE.
To Flash ESP32 and ESP8266 boards with uPyCraft IDE, we need to download MicroPython firmware for ESP32 and ESP8266. Follow these steps to download MicroPython firmware for ESP boards.
Downloading MicroPython firmware
By default, the Esp32 and Esp8622 are not flashed with micro-python. It is very easy to flash Esp32 and Esp8622 with the help of uPyCraft IDE. To download the latest version of Micro-Python firmware for the ESP32, go to the website Micro-python and in its download page here (https://micropython.org/download/esp32) and select the latest firmware for Esp32. The format will be of the type Esp32.bin
Similalry, you can download ESP8266 MicroPython firmware from this link.
Selecting the Serial Port
In order to select the serial port which will be connected open the uPyCraft IDE and go to Tools>Serial and select the Com port for your Esp32. To select the module go to Tools>Board and select Esp32. Here you can see that two serial ports are showing in my case. Because I have connected both ESP32 and ESP8266 boards with my computer.
To select boards, go to Tools>Boards and select the board which you are using. For example, if you are using ESP32, select ESP32 and if you are using ESP8266 then select ESP8266.
Flashing/Uploading MicroPython Firmware
As we have already selected our board and the Com port, now we will flash the firmware to ESP32 or ESP8266. In order to do that go to Tools>Burn Firmware and click on it.
After we click BurnFirmware the following screen will appear. Choose the following options.
- In the board section, choose ESP32 or ESP8266 whichever board you are using.
- For erase_flash choose the yes option.
- Select the correct Com port according to your module.
For firmware choose the Users option and place the Esp32.bin file which we downloaded before as shown below:
Now we have selected all the correct options. It is the time to flash micropython firmware to ESP32 or ESP8266. For example, if you are using ESP32.
Press the hold-down “BOOT/FLASH” button on the Esp32 module. It can be seen clearly in the following picture.
When we will hold down the BOOT button at the same time click OK in the burn firmware window.
After the burn and download process started, you can release the boot button. The burning process will be completed in a few seconds and the firmware would be flashed onto Esp32.
Erase False Error
If you don’t see progress in the “EraseFlash” bar and you will get this message “erase false” after some time.
That means either ESP32 or ESP8266 boards are not connected with your computer correctly or you have not pressed the “BOOT/FLASH” button correctly. In order to avoid this error, do the above steps again and this time make sure to press the “BOOT/FLASH” button on your ESP boards so that it can go to flashing mode.
A similar process is done if we want to flash Micro-Python with Esp8266. Just download the firmware from Micro-Python download for Esp8266 and follow the same steps as Esp32. Instead of choosing Esp32 choose Esp8266 instead.
Now we are ready to program our modules in MIcro-python with the help of uPyCraft IDE.
Writing Your First MicroPyhton Script
So far in this getting started tutorial on micropython for esp32 and esp8266 learned to install software that is required to process and execute cause on esp32 and esp8266. now let’s write a simple script on esp32 and execute it on the esp32 development board. We will write a simple LED blinking script for the esp32 and esp8266 Python programming language and will upload it to the ESP board and see how it works.
Serial Connection with ESP32 and ESP8266 uPyCraft IDE
Now let’s see how to establish a communication between uPyCraft IDE and ESP32 and ESP8266. After flashing micropython firmware to ESP32 and ESP8266, we can communicate with these ESP boards using this IDE.
- Now connect your boards to your computer using a USB cable.
- After that go to tools-> boards and select the board which you are using either ESP32/ESP8266.
3. Now press the connect button from the right toolbar. It will make the serial connection between IDE and ESP32/ESP8266.
4. As soon as you click the connect button, the >>> will show up in the Shell window of uPyCratf IDE and it shows that a successful connection has been developed with your board. Now to see its working, you can type this command on the shell window and it will print “Hello world” on console in response.
>>> print('Hello World') Hello World >>>
Note: If you don’t see the output of print() command on the shell console of uPyCraft IDE that means your ESP board has not made a serial connection with your computer and you cannot flash micropython code to your ESP boards. You should check the connection and try again.
Creating the main.py file on your board
In this section, we will see how to create new MicroPython file and download and run on ESP32 and ESP8266 boards. Follow these steps:
1. From the left sidebar, click the “New file” button and it will create a new file and it will up in editor window as a untitled.
2. Now copy this MicroPython code into your editor window.
from machine import Pin from time import sleep led = Pin(2, Pin.OUT) while True: led.value(not led.value()) sleep(0.5)
3. After that click on the “Save file” button to save the file on your computer. You can select any location in your computer where you want to save this file.
4. When you click on the “Save file” button, A new window as shown below, will open. Given your file a name such as main.py and save it in your computer.
Note: You can give any name to this file. But the extension of file should always be .py.
5. After saving the file, you will find the following in your uPyCraft IDE (the boot.py file under).
6. Now let’s download the script to ESP32 or ESP8266. To download script, click on the “Download and Run button” as shown below:
7. Once you click the “Download and Run button” and code downloaded successfully to your board, you will see a message saying “download ok” in the Shell window.
At the end, we need to test the script which we just downloaded to ESP32 or ESP8266 board. In order to run the MicroPython script on ESP boards, just press the stop button.
Now press the ENABLE or RESET Button of ESP32 or ESP8266.You should see that on Board LEDs of ESP32 or ESP8266 will start blinking with a delay of half second.
This is all about getting started with Micropython for esp32 and esp8266 using uPyCraft IDE. In the coming tutorial, we will further explore ESP32 and ESP8266 peripherals using MicroPython.