Python

June 24, 2026 · View on GitHub

本文档整合了以下源文件:python.md


来源:python.md

简介

MicroPython 是 Python 3 的精简实现,针对在微控制器和受限环境中运行进行了优化。它将 Python 的易用性带入了硬件编程领域。

什么是 MicroPython?

MicroPython 是 Python 3 的完整重新实现,包含标准库的子集,并针对微控制器进行了优化。

特性描述
Python 语法完整 Python 3 语法
交互式 REPL实时代码执行
硬件访问GPIO、I2C、SPI、UART
紧凑仅需 256KB flash
可扩展C/C++ 集成

与其他方案的比较

特性MicroPythonCircuitPythonArduino
语言PythonPythonC/C++
易用性
性能
丰富Adafruit 为主丰富
社区非常大

支持的硬件

兼容开发板

开发板特性
ESP32WiFi、蓝牙
ESP8266WiFi
Raspberry Pi PicoRP2040
BBC micro:bit教育
STM32工业
Pyboard官方开发板

开发板比较

开发板FlashRAMWiFi价格
ESP324MB520KB$5
ESP82664MB160KB$3
Pico2MB264KB$4
micro:bit256KB16KB$15
Pyboard1MB192KB$40

安装

安装 MicroPython

# 从 micropython.org 下载固件
# 刷入开发板

# ESP32
esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash
esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 firmware.bin

# Raspberry Pi Pico
# 连接时按住 BOOTSEL 按钮
# 复制 .uf2 文件到 RPI-RP2 驱动器

开发工具

工具描述
Thonny内置支持的 IDE
Mu简单编辑器
VS Code配合 Pymakr 扩展
rshell远程 shell
ampy文件传输

Thonny 设置

  1. 安装 Thonny
  2. 选择 MicroPython 解释器
  3. 选择开发板端口
  4. 打开 REPL |

基本编程

Hello World

print("Hello, MicroPython!")

变量和数据类型

# 数字
x = 42
y = 3.14

# 字符串
name = "MicroPython"

# 列表
items = [1, 2, 3, 4]

# 字典
config = {
    "wifi_ssid": "MyNetwork",
    "wifi_pass": "password"
}

控制流

# If 语句
if temperature > 30:
    print("Hot!")
elif temperature > 20:
    print("Warm")
else:
    print("Cold")

# For 循环
for i in range(10):
    print(i)

# While 循环
while True:
    do_something()

函数

def blink(led, times=3):
    for _ in range(times):
        led.on()
        time.sleep(0.5)
        led.off()
        time.sleep(0.5)

硬件访问

GPIO 引脚

from machine import Pin
import time

# 创建 LED 对象
led = Pin(2, Pin.OUT)

# 闪烁 LED
while True:
    led.on()
    time.sleep(1)
    led.off()
    time.sleep(1)

引脚模式

模式描述
Pin.OUT输出模式
Pin.IN输入模式
Pin.PULL_UP内部上拉
Pin.PULL_DOWN内部下拉

读取输入

from machine import Pin

# 创建按钮对象
button = Pin(0, Pin.IN, Pin.PULL_UP)

# 读取按钮状态
if button.value() == 0:
    print("Button pressed")

PWM(脉宽调制)

from machine import Pin, PWM
import time

# 创建 PWM 对象
pwm = PWM(Pin(2))
pwm.freq(1000)

# LED 渐变
while True:
    for duty in range(0, 1024):
        pwm.duty(duty)
        time.sleep(0.005)

传感器

温度传感器 (DHT11)

import dht
from machine import Pin

sensor = dht.DHT11(Pin(4))
sensor.measure()

temp = sensor.temperature()
humidity = sensor.humidity()

print(f"Temperature: {temp}°C")
print(f"Humidity: {humidity}%")

超声波传感器

from machine import Pin, time_pulse_us
import time

trigger = Pin(5, Pin.OUT)
echo = Pin(4, Pin.IN)

def measure_distance():
    trigger.value(0)
    time.sleep_us(2)
    trigger.value(1)
    time.sleep_us(10)
    trigger.value(0)
    
    duration = time_pulse_us(echo, 1)
    distance = (duration * 0.0343) / 2
    return distance

I2C 设备

from machine import I2C, Pin

# 创建 I2C 对象
i2c = I2C(scl=Pin(22), sda=Pin(21))

# 扫描设备
devices = i2c.scan()
print(devices)

通信

WiFi 连接

import network
import time

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("SSID", "password")

while not wlan.isconnected():
    time.sleep(1)

print("Connected!")
print(wlan.ifconfig())

HTTP 请求

import urequests

# GET 请求
response = urequests.get("http://httpbin.org/get")
print(response.json())

# POST 请求
data = {"key": "value"}
response = urequests.post("http://httpbin.org/post", json=data)
print(response.json())

MQTT

from umqtt.simple import MQTTClient

client = MQTTClient("esp32", "broker.example.com")
client.connect()

# 发布
client.publish(b"topic", b"message")

# 订阅
client.set_callback(callback)
client.subscribe(b"topic")

蓝牙 (ESP32)

import bluetooth

ble = bluetooth.BLE()
ble.active(True)

文件系统

文件操作

# 写入文件
with open("data.txt", "w") as f:
    f.write("Hello, World!")

# 读取文件
with open("data.txt", "r") as f:
    content = f.read()
    print(content)

文件系统操作

import os

# 列出文件
os.listdir()

# 创建目录
os.mkdir("data")

# 删除文件
os.remove("data.txt")

# 检查文件是否存在
os.stat("data.txt")

显示

OLED 显示屏 (SSD1306)

from machine import I2C, Pin
from ssd1306 import SSD1306_I2C

i2c = I2C(scl=Pin(22), sda=Pin(21))
oled = SSD1306_I2C(128, 64, i2c)

oled.fill(0)
oled.text("Hello!", 0, 0)
oled.show()

LCD 显示屏

from machine import I2C, Pin
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd

i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)

lcd.putstr("Hello, World!")

定时器和中断

硬件定时器

from machine import Timer

timer = Timer(0)
timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: print("Tick"))

中断

from machine import Pin

button = Pin(0, Pin.IN, Pin.PULL_UP)

def callback(pin):
    print("Button pressed!")

button.irq(trigger=Pin.IRQ_FALLING, handler=callback)

电源管理

深度睡眠

import machine

# 睡眠 10 秒
machine.deepsleep(10000)

唤醒源

描述
Timer定时唤醒
Pin引脚状态唤醒
Touchpad触摸唤醒

标准库

用途
machine硬件控制
networkWiFi、蓝牙
time时间函数
os文件系统
jsonJSON 解析
socket网络套接字

第三方库

用途
umqttMQTT 客户端
urequestsHTTP 请求
ssd1306OLED 显示屏
dht温度传感器
onewire1-Wire 协议

故障排除

常见问题

问题解决方案
内存不足减少变量,使用 gc
WiFi 连接失败检查 SSID/密码
导入错误安装库
REPL 无响应重置开发板

内存管理

import gc

# 检查内存
gc.mem_free()

# 垃圾回收
gc.collect()

总结

概念描述
GPIO控制引脚
传感器读取传感器数据
WiFi连接网络
MQTTIoT 消息传递
REPL交互式编程
文件存储数据