micropython-skills/sensor

MicroPython sensor reading — DHT11/22, BME280, MPU6050, ADC, ultrasonic HC-SR04, photoresistor, generic I2C sensors.

3,891 stars
Complexity: easy

About this skill

This AI agent skill offers ready-to-use Python code snippets for interfacing with a wide array of sensors on MicroPython-enabled microcontrollers. It supports popular environmental sensors like DHT11/22 (temperature/humidity) and BME280 (temperature/pressure/humidity), motion sensors like MPU6050, as well as generic analog-to-digital converters (ADC), ultrasonic distance sensors (HC-SR04), and photoresistors. Each snippet includes basic error handling and outputs results in a structured JSON format, making it easy for an agent to parse and use the data. Developers and hobbyists working on IoT projects or embedded systems can leverage this skill to quickly integrate sensor data into their applications without needing to write sensor-specific driver code from scratch. It's particularly useful for prototyping, educational purposes, or situations where rapid deployment of sensor-based functionalities is required. The "Safe tier" designation means these operations can be executed directly by the agent without constant user confirmations. By abstracting the low-level sensor communication, this skill allows an AI agent to efficiently retrieve real-world data from physical environments. It accelerates the development process for MicroPython projects, enabling agents to build more interactive and context-aware applications by easily collecting environmental readings, motion data, or proximity information directly from connected hardware.

Best use case

The primary use case is enabling AI agents to interact with physical hardware by reading data from various sensors connected to MicroPython devices. This benefits embedded systems developers, IoT engineers, and hobbyists who need to quickly prototype or deploy applications that monitor environmental conditions, detect motion, measure distances, or gather other physical data. It streamlines the process of integrating real-world input into AI-driven projects, allowing for smarter automation and data collection.

MicroPython sensor reading — DHT11/22, BME280, MPU6050, ADC, ultrasonic HC-SR04, photoresistor, generic I2C sensors.

The user should expect structured JSON output containing sensor readings (e.g., temperature, humidity, pressure, distance) from the specified MicroPython-connected hardware.

Practical example

Example input

Read the temperature and humidity from the DHT22 sensor connected to GPIO pin 4 on my MicroPython board.

Example output

RESULT:{"temperature_c": 24.5, "humidity_pct": 55.2}

When to use this skill

  • When an AI agent needs to read temperature, humidity, or pressure data from DHT or BME280 sensors on a MicroPython board.
  • When prototyping IoT applications requiring input from common sensors like MPU6050, HC-SR04, or photoresistors on MicroPython.
  • When an AI needs to get real-world environmental or motion data from a connected microcontroller.
  • When quickly integrating generic I2C sensors into a MicroPython project and receiving structured JSON output.

When not to use this skill

  • When the target device is not running MicroPython (e.g., Arduino, Raspberry Pi OS, ESP-IDF C++).
  • When highly optimized, real-time critical sensor reading with custom drivers is required beyond the provided templates.
  • When sensors are connected via protocols not explicitly covered (e.g., SPI, 1-Wire besides DHT).
  • When an agent cannot execute Python code directly or interact with an external MicroPython board.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/sensor/SKILL.md --create-dirs "https://raw.githubusercontent.com/openclaw/skills/main/skills/0x1abin/micropython-skills/skills/sensor/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/sensor/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How micropython-skills/sensor Compares

Feature / Agentmicropython-skills/sensorStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityeasyN/A

Frequently Asked Questions

What does this skill do?

MicroPython sensor reading — DHT11/22, BME280, MPU6050, ADC, ultrasonic HC-SR04, photoresistor, generic I2C sensors.

How difficult is it to install?

The installation complexity is rated as easy. You can find the installation instructions above.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

# Sensor Reading

Code templates for reading common sensors on MicroPython devices.
All sensor reads are **Safe tier** — execute directly without user confirmation.

Always adapt pin numbers to the target board. When unsure, ask the user which GPIO pins are connected.

## DHT11 / DHT22 (Temperature & Humidity)

```python
import dht, json
from machine import Pin
try:
    # DHT22 is more accurate; use dht.DHT11 for DHT11
    sensor = dht.DHT22(Pin(4))  # Adapt GPIO pin
    sensor.measure()
    print("RESULT:" + json.dumps({
        "temperature_c": sensor.temperature(),
        "humidity_pct": sensor.humidity(),
    }))
except Exception as e:
    print("ERROR:" + str(e))
```

Notes:
- DHT22 range: -40~80C, 0-100% RH, accuracy +-0.5C
- DHT11 range: 0~50C, 20-80% RH, accuracy +-2C
- Minimum 2s between reads for DHT22, 1s for DHT11
- Needs a 4.7k-10k pull-up resistor on data pin (many modules have this built-in)

## BME280 (Temperature, Pressure, Humidity)

