Ke3036 keyes pico

From znlgis/opengis-skills · View interactive version

Ke3036 keyes pico is an AI agent skill from the GitHub repository znlgis/opengis-skills. Use when programming the Keyes Raspberry Pi Pico (RP2040) learning kit with MicroPython — GPIO LED/button control, I2C/SPI/UART sensor integration, servo/motor/relay driving, Wi-Fi MQTT, 0.96 inch OLED display, ultrasonic ranging. KE3036 Keyes Pico: 42-in-1 sensor STEM learning kit for RP2040.

How to use this skill

Skill file

项目地址(资料/示例): <https://github.com/keyeswiki/KE3036-Keyes-Pico-42-1->(Keyes Wiki 镜像仓库;原 keyestudio/ke3036 仓库已下线)

Pico 官方文档: <https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html>

MicroPython for RP2040: <https://docs.micropython.org/en/latest/rp2/quickref.html>

最新版本: MicroPython v1.29.0(截至 2026-11,GitHub Releases;套件本身无独立版本号,固件随 MicroPython 主线更新)

许可证: 配套示例多为 MIT / GPL(视具体文件)

概述

KE3036 套件常见组成:


环境准备:烧录 MicroPython 固件

# 1. 下载固件(.uf2):
#    Pico   → https://micropython.org/download/RPI_PICO/
#    Pico W → https://micropython.org/download/RPI_PICO_W/

# 2. 按住 Pico 上的 BOOTSEL 键插入 USB → 出现 RPI-RP2 磁盘
# 3. 拷贝 .uf2 到该磁盘 → 自动重启进入 MicroPython

板子若是 Pico W 必须下载 RPI_PICO_W 固件,否则 import network 失败、Wi-Fi 段代码全部不可用。烧录后用 Thonny 的 REPL 执行 import os; print(os.uname()) 自检。


工具

pip install thonny     # 推荐 IDE
pip install mpremote   # 命令行工具(RP2040 无需 esptool,固件用 .uf2 拖拽烧录)
mpremote connect /dev/ttyACM0 ls
mpremote connect /dev/ttyACM0 cp main.py :main.py
mpremote connect /dev/ttyACM0 run main.py

核心 API 速查(MicroPython / RP2040)

| 模块 / 对象 | 用途 | 典型用法 |

|-------------|------|----------|

| machine.Pin | 数字 IO | Pin(25, Pin.OUT) / Pin(14, Pin.IN, Pin.PULL_UP),带 value() / on() / off() / irq() |

| machine.PWM | PWM 输出(LED 调光、舵机) | PWM(Pin(15)) → freq(50) / duty_u16() |

| machine.ADC | 模拟输入 | ADC(Pin(26)) → read_u16() |

| machine.I2C | I²C 总线(OLED 等) | I2C(0, scl=Pin(1), sda=Pin(0), freq=400_000) → scan() |

| machine.SPI | SPI 总线 | SPI(0, baudrate=1_000_000, ...) |

| machine.UART | 串口 | UART(0, baudrate=9600) |

| machine.Timer | 软/硬定时器 | 周期回调;machine.time_pulse_us 可测脉冲宽度(超声波) |

| machine.freq | 主频调节 | freq(125_000_000) |

| rp2.PIO | 可编程 IO(WS2812 等时序敏感外设) | 需配合 .pio 程序 |

| network.WLAN | Pico W 联网 | WLAN(network.STA_IF) → connect() / ifconfig() |

| umqtt.simple | MQTT 客户端(Pico W) | MQTTClient(...) → connect() / publish() / subscribe() |

| dht | DHT 温湿度(内置驱动) | dht.DHT11(Pin(...)) → measure() |

| ssd1306 | OLED 驱动(需自备/mip 装库) | I2C + ssd1306.SSD1306_I2C(...) → text() / show() |

| mip | 联网装第三方包 | mip.install("...") |

| time / utime | 延时与计时 | sleep() / sleep_us() / ticks_ms() |


GPIO 入门

板载 LED 闪烁(Pico GP25)

from machine import Pin
from time import sleep
led = Pin(25, Pin.OUT)
while True:
    led.toggle()
    sleep(0.5)

