MicroPython with esp8266 and esp32: flashing firmware and using PyCharm IDE – 3

Spread the love

If you’re looking to start programming microcontrollers using Python, then MicroPython with esp8266 and esp32 is an excellent place to begin. With MicroPython, you can use Python to program microcontrollers and control devices like sensors, motors, and lights.

MicroPython with esp8266 and esp32: flashing firmware and using PyCharm IDE
MicroPython with esp8266 and esp32: flashing firmware and using PyCharm IDE

One of the easiest ways to get started with MicroPython on esp8266 and esp32 is to use PyCharm as your integrated development environment (IDE). In this article, we’ll take a look at how to set up PyCharm for MicroPython development, and we’ll create a simple project to blink an LED.

ESP32 details

I like these devices. It’s very powerful and low cost with an effective value WIFI microcontroller.

You can find It here ESP32 Dev Kit v1 - Selectable - TTGO T-Display 1.14 ESP32 - NodeMCU V3 V2 ESP8266 Lolin32 - NodeMCU ESP-32S - WeMos Lolin32 CP2104 CH340 - ESP32-CAM programmer - ESP32-CAM bundle - ESP32-WROOM-32 - ESP32-S

Specs

Remember that exist a lot of variants of this device. Some have a specified killer function, so this is a generic specs description.

Function Block diagram Espressif esp32 Wi-Fi Bluetooth Microcontroller
Function Block diagram Espressif esp32 Wi-Fi Bluetooth Microcontroller
  • Processors:
    • CPU: Xtensa dual-core (or single-core) 32-bit LX6 microprocessor, operating at 160 or 240 MHz and performing at up to 600 DMIPS
    • Ultra low power (ULP) co-processor
  • Memory: 520 KiB SRAM
  • Wireless connectivity:
    • Wi-Fi: 802.11 b/g/n
    • Bluetooth: v4.2 BR/EDR and BLE (shares the radio with Wi-Fi)
  • Peripheral interfaces:
  • Security:
  • Power management:
    • Internal low-dropout regulator
    • Individual power domain for RTC
    • 5μA deep sleep current
    • Wake up from GPIO interrupt, timer, ADC measurements, capacitive touch sensor interrupt

Pinouts

ESP32 DEV KIT v1

ESP32 DevKIT v4

Esp32 dev kit v4 pinout
Esp32 dev kit v4 pinout

ESP32 WeMos LOLIN32

This device is very powerful, and you can see that there are capacitive touch pins, more than one UART various Analog pins.

How to

You must download drivers for the USB chip, probably you have an esp32 version with cp2102, but exist a variant with ch340 chip.

ESP8266 details

You can find It here WeMos D1 mini - NodeMCU V2 V2.1 V3 - esp01 - esp01 programmer

Specs

Pinouts

Wemos D1 mini

WeMos D1 mini esp8266 pinout mischianti low resolution
WeMos D1 mini esp8266 pinout mischianti low resolution

WeMos D1 mini high-resolution image

NodeMCU V2

NodeMcu v2 pinout low resolution
NodeMcu v2 pinout low resolution

NodeMCU v2.x high-resolution image

NodeMCU v2.1

NodeMcu v2.1 low resolution pinout
NodeMcu v2.1 low resolution pinout

NodeMCU v2.x high-resolution image

NodeMCU v3

NodeMcu v3 low resolution pinout
NodeMcu v3 low resolution pinout

I think that the interesting thing is that It has more than one Hardware Serial, so you can use Serial for communication with devices and Serial1 D4 (only Transmission) to debug.

Drivers

You can download drivers for the USB chips here, check your version of D1 mini and click on drivers.

Here is a more exhaustive list of devices.

You need python

First of all, you need to install Python and download It from here.

And remember to add It to the PATH.

Python installation on windows: add to PATH
Python installation on windows: add to PATH

Flashing MicroPython firmware on the board

To install MicroPython on your ESP8266 or ESP32 board, you will need to download the firmware from the official MicroPython website. The firmware is available in binary form for a variety of microcontrollers, including the ESP8266 and ESP32.

  1. Download the firmware
  1. Install the esptool.py tool, which is a command-line utility that can be used to flash the MicroPython firmware onto the board. The tool can be installed using pip, a Python package manager.
    • For Windows users:
      • Open Command Prompt
      • Install pip by running the following command (you probably don’t need this step): python -m ensurepip
      • Install esptool.py by running the following command: pip install esptool
    • For Mac or Linux users:
      • Open Terminal
      • Install pip by running the following command: sudo easy_install pip
      • Install esptool.py by running the following command: sudo pip install esptool
C:\Users\renzo>pip install esptool
Defaulting to user installation because normal site-packages is not writeable
Collecting esptool
  Downloading esptool-4.5.1.tar.gz (252 kB)
     |████████████████████████████████| 252 kB 2.2 MB/s
Collecting bitstring>=3.1.6
  Downloading bitstring-4.0.1-py3-none-any.whl (45 kB)
     |████████████████████████████████| 45 kB 3.2 MB/s
