Projects

Each project is a sketch plus the modules it needs. Swap the sensor driver from libraries if your breakout is different.

Projects

Weather station

BME280 or DHT22 on I2C/1-wire, readings every minute, optional MQTT publish. Hardware: Pico W or ESP32, sensor, 3.3 V.

import time, network
from machine import I2C, Pin
# from bme280 import BME280

i2c = I2C(0, sda=Pin(4), scl=Pin(5))
# sensor = BME280(i2c=i2c)
while True:
    # print(sensor.values)
    print(i2c.scan())
    time.sleep(60)

Tiny web server

Listen on port 80 and toggle an LED from a phone on the same LAN.

import socket
from machine import Pin

led = Pin('LED', Pin.OUT)
html = b'HTTP/1.0 200 OK

<a href="/on">on</a>'

s = socket.socket()
s.bind(('0.0.0.0', 80))
s.listen(1)
while True:
    c, _ = s.accept()
    req = c.recv(256)
    if b'GET /on' in req:
        led.on()
    c.send(html)
    c.close()

Data logger

Append CSV to flash (or an SD card via machine.SDCard). Sync the RTC first if you care about timestamps after a power loss.

import time
from machine import ADC, Pin

adc = ADC(Pin(26))
with open('log.csv', 'a') as f:
    for _ in range(10):
        f.write('%d,%d
' % (time.time(), adc.read_u16()))
        time.sleep(1)

Two-wheel robot

Two PWM pins into an H-bridge (L298 / DRV8833), two GPIO for direction. Keep the motor supply off the 3.3 V rail.

from machine import Pin, PWM

class Motor:
    def __init__(self, pwm_pin, dir_pin):
        self.pwm = PWM(Pin(pwm_pin), freq=1000)
        self.dir = Pin(dir_pin, Pin.OUT)
    def drive(self, duty):  # -65535..65535
        self.dir(duty < 0)
        self.pwm.duty_u16(min(abs(duty), 65535))

left, right = Motor(16, 17), Motor(18, 19)
left.drive(20000)
right.drive(20000)

OLED clock

SSD1306 128×64 over I2C is the usual first display. The Thumby emulator already runs this chip if you want to see pixels move without wiring.

from machine import Pin, I2C, RTC
# from ssd1306 import SSD1306_I2C

i2c = I2C(0, sda=Pin(4), scl=Pin(5))
# oled = SSD1306_I2C(128, 64, i2c)
print(RTC().datetime(), i2c.scan())

Thumby demo on emulators.org.