Pico W 板载 LED:

from machine import Pin
led = Pin("LED", Pin.OUT)

读取按键

btn = Pin(15, Pin.IN, Pin.PULL_UP)
while True:
    if btn.value() == 0: print("pressed")
    sleep(0.1)

PWM 蜂鸣器

from machine import PWM, Pin
buz = PWM(Pin(14)); buz.freq(880); buz.duty_u16(20000)
sleep(0.5); buz.deinit()

模拟输入(电位器 / 光敏)

from machine import ADC, Pin
adc = ADC(Pin(26))               # GP26 = ADC0
val = adc.read_u16()             # 0..65535
volt = val * 3.3 / 65535

板载温度传感器:

adc = ADC(4)
v = adc.read_u16() * 3.3 / 65535
temp = 27 - (v - 0.706) / 0.001721

I²C 与 OLED

from machine import I2C, Pin
import ssd1306
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400_000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
oled.fill(0); oled.text("Hello KE3036", 0, 0); oled.show()

SPI / 1-Wire / DHT

import dht, machine
d = dht.DHT11(machine.Pin(28))
d.measure(); print(d.temperature(), d.humidity())

Pico W 的 Wi-Fi & MQTT

import network, time
wlan = network.WLAN(network.STA_IF); wlan.active(True)
wlan.connect("SSID","PWD")
while not wlan.isconnected(): time.sleep(0.5)
print(wlan.ifconfig())

from umqtt.simple import MQTTClient
c = MQTTClient("pico", "broker.emqx.io", 1883)
c.connect()
c.publish(b"ke3036/temp", str(d.temperature()).encode())

C/C++ SDK(更高性能)

git clone https://github.com/raspberrypi/pico-sdk
export PICO_SDK_PATH=$PWD/pico-sdk
mkdir build && cd build && cmake .. && make
# 生成 *.uf2,按 BOOTSEL 后拷贝到 RPI-RP2
#include "pico/stdlib.h"
int main() {
    gpio_init(25); gpio_set_dir(25, GPIO_OUT);
    while (1) { gpio_put(25, 1); sleep_ms(500);
                gpio_put(25, 0); sleep_ms(500); }
}

常见外设速查

| 外设 | 接法 | 库 |

|------|------|----|

| OLED 0.96 | I²C SDA/SCL | ssd1306 |

| DHT11/22 | 单线 | dht |

| 超声波 HC-SR04 | TRIG/ECHO | machine.time_pulse_us |

| 舵机 SG90 | PWM 50 Hz | PWM |

| 直流电机 + L298N | PWM + IN1/IN2 | PWM/Pin |

| MQ-2 烟雾 | ADC | ADC |

| RFID RC522 | SPI | mfrc522 |


调试与排查

  1. 通过 Thonny → Stop / Restart 进入 REPL
  2. from machine import freq; freq(125_000_000)
  3. 文件传输:Thonny 文件视图,或 mpremote
  4. pico_w 程序需 urequests 等库通过 mip 安装:
import mip; mip.install("umqtt.simple")

教学项目示例


典型工作流

场景一:温湿度记录仪

# 1. 硬件连接:DHT11 接 GP28,OLED 接 I2C0 (SDA=GP0, SCL=GP1)

# 2. main.py
from machine import Pin, I2C
from time import sleep
import dht, ssd1306

# 初始化传感器
sensor = dht.DHT11(Pin(28))
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400_000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

while True:
    sensor.measure()
    temp = sensor.temperature()
    hum = sensor.humidity()

    # 显示到 OLED
    oled.fill(0)
    oled.text(f"Temp: {temp:.1f} C", 0, 0)
    oled.text(f"Hum:  {hum:.1f} %", 0, 20)
    oled.show()

    # 可选:通过 Wi-Fi (Pico W) 上报 MQTT
    # client.publish(b"ke3036/temp", str(temp).encode())

    sleep(5)

# 3. 用 mpremote 部署
# mpremote cp main.py :main.py
# mpremote run main.py

场景二:智能避障小车

