Skip to content

tomas-fryza/esp-micropython

Repository files navigation

MicroPython on ESP32/ESP8266 microcontollers

The repository contains MicroPython lab exercises for Erasmus course at Brno University of Technology, Czechia. The course focuses on advanced digital circuits, microprocessor technology, and use of MicroPython language for programming 32-bit microcontrollers. The course provides practical experience in the design and implementation of embedded systems, allowing students to deepen their programming skills and understanding of individual components within microprocessor systems.

firebeetle_multiple-leds

Installation

To use MicroPython with a real ESP32 board, you will need to follow these steps:

  • Download MicroPython firmware
  • Flash the firmware
  • Connect to the Board's Serial REPL and interact with MicroPython
  • Transfer files to the ESP32 board

There are several very good tutorials how to install and use MicroPython on an ESP microcontroller, such as this one for Windows. The following text was tested under Linux-based operating system.

NOTE: The MicroPython firmware can also be flashed by Thonny IDE.

  1. Install Python.

  2. Open terminal (typically Ctrl+Alt+T) and install esptool:

    pip install esptool

    Connect your ESP board and test the esptool:

    # Get the version
    esptool.py version
    
    # Read chip info, serial port, MAC address, and others
    # Note: Use `dmesg` command to find your USB port
    esptool.py --port /dev/ttyUSB0 flash_id
    
    # Read all eFuses from the chip
    espefuse.py --port /dev/ttyUSB0 summary

For ESP32 chips:

  1. Download the latest firmware for your target device, such as esp32-20230426-v1.20.0.bin for Espressif ESP32.

  2. Erase the Flash of target device (use your port name):

    esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash
  3. Deploy the new firmware:

    esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 esp32-20230426-v1.20.0.bin

For ESP8266 chips:

  1. Download the latest firmware, such as esp8266-20230426-v1.20.0.bin.

  2. Erase the Flash before deploying the firmware:

    esptool.py --chip esp8266 --port /dev/ttyUSB0 erase_flash
  3. Deploy the firmware:

    esptool.py --chip esp8266 --port /dev/ttyUSB0 write_flash --flash_mode dio --flash_size 4MB 0x0 esp8266-20230426-v1.20.0.bin

Usage

Test MicroPython via PuTTY or directly in terminal by screen. You need to press on-board reset button:

screen /dev/ttyUSB0 115200

Note: To exit the screen, press Ctrl+A, followed by K and Y.

# Print string to a Shell
>>> print("Hi there!")
Hi there!

# Operators used for the different functions like division,
# multiplication, addition, subtraction, ...
>>> 10/3
3.333333
>>> 10//3
3
>>> 10%3
1
>>> 10*3
30
>>> 10**3
1000

# Integers, floats, strings
>>> type(10)
<class 'int'>
>>> type(10.0)
<class 'float'>

>>> pi = 3.1415
>>> pi_str = str(pi)
>>> type(pi_str)
<class 'str'>
>>> len(pi_str)
6

# Convert numbers
>>> x = 65
>>> bin(x)  # to binary representation
'0b1000001'
>>> hex(x)  # to hexadecimal
'0x41'
>>> chr(x)  # to unicode string
'A'
>>> ord("a")  # to unicode code
97

# `ord` returns unicode code of a specified character
>>> ord("A")
65
>>> ord("a")
97
>>> ord("0")
48

>>> print(pi_str)
3.1415
>>> ord(pi_str[0])
51
>>> ord(pi_str[-1])
53

See MicroPython tutorials, such as MicroPython Programming Basics with ESP32 and ESP8266 for detailed explanation.

Test some other useful commands from Quick reference for the ESP32:

# List built-in libraries
# https://docs.micropython.org/en/latest/library/index.html
>>> help("modules")

# A platform identifier
>>> import sys
>>> sys.platform
'esp32'

# Get the current frequency of the CPU and RTC time
>>> import machine
>>> help(machine)
>>> freq = machine.freq()
>>> print(f"Machine clock frequency: {freq/1000000:,} MHz")
>>> machine.RTC().datetime()

# Get Flash size in Bytes
>>> import esp
>>> esp.flash_size()
>>> print(f"Flash size: {esp.flash_size()/1000:,} kB")

# Read the internal temperature (in Fahrenheit)
>>> import esp32
>>> esp32.raw_temperature()
# FYI: temp_c = (temp_f-32) * (5/9)
#      temp_f = temp_c * (9/5) + 32

Tools

Wokwi simulator

Wokwi is a web-based platform for simulating and visualizing electronics projects right in your web browser. You can use it to simulate Arduino, ESP32, STM32, and many other popular boards, parts and sensors in C, MicroPython or Rust language.

wokwi_blink

ViperIDE

ViperIDE is a lightweight MicroPython / CircuitPython IDE for Web and Mobile. It provides a clean interface with essential features like a serial console, code uploading, and direct interaction with the MicroPython REPL. Viper IDE allows easy management of files on the microcontroller, real-time code execution, and debugging.

Thonny IDE

Thonny is an integrated development environment (IDE) designed primarily for Python programming. It provides a user-friendly and beginner-friendly environment for writing, running, and debugging Python code. It can also be used with MicroPython for programming microcontrollers like the ESP8266, ESP32, Raspberry Pi Pico, etc. Thonny is available for multiple platforms, including Windows, macOS, and Linux.

Mu Editor

Mu is another simple Python editor with built-in support for MicroPython. It has an intuitive user interface with easy-to-access buttons for uploading code and interacting with the board. Mu is designed for beginners and education, with features like a REPL, plotter, and simple code running on devices like the BBC micro and ESP32.

Visual Studio Code with Pymakr extension

Pymakr allows you to connect, upload, and run MicroPython code on boards like the ESP32. It includes features like auto-completion, real-time terminal access to the board, and a file manager.

Labs

  1. Programming in Python
  2. Control of GPIO pins
  3. Object-oriented programming
  4. Timers
  5. LCD (Liquid Crystal Display)
  6. I2C serial communication
  7. Wi-Fi communication
  8. Project

Note: The documentation for the modules used in the labs, generated by Sphinx, is available here.

Components and tools

The following hardware and software components are mainly used in the lab.

firebeetle_pinout

Component Link(s)
ESP32 microcontroler Expressif
FireBeetle board Schematic & manual
DHT12 I2C humidity and temperature sensor: data sheet
MPU6050 I2C gyroscope and accelerometer: data sheet
DS3231 I2C real time clock: data sheet
HC-SR04 ultrasonic sensor: datasheet
Joystick PS2 Analog joystick PS2
Logic analyzer 24MHz 8-channel logic analyzer: software
Osciloscope DSOX3034T Oscilloscope Keysight Technologies DSOX3034T (350 MHz, 4 analog channels), including 16 logic timing channels DSOXT3MSO and serial protocol triggering and decode options D3000BDLA
Thonny IDE Python IDE for beginners
Mu editor Python editor
Visual Studio Code web page
Git git

Tested on

Version Result (yyyy-mm-dd) Note
macOS Sonoma 14.6.1 OK (2024-09-03) MacBook
Windows 10 OK (2023-09-18) Lab SC 6.61
Linux Mint 20.3 (Una) OK (2023-05-23) Laptop

FYI: How to identify the version of the operating system from the command line.

# Linux:
cat /etc/os-release
# or
neofetch

# macOS:
sw_vers

# Windows:
WINVER

Links

License

Licensed under the MIT License.