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.
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.
-
Install Python.
-
Open terminal (typically
Ctrl+Alt+T
) and installesptool
: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:
-
Download the latest firmware for your target device, such as
esp32-20230426-v1.20.0.bin
for Espressif ESP32. -
Erase the Flash of target device (use your port name):
esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash
-
Deploy the new firmware:
esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 esp32-20230426-v1.20.0.bin
For ESP8266 chips:
-
Download the latest firmware, such as
esp8266-20230426-v1.20.0.bin
. -
Erase the Flash before deploying the firmware:
esptool.py --chip esp8266 --port /dev/ttyUSB0 erase_flash
-
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
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
, pressCtrl+A
, followed byK
andY
.
# 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
# `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:
# A platform identifier
>>> import sys
>>> sys.platform
'esp32'
# Get the current frequency of the CPU and RTC time
>>> import machine
>>> help(machine)
>>> machine.freq()
>>> machine.RTC().datetime()
# Get Flash size in Bytes
>>> import esp
>>> esp.flash_size()
# 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
- Programming in Python
- Control of GPIO pins
- Timers
- LCD (Liquid Crystal Display)
- I2C serial communication
- Wi-Fi communication
- Object-oriented programming
- Project
- Blink
- Timer blink
- Wi-Fi scan
- Wi-Fi connection
- I2C humidity & temperature sensor
- I2C sensor & ThingSpeak
- RTC & NTP times
- Wi-Fi access point
- Web server & I2C sensor
- Jupyter example
The following hardware and software components are mainly used in the lab.
Component | Link(s) |
---|---|
ESP32 microcontroler | Expressif |
FireBeetle board | Schematic & manual, pinout |
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 |
Version | Result (yyyy-mm-dd) | Note |
---|---|---|
macOS Sonoma 14.6.1 | OK (2024-09-03) | Laptop |
Windows 10 | OK (2023-09-18) | Lab SC 6.61 |
Linux Mint 20.3 (Una) | OK (2023-05-23) | Laptop |
# FYI: How to check OS version in Linux
cat /etc/os-release
# or
neofetch
# How to identify the macOS version
sw_vers
-
Peter Kazarinoff. How to install MicroPython on an ESP32 microcontroller
-
ESP32 brief overview (YouTube video)
-
MicroPython Documentation. Quick reference for the ESP32
-
DFRobot. 250+ Must-read Tutorials for Learning ESP32 and Arduino
-
40+ MicroPython Projects, Tutorial and Guides with ESP32 / ESP8266
-
Rafael Aroca. ESP32, Camera, MicroPython and NO esptool!