# 硬件:L298N 电机驱动 + HC-SR04 超声波
# 电机:IN1=GP2, IN2=GP3, ENA=GP4, IN3=GP5, IN4=GP6, ENB=GP7
# 超声波:TRIG=GP8, ECHO=GP9

from machine import Pin, PWM
from time import sleep_us, sleep
import time

trig = Pin(8, Pin.OUT)
echo = Pin(9, Pin.IN)
ena = PWM(Pin(4)); enb = PWM(Pin(7))
ena.freq(1000); enb.freq(1000)

def distance():
    trig.low(); sleep_us(2)
    trig.high(); sleep_us(10); trig.low()
    while echo.value() == 0: pass
    start = time.ticks_us()
    while echo.value() == 1: pass
    return time.ticks_diff(time.ticks_us(), start) * 0.0343 / 2

while True:
    if distance() < 20:  # 障碍物 < 20cm → 转向
        # 左转
        ena.duty_u16(30000); enb.duty_u16(0)
    else:
        # 前进
        ena.duty_u16(40000); enb.duty_u16(40000)
    sleep(0.1)

常见问题

| 问题 | 解决 |

|------|------|

| 烧录后无 LED | 检查 GP25(Pico)/ "LED"(Pico W)使用正确 |

| ADC 抖动 | 多次平均;接旁路电容 |

| I²C 找不到设备 | i2c.scan() 查看;上拉电阻 4.7kΩ |

| Wi-Fi 不连 | 仅支持 2.4 GHz;密码与国别码(network.country('CN')) |

| 串口在 Linux 需要权限 | sudo usermod -aG dialout $USER |

| OSError: [Errno 5] EIO(初始化 I²C 外设时) | I²C 接线错、设备地址不对或上拉缺失:i2c.scan() 打印实际地址,确认 SDA/SCL 与 Pin() 编号一致,再按扫描结果改 SSD1306_I2C(..., addr=0x3c) |

| OSError: [Errno 110] ETIMEDOUT(wlan.connect 后) | Wi-Fi 连不上:SSID/密码错、只有 5 GHz 频段或国别码未设;用手机热点 2.4 GHz 复测,连接前执行 network.country('CN'),用 wlan.status() 打印失败码对照 MicroPython 文档 |

| ImportError: no module named 'ssd1306' / umqtt | 库没装进 Pico 文件系统(REPL 里能 import 不代表已入库);用 Thonny「文件 → 保存到 Pico」或 mpremote cp,或在 Pico W 上 import mip; mip.install('umqtt.simple') |

| 看门狗复位循环(WDT / 反复重启) | main.py 死循环里没让出 CPU,或引脚电平冲突;循环内加 time.sleep_ms(10),用 Thonny → Stop 进 REPL 后逐段注释定位冲突引脚 |

| 烧录 .uf2 后电脑不出现 RPI-RP2 盘 | 没按住 BOOTSEL 再插线,或 USB 线只供电不通数据;按住 BOOTSEL 重新插线,换一根能传数据的 USB 线,也可用 mpremote 直接连 |


AI 使用建议

推荐工作流

  1. 确认硬件型号:先确定是 Pico 还是 Pico W(Wi-Fi 能力不同),GP25 vs "LED" 引脚名有差异
  2. 烧录 MicroPython 固件:按住 BOOTSEL 插入 USB → 拖入 .uf2 → 自动重启
  3. 用 Thonny 调试:Thonny 的 REPL 交互最适合 MicroPython 快速验证
  4. 先逐个测试外设:LED → 按键 → I²C OLED → 传感器,不要一次性连接所有外设
  5. 最终固化:将 main.py 通过 mpremote 部署到 Pico,上电自启动

关键模式与常见陷阱

如何选择正确方案

| 场景 | 推荐方案 |

|------|---------|

| 快速原型 / STEM 教学 | MicroPython + Thonny |

| 高性能 / 实时控制 | C/C++ SDK (pico-sdk) |

| Wi-Fi 数据上报 | Pico W + MQTT |

| 简单桌面自动化 | 不用 Pico,用 openclaw |


相关技能


参考资源

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.