Collecting cryptography>=2.1.4
  Downloading cryptography-39.0.2-cp36-abi3-win_amd64.whl (2.5 MB)
     |████████████████████████████████| 2.5 MB 2.2 MB/s
Collecting ecdsa>=0.16.0
  Downloading ecdsa-0.18.0-py2.py3-none-any.whl (142 kB)
     |████████████████████████████████| 142 kB 2.2 MB/s
Requirement already satisfied: pyserial>=3.0 in c:\users\renzo\appdata\roaming\python\python39\site-packages (from esptool) (3.0.1)
Collecting reedsolo<=1.6.0,>=1.5.3
  Downloading reedsolo-1.6.0.tar.gz (278 kB)
     |████████████████████████████████| 278 kB 2.2 MB/s
Collecting cffi>=1.12
  Downloading cffi-1.15.1-cp39-cp39-win_amd64.whl (179 kB)
     |████████████████████████████████| 179 kB 2.2 MB/s
Collecting pycparser
  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
     |████████████████████████████████| 118 kB 2.2 MB/s
Collecting six>=1.9.0
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Using legacy 'setup.py install' for esptool, since package 'wheel' is not installed.
Using legacy 'setup.py install' for reedsolo, since package 'wheel' is not installed.
Installing collected packages: pycparser, six, cffi, reedsolo, ecdsa, cryptography, bitstring, esptool
    Running setup.py install for reedsolo ... done
    Running setup.py install for esptool ... done
Successfully installed bitstring-4.0.1 cffi-1.15.1 cryptography-39.0.2 ecdsa-0.18.0 esptool-4.5.1 pycparser-2.21 reedsolo-1.6.0 six-1.16.0

If you add the Script folder to the PATH variable, you can directly call the esptool.py.

Add Script folder to the PATH of windows
Add Script folder to the PATH of windows
  1. Connect the board to your computer via USB.
    To find the new Serial COM port, go to Device Manager from windows menù and check which port is born.
Device Manager find the Serial COM port
Device Manager find the Serial COM port
  • Search in the windows menu “Edit the system environment variables“;
  • In the panel click Environment variables....
  • In user variables, select “Path
  • And add the Script folder.
  1. Erase the flash memory of the board by running the following command:
    For ESP8266 and esp32:
esptool.py --port COM5 erase_flash

If you don’t have esptool.py in path use this command

python -m esptool --port COM5 erase_flash

Replace COM5 with the correct serial port of your board.

D:\projects\micropython\testesp>python -m esptool --port COM5 erase_flash
esptool.py v4.5.1
Serial port COM5
Connecting....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 50:02:91:74:fd:2f
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 8.6s
Hard resetting via RTS pin...
  1. Flash the MicroPython firmware onto the board by running the following command:

For ESP8266:

esptool.py --port COM5 --baud 460800 write_flash --flash_size=detect 0 <firmware.bin>

or

python -m esptool --port COM5 --baud 460800 write_flash --flash_size=detect 0 <firmware.bin>

For ESP32:

esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 <firmware.bin>

or

python -m esptool --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 <firmware.bin>

Replace COM5 with the correct serial port of your board and <firmware.bin> with the path to the MicroPython firmware file that you downloaded earlier.

