ESP32 is a powerful device for IOT development with WIFI & Bluetooth.Compared to NodeMCU it has more ADC pins , DAC & much more.
ESP32 can be programmed by many ways including Arduino core.
This post is on programming ESP32 using MICROPYTHON.
MicroPython is a lean and fast implementation of the Python 3 programming language that is optimised to run on a microcontroller.
MicroPython has advantages over traditional programming software such as C/C++ because it is essentially a live development environment and you write and debug code much faster…in fact 5-10 times faster than C++.
when a device boots up using MicroPython it does not consume much memory and thus doesn’t incur current drain.Micropython also has a number of Micropython specific libraries for accessing hardware level features.
Adafruit also introduced CircuitPython ,Soon after MicroPython started picking up the pace. However, CircuitPython offers support for the Adafruit range of hardware only.
Python allows for rapid feedback. This is because you can interactively enter commands and get a response using the REPL –read–eval–print loop. You could even tweak your code and run it right away, rather than iterating through code-compile-upload-execute cycles.
This post is on implementing MICROPYTHON on ESP32.
As a programming environment , many IDE s are available , of which THONNY IDE is simple & powerful.
Thonny IDE : An integrated development environment (IDE– Integrated Development Environment) for Python that is designed for beginners. It stands out for its simplicity of installation and use, ease of writing and debugging of code, possibility of incorporating plugins and its good documentation.
uPyCraft IDE : It is anIDE specifically oriented for MicroPython, which makes it one of the besteditorsfor this use. It allows you to upload the MicroPython firmware to the microcontroller in a very simple way (as an alternative toesptool.py)
Visual Studio Code (VSCode) , using the PyMakr extension: It is avery sophisticatedIDE , which supports many practical functionalities working with code, but is only recommended for advanced users
PYCHARM is for advanced users.There is community edition for free.There’s a PyCharm plugin that adds MicroPython support to your IDE. It’s available right in the plugin marketplace
visit https://thonny.org/ & download the installer relevant for you (available for Windows,Mac & Linux)
Install THONNY IDE by double clicking the downloaded installer.
Open THONNY IDE .
The top portion is the EDITOR where you write your Micropython code.
Bottom is the SHELL.
As we’ve not yet installed Micropython interpreter, the Shell displays the default Python prompt.
‘>>>’ is the prompt of the interactive Python interpreter, meaning that the interpreter is ready to get Python statements typed in.
The PROMPT Is called CHEVRONS.
To get the Micropython prompt, click on Tools –> Options
Under options window click on INTERPRETER.
Under first drop down menu select MICROPYTHON (ESP32)
Under Port , select the port number where ESP32 is connected.
Click on Firmware install bar.
Micropython firmware .bin file can be downloaded from
http://micropython.org/download
Under ESP32 select the GENERIC .bin file & download it.
http://micropython.org/resources/firmware/esp32-idf3-20200209-v1.12-154-gce40abcf2.bin
Now , from the Thonny install firmware window , click browse and locate the downloaded .bin file.
Click on install button to start the firmware installation.
You need to press & hold the BOOT button on ESP32 module for a moment.
You may get an error stating ESPTOOL is not installed.
From within Thonny IDE itself you can install it.
Click Tools –> Manage Plug ins..
Under plug in window search for esptool & install it.
Now if you install firmware , you can see the writing starts at location 0x1000
Once the firmware is installed you can see the Micropython prompt under SHELL.
You can type in help() to see a list of python commands to access modules ,network, etc..
help(“modules”)
displays the list of modules available.
machine, uos, esp , time , network are some of the frequently used modules.
To control GPIO pins you need to import machine module.
the machine module of MicroPython, which is the “library” of functions and classes , to control the hardware of the microcontroller.
The machine module has 12 functions and 12 classes :
- Functions : reset (), reset_cause (), enable_irq (), disable_irq (), freq (), idle (), time_pulse_us (), sleep (), lightsleep (), deepsleep (), wake_reason () and unique_id ().
- Classes : Timer (), WDT (), Pin () , Signal (), TouchPad (), ADC (), DAC (), I2C (), PWM (), RTC (), SPI () and UART () ) .
We can see this listing in Thonny using the function help(machine).
To control an LED on / off, the following must be done:
- First, the Pin class of the machine module is imported , which allows controlling the inputs / outputs (General Purpose Input / Output – GPIO -) of the microcontroller – and therefore, of its corresponding pins on the board. Since pin 02 ( GPIO 02 ) is connected to the integrated LED, you also have control over it.
- Then an object that we will call pin_02 is initialized to identify it, turning GPIO 02 “2” into an output pin “ Pin.OUT ”.
Among the classes of the module, the class
Pin()
is used to create objects to control inputs / outputs of the microcontroller ( GPIO ), which will help us to turn on /off the LED.
Additionally, if we ask for information about the class Pin()
we can see that it has 5 methods – Pin.init (), Pin.value (), Pin.off (), Pin.on () and Pin.irq () – :
In order to use this class , we need to know how your constructor and its methods work .
The constructor is the subroutine whose mission is to initialize the object . The constructor of the Pin class is:
class machine.Pin (id, mode, pull, *, alt)
The arguments of the constructor are:
The arguments to turn on the blue LED integrated in the ESP32 DEVKIT board will be the following:
- id “ 2 ” since the on board LED is connected with pin 02 ( GPIO 02 ).
- mode “ Pin.OUT ” to make pin 02 become a voltage output pin .
- pull “ None ” to not configure pull-up or pull-down resistors (they are only useful in the case of being an input pin – Pin.IN -). It is not necessary to define this argument.
- value “ 1 ” to turn on the LED-3.3V voltage output.
Once the code has been executed, we will have initialized an object , which has been assigned the name pin_02 , whose initial values convert pin 2 (GPIO 02) into an output pin and its status is on (3.3V voltage) .
The methods that we can use once the object is created allow us to manipulate (modify) its functionalities . In the Pin class we have the following:
- Pin.init () : Modify theinitial arguments of the constructor . You can modify the mode , pull , * (value) and alt argumentswhen new values are defined.
- Pin.value () : allows you to read the presence / absence of input voltage of the pin (without an argument is provided) or set an output voltage on the pin providing the argument. The arguments can be “ 0 ” off (0.0V) and “ 1 ” on (3.3V). Booleans can be usedas arguments.
- Pin.off () : Sets a pin output voltage of 0.0V- off.
- Pin.on () : sets an output voltage of the 3.3V pin – on -.
- Pin.irq () : enables external interruptions in the pin , as long as it is an input pin .
Put together
First from machine module import the class Pin.
Then assign the built in LED GPIO pin 2 & declare it as Pin.OUT
The pin that is connected to the led is instantiated & the second argument is set as OUT ,indicating
that the pin acts as OUTPUT.
Now led.value(1) makes the on board LED ON.
led.value ( 0) makes it OFF.
We have controlled the LED from Interpreter. Now we shall write the complete code in the EDITOR Section to blink the LED repeatedly.
Here we make use of the sleep class of time function to introduce delay.
Never ending loop is created using
while True:
Notice the loop starts by colon : & instead of braces micropython uses indentation.
Click File –> Save as
& select Micropython device
Provide the file name as main.py
You can notice , by default already boot.py is available on device.
Micropython will first execute boot.by & then looks out to execute file named main.py.
So you cannot upload file in other name to execute.If you use some other name , then the file will be executed once.After power reboot it will not execute.
Once the file is stored click on F5 or the Run green button.
The script is uploaded and the on board LED blinks.
To stop execution you can press the STOP Red button.
Now the chevrons >>> appear at the shell.
You can write new code & save it to device , again in the name main.py.
This is how to upload new code.
VIDEO :
Dear Sir,
The tutorial video very use full i just uploaded and tested my esp32 thankyou very much.Expecting more from you.
regards
Rajkumar
Thanks for the tip on Thonny. It’s a great little tool for working with EPS32’s.