I2C sensor, common addresses: 0x76 or 0x77.

```python
from machine import Pin, I2C
import json
try:
    i2c = I2C(0, sda=Pin(21), scl=Pin(22), freq=100000)
    addrs = i2c.scan()
    bme_addr = 0x76 if 0x76 in addrs else (0x77 if 0x77 in addrs else None)
    if not bme_addr:
        print("ERROR:BME280 not found. I2C scan: " + str(["0x{:02x}".format(a) for a in addrs]))
        raise SystemExit

    # Read calibration and raw data — simplified using BME280 register map
    # For production use, recommend uploading a bme280 library via mpremote fs cp
    # Quick raw read of chip ID to verify:
    chip_id = i2c.readfrom_mem(bme_addr, 0xD0, 1)[0]
    if chip_id == 0x60:
        sensor_type = "BME280"
    elif chip_id == 0x58:
        sensor_type = "BMP280 (no humidity)"
    else:
        sensor_type = "Unknown (chip_id=0x{:02x})".format(chip_id)

    print("RESULT:" + json.dumps({
        "sensor": sensor_type,
        "address": "0x{:02x}".format(bme_addr),
        "note": "Upload bme280.py library for full T/P/H reading: mpremote fs cp bme280.py :",
    }))
except Exception as e:
    print("ERROR:" + str(e))
```

With library uploaded (`mpremote mip install bme280` or manual upload):
```python
import bme280, json
from machine import Pin, I2C
try:
    i2c = I2C(0, sda=Pin(21), scl=Pin(22))
    sensor = bme280.BME280(i2c=i2c)
    t, p, h = sensor.values  # Returns strings like "23.45C", "1013.25hPa", "48.7%"
    print("RESULT:" + json.dumps({"temperature": t, "pressure": p, "humidity": h}))
except Exception as e:
    print("ERROR:" + str(e))
```

## MPU6050 (Accelerometer + Gyroscope)

I2C address: 0x68 (or 0x69 with AD0 high).

```python
from machine import Pin, I2C
import json, struct
try:
    i2c = I2C(0, sda=Pin(21), scl=Pin(22), freq=400000)
    addr = 0x68

    # Wake up MPU6050 (power management register)
    i2c.writeto_mem(addr, 0x6B, b'\x00')

    # Read 14 bytes starting at 0x3B (accel + temp + gyro)
    data = i2c.readfrom_mem(addr, 0x3B, 14)
    ax, ay, az, temp_raw, gx, gy, gz = struct.unpack(">hhhhhhh", data)

    # Convert: accel in g (default +-2g range), gyro in deg/s (default +-250)
    print("RESULT:" + json.dumps({
        "accel_g": {"x": ax/16384, "y": ay/16384, "z": az/16384},
        "gyro_dps": {"x": gx/131, "y": gy/131, "z": gz/131},
        "temp_c": temp_raw/340 + 36.53,
    }))
except Exception as e:
    print("ERROR:" + str(e))
```

## ADC (Analog-to-Digital Converter)

```python
from machine import ADC, Pin
import json
try:
    adc = ADC(Pin(34))  # ESP32 ADC1 pins: 32-39
    # ESP32 attenuation settings:
    # ADC.ATTN_0DB: 0-1.1V (most accurate)
    # ADC.ATTN_6DB: 0-1.5V
    # ADC.ATTN_11DB: 0-3.3V (full range)
    adc.atten(ADC.ATTN_11DB)
    adc.width(ADC.WIDTH_12BIT)  # 0-4095

    raw = adc.read()
    voltage = raw / 4095 * 3.3  # Approximate voltage

    print("RESULT:" + json.dumps({
        "raw": raw,
        "voltage_v": round(voltage, 3),
        "attenuation": "11dB (0-3.3V)",
    }))
except Exception as e:
    print("ERROR:" + str(e))
```

Notes:
- ESP32 ADC is non-linear; for precision, use calibration lookup or external ADC
- ADC2 pins (GPIO 0,2,4,12-15,25-27) cannot be used while WiFi is active
- RP2040: use `ADC(26)`, `ADC(27)`, `ADC(28)` — 12-bit, 0-3.3V range

## HC-SR04 Ultrasonic Distance

```python
from machine import Pin, time_pulse_us
import time, json
try:
    trigger = Pin(5, Pin.OUT)
    echo = Pin(18, Pin.IN)

    trigger.value(0)
    time.sleep_us(2)
    trigger.value(1)
    time.sleep_us(10)
    trigger.value(0)

    duration = time_pulse_us(echo, 1, 30000)  # timeout 30ms (~5m max)
    if duration < 0:
        print("ERROR:No echo received (object too far or not connected)")
    else:
        distance_cm = duration * 0.0343 / 2
        print("RESULT:" + json.dumps({
            "distance_cm": round(distance_cm, 1),
            "duration_us": duration,
        }))
except Exception as e:
    print("ERROR:" + str(e))
```

