This repository contains examples for the M5Stamp C3U development board.
All drivers are written in pure Micropython and are intended to be used with the generic Micropython build for ESP32-C3-USB-based boards.
Some of the modules in this repository make use of micropython.const
to optimize memory usage when deployed in pre-compiled bytecode form.
This section guides you through the steps to flash Micropython to the M5Stamp C3U board.
To enter the download mode, press and hold the center button (G9) under a power failure condition. To achieve this, either hold the center button while pressing reset and then release the center button, or disconnect the USB cable and then hold the center button while connecting the USB cable, and then release the center button.
If successful, the M5Stamp will present a USB CDC ACM device on your host.
Download the ESP32-C3 with USB Micropython port for the M5Stamp C3U.
While the device is in download mode, flash Micropython to the M5Stamp C3U as follows:
esptool.py --chip esp32c3 --port /dev/ttyACM0 --baud 460800 \
write_flash -z 0x0 esp32c3-usb-20221216-unstable-v1.19.1-774-ged58d6e4c.bin
Reset the device and you will be able to use the Micropython console via the USB CDC serial device.
This example changes the LED color each time the center button is pressed.
import machine, neopixel, random, time
button = machine.Pin(9, machine.Pin.IN, machine.Pin.PULL_UP)
led = neopixel.NeoPixel(machine.Pin(2, machine.Pin.OUT), 1)
led.fill((0, 255, 200))
led.write()
while True:
if button.value() == 0:
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
led.fill((r, g, b))
led.write()
time.sleep_ms(50)
The following example assumes a Grove connector is soldered to the board on Port A (pins G0/G1/5V/GND). It assumes the following units are all connected to the same I2C bus via the Grove Hub:
- CO2 Unit:
SCD40
(co2, temperature and humidity, I2C0x62
) - ENV III Unit:
SHT30
(temperature and humidity, I2C0x44
),QMP6988
(absolute air pressure, I2C0x70
) - DLight Unit:
BH1750FVI
(ambient light, I2C0x23
)
Earlier versions of this project used a cheaper eCO2 sensor, but it is no longer in use:
- TVOC/eCO2 Unit:
SGP30
(indoor air quality, I2C0x58
)
import machine
import uasyncio
import bh1750fvi
import sht30
import scd40
import qmp6988
i2c = machine.I2C(0, sda=machine.Pin(1), scl=machine.Pin(0), freq=400000)
async def main():
dlx = bh1750fvi.BH1750FVI(i2c)
rht = sht30.SHT30(i2c)
scd = scd40.SCD40(i2c)
prt = qmp6988.QMP6988(i2c)
print("Initializing sensors...")
await scd.start()
while True:
light = dlx.measure()
print("Ambient Light: {}lx".format(light))
temp, humidity = rht.measure()
print("Temp/Humidity: {}°C/{}%".format(temp, humidity))
temp, pressure = prt.measure()
print("Temp/Pressure: {}°C/{}Pa".format(temp, pressure))
scd.set_ambient_pressure(pressure)
co2, temp, humidity = scd.measure()
print("CO2/Temp/Humidity: {}ppm/{}°C/{}%".format(co2, temp, humidity))
await uasyncio.sleep(1)
uasyncio.run(main())
Note: To install the required Micropython drivers using
mpremote
, copy the lib/
folder to your
device as follows:
$ mpremote a0 cp -r lib :
lib/homeassist
requires polling support on SSL sockets whenuse_ssl=True
(micropython/micropython#9871)
Contributions are welcome! Please read and follow the Code of Conduct and make sure to acknowledge the Developer Certificate of Origin when contributing.