micropython-samples
The first part of this repo comprises assorted code ideas for MicroPython and
also serves as an unofficial MicroPython FAQ. Some samples are targeted at
Pyboard variants. Some are intended as pointers for programmers rather than
being complete solutions. Egregious bugs will be fixed but I may not accept
feature requests.
Section 5 is an index to complete applications
and modules which are documented and supported.
Please also see the official examples.
0. Index
- Installation guides
1.1 Installing MicroPython libraries
1.2 Fastbuild Build scripts and udev rules
1.3 A web framework Microdot.
1.4 Buildcheck Check firmware build date
1.5 A USB pitfall A problem with platforms which share their filesystem with the PC.
- Hardware information and drivers
2.1 ESP32 Pinout and notes on the reference board
2.2 SSD1306 Write large fonts to the SSD1306.
2.3 Pyboard D Some information remains absent or hard to find in the docs.
2.4 DS3231 MAX31328 precision RTC Use cheap hardware to calibrate Pyboard RTC.
- Essays General thoughts.
3.1 Resilient A guide to writing resilient WiFi code
3.2 Serialisation Review of MicroPython's five serialisation libraries
3.3 Measurement of relative timing and phase of fast analog signals For Pyboard.
3.4 Import subtleties Ways to save RAM with import statements.
3.5 SSL/TLS tutorial (This excellent resource is not my work.)
- Code samples Samples prefixed Pyboard are Pyboard specific
4.1 Pyboard Mutex Share data between threads and ISR's.
4.2 Pyboard watchdog Access a Pyboard hardware WDT.
4.3 Software Watchdog Cross-platform soft WDT.
4.4 Reverse Reversal algorithms for bytearrays.
4.5 Timed function Time execution with a decorator.
4.6 ESP8266 MQTT benchmark Test performance of MQTT with official library.
4.7 Rotary incremental encoder Fast, simple, proven algorithm.
4.8 Pseudo random number generators
4.9 Verifying incrementing sequences Test communications drivers.
4.10 Bitmaps Non-allocating ways to access bitmaps.
4.11 Functors and singletons Useful decorators.
4.12 Quaternions Scale, move and rotate 3D objects with minimal mathematics.
4.13 A Pyboard power meter One of my own projects.
4.14 NTP time More portable than official driver with other benefits.
4.15 Date Small and simple classes for handling dates.
4.16 Greatest common divisor Neat algorithm.
4.17 2D array indexing Use [1:3, 20] syntax to address a 2D array.
4.18 Astronomy Derive Sun and Moon rise and set times, moon phase.
4.19 Tone detection Goertzel algorithm.
4.20 Using RP2XXX hardware Introduction to PIO, DMA and dual-core code.
- Module Index Index to my fully supported modules. Device drivers, GUI's, utilities.
5.1 asyncio Tutorial and drivers for asynchronous coding.
5.2 Memory Device Drivers Drivers for nonvolatile memory devices.
5.3 Inertial Measurement Units Gravity, gyro and magnetic sensors.
5.4 Other hardware drivers
5.5.Communications
5.6 Displays Fonts, graphics, GUIs and display drivers
5.7 Pyboard micropower
5.8 Pyboard DSP Fourier transforms and filters.
5.9 rshell Fork of rshell with text macros.
5.10 Hard to categorise Other modules.
1. Installation guides
1.1 Installing MicroPython libraries
Please read
the official docs.
This doc describes a simple way to override modules
which have been incorporated in firmware as frozen bytecode.
1.2 Fastbuild
Scripts for building MicroPython for various target hardware types and for
updating your local source. Now detects and builds for Pyboard D. See
docs
1.3 A Web Framework
Paul Sokolovsk's PicoWeb requires his
fork of MicroPython and requires some modification to run under official
MicroPython.
The solution preferred by MicroPython maintainers is
Microdot.
1.4 Buildcheck
Raise an exception if a firmware build is earlier
than a given date.
1.5 A USB pitfall
By default the Pyboard's /flash/boot.py enables MSC (mass storage) mode. This
makes the Pyboard look like a USB stick, making its filesystem visible to the
PC. This helpful feature ignores a fundamental flaw which leads to filesystem
corruption. This is because the USB standard requires mass storage devices to
behave like disks with static content. By contrast a Pyboard can independently
modify the "disk" contents causing chaos.
To fix this, edit /flash/boot.py so that the usb_mode line reads:
pyb.usb_mode('VCP')
On reboot the Pyboard will no longer appear as a mass storage device on the PC.
Various tools are available to manage the device's storage via USB. I use
rshell.
2. Hardware information and drivers
2.1 ESP32
Pinout diagram for the reference board with notes and warnings about reserved
pins etc. See this doc. See also this
excellent resource.
2.2 SSD1306
A means of rendering multiple larger fonts to the SSD1306 OLED display. The
Writer class which performs this has been substantially improved and may now
be found as part of this repository.
2.3 Pyboard D
Assorted information not yet in the official docs or
hard to find.
2.4 DS3231 MAX31328 precision RTC
This is a low cost precision battery backed real time clock (RTC) accurate to
+-2 minutes/year. Two drivers are provided, one portable across platforms and
one which is Pyboard specific.
This driver also supports the MAX31328 which is a 100% compatible chip from the
same manufacturer.
The Pyboard-specific driver provides a facility to calibrate the Pyboard's RTC
from the DS3231. Calibration to high precision may be achieved in five minutes.
The drivers are documented here.