Pico W Sensors & I2C Cheatsheet
Pico W Sensors & I2C Cheatsheet
I2C setup, scanning, DHT22, BME280, and OLED display.
I2C Setup
| from machine import I2C, Pin | Import I2C class |
| I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) | Create I2C bus 0 at 400 kHz |
| i2c.scan() | Returns list of device addresses on the bus |
| 4.7 k?© pull-ups on SDA and SCL | Required unless your breakout board already has them |
Common Sensor Addresses
| 0x3C / 0x3D | SSD1306 OLED display (128x64) |
| 0x76 / 0x77 | BME280 temperature, pressure, humidity sensor |
| 0x40 | SHT31 / SHT40 humidity sensor |
| 0x68 | MPU6050 accelerometer and gyroscope |
DHT22
| import dht; from machine import Pin | Import DHT library (included in MicroPython) |
| dht.DHT22(Pin(15)) | Create DHT22 sensor on GPIO 15 |
| sensor.measure() | Trigger a measurement ߀” wait 2 s between calls |
| sensor.temperature() / .humidity() | Read last measured temperature (?°C) and humidity (%) |
SSD1306 OLED
| ssd1306.SSD1306_I2C(128, 64, i2c) | Create 128x64 OLED driver |
| oled.fill(0) | Clear display to black (0) or white (1) |
| oled.text("Hello", x, y) | Draw text at pixel position (x, y) |
| oled.show() | Push framebuffer to the physical display |