Getting started with MicroPython: Program ESP32 using MicroPython

Getting started with ESP32 MicroPython: Program ESP32 using MicroPython

ESP32 is a great development board for the IoT applications as it is low cost, low power consumption and has many powerful features. ESP32 can be programmed in various Environments like Arduino IDE, Espressif-IDF, MicroPython, Lua etc. We already learned about programming ESP32 with Arduino IDEwith Espressif-IDF, and with Lua in previous articles. In this article you will see how to use MicroPython with ESP32 to flash ESP32 and blinking an LED using MicroPython.

 

Prerequisite

  1. Windows PC with Python 3.7.X
  2. uPyCraft IDE
  3. ESP32
  4. USB Cable
  5. Basic knowledge of Python code.

 

Installation of Python 3.7.X

MicroPython is a structured implementation of Python 3 language that includes Python standard library and is optimized to run on microcontrollers. MicroPython is an interesting and easy to use environment for microcontrollers and considered one of the easiest way to program microcontrollers. So first to work with MicroPython you have to install Python 3 in your PC.

To install Python 3 following steps should be performed:

  1. Open your web browser and search for the given link:

https://www.python.org/downloads/

 

  1. After this, you will find a window as shown in figure below. Click on Download Python 3.7.2 to download Python 3.

 Installation of Python 3.7 Program ESP32 using MicroPython

 

  1. After downloading the file install it in your PC and after installing you will get a message on your screen that Setup was successful this means that Python  3.7.2 is successfully installed in your PC.

 

uPyCraft IDE for ESP32

As discussed earlier, ESP32 can be programmed using various environments like Arduino IDE, MicroPython, ESP-IDF, etc., so to program ESP32 using MicroPython firmware you will be required an IDE so I will use uPyCraft IDE for this. In the following steps I will show you how to install uPyCraft IDE software:

  1. uPyCraft IDE is a GitHub repository so to download the software open your browser and go for: https://github.com/DFRobot/uPyCraft

 

  1. To download IDE click on ‘Clone or download’ and download Zip file and it will start downloading its Zip file.

 Download Files for uPyCraft Master

 

  1. After downloading the Zip file extract the file to any drive of your PC e.g. I extracted it to my Local disk C.

 Extract Files for uPyCraft Master

 

  1. Open your local disk to which you have extracted the file and you will find folder named uPyCraft-master.

 Unzip Files for uPyCraft Master

 

  1. Open this folder and you will find lots of files in it which consists of bin files, zipped example files, etc. Here open the file named uPyCraft.

Install uPyCraft Master

 

  1. Open this file and you will find a new window with the uPyCraft IDE software.

 Run uPyCraft Master

 

This is the software of uPyCraft IDE in which you can see various example files also saved with .py extension file. It works on MicroPython Programming.

This is how you successfully installed uPyCraft IDE in your PC.

 

Flashing/Uploading of MicroPython Firmware to ESP32 board

Following are the steps to flash MicroPython to your ESP32:

  1. Open your browser and go to

http://micropython.org/downloads

 

  1. After scrolling down the webpage you will find Firmware for ESP32 boards in which you have to download the latest bin file.

 Flashing of MicroPython Firmware to ESP32 Board

 

  1. After downloading the file connect ESP32 to your PC and check on which COM Port it is connected, to check go to device manager and check Port as in my PC it is connected to COM6.

 Connect ESP32 with PC

 

  1. Now open your uPyCraft IDE and go to Tools and Select serial Port COM6 and on selecting port you will find >>> in output window and in board option select ESP32, now go to burn firmware and a window will appear.

 Check Serial Port for ESP32

 

  1. Enter following details in the window:

Board                         --------> esp32

Burn_addr               --------> 0x1000

erase_flash              --------> yes

com                            --------> COM6

Firmware Choose   --------> Users

 Update Firmware for uPyCraft Master

 

In Firmware Choose option select the bin file you have downloaded earlier and click ok and then firmware started uploading to your ESP and you will see below window:

 Burn Firmware for uPyCraft Master

 

If you find command “False Erase” then firmware is not uploaded and you have to upload it again by performing above steps carefully.

 Erase Firmware for uPyCraft Master

 

After uploading MicroPython firmware you are now able to program your ESP32 using MicroPython.

 

LED Blinking using uPyCraft on ESP32

To start a project on ESP32 using uPyCraft following steps should be performed:

  1. Open your uPyCraft IDE and go to file and create new file.

 LED Blinking using uPyCraft on ESP32

 

  1. Now go to Tools and select Serial port and Board i.e. ESP32, After selecting these parameters a boot.py file is generated it means you are in right direction.

 Booting uPyCraft on ESP32

 

  1. Now type the following code in the main file that you have saved earlier and save the code
from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

while True:
  led.value(not led.value())
  sleep(1.0)

 

  1. After saving the code go to Tools and click on Download and Run.

 Download and Run Code for LED Blinking using uPyCraft on ESP32

 

  1. After running the code you will see a LED will start blinking on your ESP at an interval of 1 second and following message on output window.

 LED Blink on ESP using uPyCraft on ESP32

 

  1. In this you can stop your process at any time, for this go to Tools and click on Stop.

 

After clicking on Stop your LED will stop blinking and will start again if you click on Download and Run.

This is how you are successfully able to blink LED using MicroPython on ESP32. Now you can easily program ESP32 for any IoT project, and we will also use it in our future IoT projects.