Seeed Studio XIAO SAMD21 使用 MicroPython
本文档由 AI 翻译。如您发现内容有误或有改进建议,欢迎通过页面下方的评论区,或在以下 Issue 页面中告诉我们:https://github.com/Seeed-Studio/wiki-documents/issues
MicroPython 简介
MicroPython 是一个带有部分原生代码编译功能的 Python 解释器。它提供了 Python 3.5 的一个子集功能,专为嵌入式处理器和受限系统实现。它与 CPython 不同,您可以在 这里 阅读更多关于它们之间差异的信息。

快速入门
首先,我们将把 Seeed Studio XIAO SAMD21 连接到计算机,并通过 MicroPython 上传一个简单的代码,以检查开发板是否正常工作。
硬件准备
- Seeed Studio XIAO SAMD21 x1
- Type-C 数据线 x1
- 电脑 x1
软件准备
- 步骤 1. 根据您的操作系统下载并安装最新版本的 Thonny 编辑器

步骤 2. 启动 Thonny
步骤 3. 点击 "Tools-->Options" 打开设置。

- 步骤 4. 选择 "Interpreter" 界面,将设备设置为 "MicroPython(generic)",端口设置为 "Try to detect port automatically"

连接 Seeed Studio XIAO SAMD21 到电脑并点亮
- 步骤 1. 按住 "BOOT" 按钮,然后通过 Type-C 数据线将 Seeed Studio XIAO SAMD21 连接到电脑。如果连接正常,电脑上会显示一个名为 "Arduino" 的磁盘。

- 步骤 2. 刷写固件
访问官方 链接 下载最新固件。
您也可以通过 编译自己的固件 来确保安全性和支持最新功能,但这不是必须的。
软件开发
SEEED XIAO SAMD21 引脚分配表
Pin | GPIO | Xiao Pin 名称 | IRQ | ADC |
---|---|---|---|---|
2 | PA02 | 0 | 2 | 0 |
4 | PA04 | 1 | 4 | 4 |
10 | PA10 | 2 | 10 | 18 |
11 | PA11 | 3 | 11 | 19 |
8 | PA08 | 4 | * | 16 |
9 | PA09 | 5 | 9 | 17 |
40 | PB082 | 6 | 8 | 2 |
41 | PB09 | 7 | 9 | 3 |
7 | PA07 | 8 | 7 | 7 |
5 | PA05 | 9 | 5 | 5 |
6 | PA06 | 10 | 6 | 6 |
18 | PA18 | RX_LED | 2 | * |
30 | PA30 | SWCLK | 10 | * |
31 | PA31 | SWDIO | 11 | * |
19 | PA19 | TX_LED | 3 | * |
上传代码
通过点击 "Run current script" 按钮上传代码。第一次运行时,Thonny 会询问您希望将代码文件保存在哪里。This Computer 和 MicroPython device 都可以。
如果您希望离线运行程序,应将程序保存到 XIAO SAMD21。
同时按住 Ctrl + Shift + S,然后选择保存到 MicroPython device。

GPIO 测试 (LED)
我们需要准备:
将以下代码复制到 Thonny。
运行后可以看到蓝色的 RX_LED 点亮并以每秒一次的频率闪烁。
from machine import Pin, Timer
led = Pin(18, Pin.OUT)
Counter = 0
Fun_Num = 0
def fun(tim):
global Counter
Counter = Counter + 1
print(Counter)
led.value(Counter%2)
tim = Timer(-1)
tim.init(period=500, mode=Timer.PERIODIC, callback=fun)

GPIO 控制继电器
我们需要准备:
from machine import Pin, Timer
output_4 = Pin(8, Pin.OUT)
detect_1 = Pin(4, Pin.IN, Pin.PULL_UP)
output_value = Pin(2, Pin.OUT)
Counter = 0
def fun(tim):
global Counter
Counter = Counter + 1
output_4.value(Counter%2)
print(Counter%2,detect_1.value())
if detect_1.value() :
output_value.value(1)
else:
output_value.value(0)
tim = Timer(-1)
tim.init(period=200, mode=Timer.PERIODIC, callback=fun)
人体检测自动控制
我们需要准备:
from machine import Pin, Timer
led = Pin(8, Pin.OUT)
input_value_1 = Pin(4, Pin.IN, Pin.PULL_UP)
input_value_2 = Pin(10, Pin.IN, Pin.PULL_UP)
output_value = Pin(2, Pin.OUT)
Counter = 0
Fun_Num = 0
def fun(tim):
global Counter
Counter = Counter + 1
led.value(Counter%2)
print(input_value_1.value(),input_value_2.value())
if input_value_1.value() :
output_value.value(1)
else:
output_value.value(0)
tim = Timer(-1)
tim.init(period=50, mode=Timer.PERIODIC, callback=fun)
I2C 支持
from machine import Pin, SoftI2C
i2c = SoftI2C(scl=Pin(9), sda=Pin(8), freq=100000)
devices = i2c.scan()
for device in devices:
print("十进制地址: ", device, " | 十六进制地址: ", hex(device))
i2c.writeto(0x51, 'b')
print(i2c.readfrom(0x51, 4)) # 从地址为 0x51 的设备读取 4 个字节
i2c.writeto(0x51, 'a') # 向地址为 0x51 的设备写入 'a'
print(i2c.readfrom(0x51, 4)) # 从地址为 0x51 的设备读取 4 个字节
i2c.writeto(0x51, 'b')
print(i2c.readfrom(0x51, 4))

熟悉 MicroPython 可以让您实现更多功能,我们期待为您创造更多价值。也欢迎随时与我们分享您的项目!
DAC 支持
感谢 Aleksei Tertychnyi 提交的代码,所有相关功能均由他开发和贡献。
from machine import Pin, Timer, DAC
led = Pin(18, Pin.OUT)
counter = 0
dac = DAC(0) # DAC 输出到 A0 引脚
def loop(tim):
global counter
led.value(counter % 2)
print('DAC 值: ', end =" ")
print(counter)
dac.write(counter % 1024)
counter = counter + 1
tim = Timer(-1)
tim.init(period=1000, mode=Timer.PERIODIC, callback=loop)
A0 引脚上的电压将逐渐增加,在达到大约 3.3V 的最大值后,会降至 0V,然后循环重复。
MicroPython 设备控制台
我们的合作伙伴 Neil 为 XIAO 编写了一个基于 MicroPython 的命令行控制台程序。通过该程序,您可以轻松上传、下载和删除文件。我们感谢他对 XIAO 的贡献!
技术支持与产品讨论
感谢您选择我们的产品!我们为您提供多种支持渠道,确保您在使用我们的产品时体验顺畅。我们提供多种沟通方式,以满足不同的偏好和需求。