## Photoresistor (LDR)

Light level via ADC — brighter light = lower resistance = higher ADC value (with pull-down) or lower (with pull-up voltage divider):

```python
from machine import ADC, Pin
import json
try:
    adc = ADC(Pin(34))
    adc.atten(ADC.ATTN_11DB)
    raw = adc.read()
    # Map to rough percentage (depends on voltage divider circuit)
    light_pct = round(raw / 4095 * 100, 1)
    print("RESULT:" + json.dumps({"raw": raw, "light_pct": light_pct}))
except Exception as e:
    print("ERROR:" + str(e))
```

## Generic I2C Sensor Read

Template for reading arbitrary I2C registers:

```python
from machine import Pin, I2C
import json
try:
    i2c = I2C(0, sda=Pin(21), scl=Pin(22), freq=100000)
    addr = 0x48  # Target device address
    reg = 0x00   # Register to read
    nbytes = 2   # Number of bytes to read

    data = i2c.readfrom_mem(addr, reg, nbytes)
    print("RESULT:" + json.dumps({
        "address": "0x{:02x}".format(addr),
        "register": "0x{:02x}".format(reg),
        "data_hex": " ".join("0x{:02x}".format(b) for b in data),
        "data_bytes": list(data),
    }))
except Exception as e:
    print("ERROR:" + str(e))
```

Related Skills

micropython-skills/network

3891
from openclaw/skills

MicroPython networking — WiFi STA/AP, HTTP requests, MQTT pub/sub, BLE, NTP time sync, WebSocket.

Coding & Development

micropython-skills/algorithm

3891
from openclaw/skills

MicroPython on-device algorithms — PID controller, moving average, Kalman filter, state machine, task scheduler, data logger.

Coding & Development

Go Production Engineering

3891
from openclaw/skills

You are a Go production engineering expert. Follow this system for every Go project — from architecture decisions through production deployment. Apply phases sequentially for new projects; use individual phases as needed for existing codebases.

Coding & Development

Database Engineering Mastery

3891
from openclaw/skills

> Complete database design, optimization, migration, and operations system. From schema design to production monitoring — covers PostgreSQL, MySQL, SQLite, and general SQL patterns.

Coding & Development

afrexai-code-reviewer

3891
from openclaw/skills

Enterprise-grade code review agent. Reviews PRs, diffs, or code files for security vulnerabilities, performance issues, error handling gaps, architecture smells, and test coverage. Works with any language, any repo, no dependencies required.

Coding & Development

API Documentation Generator

3891
from openclaw/skills

Generate production-ready API documentation from endpoint descriptions. Outputs OpenAPI 3.0, markdown reference docs, and SDK quickstart guides.

Coding & Development

bili-rs

3891
from openclaw/skills

Development skill for bili-rs, a Rust CLI tool for Bilibili (B站). Use when implementing features, fixing bugs, or extending the bilibili-cli-rust codebase. Provides architecture conventions, API endpoints, coding patterns, and project-specific constraints. Triggers on tasks involving adding CLI commands, calling Bilibili APIs, handling authentication, implementing output formatting, or working with the layered cli/commands/client/payloads architecture.

Coding & Development

Puppeteer

3891
from openclaw/skills

Automate Chrome and Chromium with Puppeteer for scraping, testing, screenshots, and browser workflows.

Coding & Development

pharaoh

3891
from openclaw/skills

Codebase knowledge graph with 23 development workflow skills. Query architecture, dependencies, blast radius, dead code, and test coverage via MCP. Requires GitHub App installation (read-only repo access) and OAuth authentication. Connects to external MCP server at mcp.pharaoh.so.

Coding & Development

git-commit-helper

3891
from openclaw/skills

Generate standardized git commit messages following Conventional Commits format. Use this skill when the user asks to commit code, write a commit message, or create a git commit. Enforces team conventions for type prefixes, scope naming, message length, and breaking change documentation.

Coding & Development

ask-claude

3891
from openclaw/skills

Delegate a task to Claude Code CLI and immediately report the result back in chat. Supports persistent sessions with full context memory. Safe execution: no data exfiltration, no external calls, file operations confined to workspace. Use when the user asks to run Claude, delegate a coding task, continue a previous Claude session, or any task benefiting from Claude Code's tools (file editing, code analysis, bash, etc.).

Coding & Development

bnbchain-mcp

3891
from openclaw/skills

Interact with the BNB Chain Model Context Protocol (MCP) server. Blocks, contracts, tokens, NFTs, wallet, Greenfield, and ERC-8004 agent tools. Use npx @bnb-chain/mcp@latest or read the official skill page.

Coding & Development