Seeed Studio XIAO SAMD21 与 MicroPython
MicroPython 介绍
MicroPython 是一个具有部分原生代码编译功能的 Python 解释器。它提供了 Python 3.5 功能的子集,专为嵌入式处理器和受限系统实现。它与 CPython 不同,您可以在这里了解更多差异。

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

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

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

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

- 步骤 2. 刷写固件
只需前往官方链接并下载最新固件
也可以编译您自己的固件以确保安全性和对最新功能的支持,但这不是必需的。
软件开发
SEEED XIAO SAMD21 引脚分配表
引脚 | GPIO | Xiao 引脚名称 | 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("Decimal address: ",device," | Hexa address: ",hex(device))
i2c.writeto(0x51, 'b')
print(i2c.readfrom(0x51, 4)) # read 4 bytes from device with address 0x51
i2c.writeto(0x51, 'a') # write 'a' to device with address 0x51
print(i2c.readfrom(0x51, 4)) # read 4 bytes from device with address 0x51
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 on A0 output
def loop(tim):
global counter
led.value(counter%2)
print('DAC value: ', 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 的贡献!
技术支持与产品讨论
感谢您选择我们的产品!我们在这里为您提供不同的支持,以确保您使用我们产品的体验尽可能顺畅。我们提供多种沟通渠道,以满足不同的偏好和需求。