D:\projects\micropython\testesp>python -m esptool --port COM5 --baud 460800 write_flash --flash_size=detect 0 esp8266-20220618-v1.19.1.bin
esptool.py v4.5.1
Serial port COM5
Connecting....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 50:02:91:74:fd:2f
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash will be erased from 0x00000000 to 0x0009afff...
Flash params set to 0x0040
Compressed 634844 bytes to 419808...
Wrote 634844 bytes (419808 compressed) at 0x00000000 in 10.0 seconds (effective 509.5 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
  1. Once the firmware has been flashed onto the board, you can connect to the board using a serial terminal program, such as PuTTY (Windows) or screen (Mac/Linux). The baud rate should be set to 115200.

Starting with PyCharm

Put on work PyCharm is quite simple, here the settings process.

Install the IDE

Download the PyCharm Community edition from the JetBrains website.

PyCharm download page
PyCharm download page

After that, start the installation wizard.

Installing PyCharm for MicroPython
Installing PyCharm for MicroPython

Now start the program.

PyCharm splash screen
PyCharm splash screen

Install and set MicroPython plugin

On the Welcome page,

  1. select the menu item Plugin;
  2. search MicroPython;
  3. and install It.

PyCharm MicroPython plugin
PyCharm MicroPython plugin

Now restart the IDE.

At this time, select the Project menu item, and the New Project button.

PyCharm first project MicroPython select venv e python
PyCharm first project MicroPython select venv e python

In the Location select or write the folder name of the project (I write pythonProject).

Now you must enable MicroPython support, to do that:

  1. Click on File --> Settings;
  2. Now go to Language & Framework --> MicroPython;
  3. Check Enable MicroPython support.
PyCharm abilita il supporto di MicroPython
PyCharm abilita il supporto di MicroPython

Now select the correct microcontroller framework, for ESP32 and esp8266 is ESP8266 item.

PyCharm MicroPython: select microcontroller esp8266 esp32 raspberry
PyCharm MicroPython: select microcontroller esp8266 esp32 raspberry

Now you must add the correct Serial COM port, and you can get It as described in the previous part of this article. Write It in the Device path.

PyCharm MicroPython: set the correct Serial COM port
PyCharm MicroPython: set the correct Serial COM port

Your first print sketch

When you close the modal, create a python file main.py and add this line of code.

print("Hello from www.mischianti.org")
PyCharm first MicroPython print example: no dependencies
PyCharm first MicroPython print example: no dependencies

As you can see, the IDE alerts you that some dependencies are missing. You must simply click Missing required MicroPython packages, and the IDE fixes It for you.

Now the project It’s ready, to start the program you must only click with the right button of the mouse over the py file and click on the link Run "Flash main.py".

PyCharm first MicroPython print example
PyCharm first MicroPython print example

The IDE creates a starter in the up right part of the IDE bar, and the program start.

Pycharm MicroPython: edit start script
Pycharm MicroPython: edit start script

The following times you must only click on the green arrow to the right of the starter.

The result in the Run console is:

Connecting to COM5
Uploading files: 0% (0/1)
C:\Users\renzo\PycharmProjects\pythonProject\main.py -> main.py
Uploading files: 100% (1/1)
Soft reboot
PyCharm MicroPython: show print in REPL
PyCharm MicroPython: show print in REPL

Now if you open the MicroPython tab to the bottom of the IDE, click on the restart arrow on the left of the MicroPython (REPL) console, and reset the device, you get this:

                                              ��ll�
Hello from www.mischianti.org                             
                                                          
MicroPython v1.19.1 on 2022-06-18; ESP module with ESP8266
Type "help()" for more information.                       
>>> 

Interactive REPL console

As you can see, you can use the REPL (Read-Eval-Print Loop) to interact with the board and run Python code. The REPL is a simple way to enter Python commands and see their output in real-time. You can connect to the board’s REPL using a serial terminal program, like PuTTY or screen, over a USB connection.

So if you are going to write this code on the REPL console:

print("Hello from www.mischianti.org! directly from console")
PyCharm MicroPython: writes code in the console REPL
PyCharm MicroPython: writes code in the console REPL

You receive back these lines:

Hello from www.mischianti.org! directly from console
>>>    

Blink and print sketch

Now we are going to write a more complex sketch that interacts with the device.

Take this code:

import machine
import time
pin = machine.Pin(2, machine.Pin.OUT)
while True:
    pin.value(0)
    print("LED ON");
    time.sleep(0.5)
    pin.value(1)
    print("LED OFF");
    time.sleep(0.5)

The code first imports two modules: machine and time. The machine module provides access to the hardware peripherals of the microcontroller, while the time module provides functions for working with time and delays.

Next, the code sets up a GPIO pin on the microcontroller (in this case, pin 2) as an output pin using the machine.Pin() method with the machine.Pin.OUT argument.

The code then enters a loop that will run indefinitely (since the condition True is always true). Within the loop, the code first sets the pin to 0, which turns the LED connected to it on. It then prints LED ON to the console using the print() function. The code then waits for half a second using the time.sleep() function.

Pycharm MicroPython: blink sketch with print
Pycharm MicroPython: blink sketch with print

Next, the code sets the pin to 1, which turns the LED off, and prints LED OFF to the console. It then waits for another half a second before repeating the loop.

Overall, this code will turn an LED on and off in a repeating pattern with a 0.5 second delay between each state change.

If you have some problem using machine import, follow the guide to fix It in the previous articles about MicroPython.

Probably at the start, you get:

Quit: Ctrl+] | Stop program: Ctrl+C | Reset: Ctrl+D 
Type 'help()' (without the quotes) then press ENTER.
LED ON
Traceback (most recent call last):                        
  File "main.py", line 7, in <module>                     
KeyboardInterrupt:                                        
MicroPython v1.19.1 on 2022-06-18; ESP module with ESP8266
Type "help()" for more information.                       
>>> 

But if you reset the device, the program starts correctly:

LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
WeMos d1 mini (esp8266): led light
WeMos d1 mini (esp8266): led light

Thanks

Mastering esp8266 programming with MicroPython

  1. MicroPython with esp8266 and esp32: flashing firmware and using standard tools
  2. MicroPython with esp8266 and esp32: flashing firmware and using Thonny IDE
  3. MicroPython with esp8266 and esp32: flashing firmware and using PyCharm IDE

Mastering ESP32 programming with MicroPython

  1. MicroPython with esp8266 and esp32: flashing firmware and using standard tools
  2. MicroPython with esp8266 and esp32: flashing firmware and using Thonny IDE
  3. MicroPython with esp8266 and esp32: flashing firmware and using PyCharm IDE


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *