Skip to main content

Seeed Studio XIAO SAMD21 with MicroPython

Introduction of MicroPython

MicroPython is a Python interprer with a partial native code compilation feature. It provides a subset of Python 3.5 features, implemented for embedded processors and constrained systems. It is different from CPython and you can read more about the differences here.

Getting Started

First, we are going to connect the Seeed Studio XIAO SAMD21 to the computer and upload a simple code from MicroPython to check whether the board is functioning well.

Hardware Setup

Software Setup

  • Step 1. Download and Install the latest version of Thonny editor according to your operating system
  • Step 2. Launch the Thonny

  • Step 3. Click "Tools-->Options" to open the settings.

  • Step 4. Chose the "Interpreter" interface and select the device as "MicroPython(generic)" and the port as "Try to detect prot automatically"

Connect Seeed Studio XIAO SAMD21 to the PC and Light it up

  • Step 1. Press and hold the "BOOT" button and then connect the Seeed Studio XIAO SAMD21 to the PC through the Type-C cable. If it works well, there is an "Arduino" desk shown on the PC.
  • Step 2. Flash the firmware

For windows:

Copy the Seeed XIAO SAMD21 firmware for MicroPython Support and place it in this folder

For Linux:

wget "https://micropython.org/resources/firmware/SEEED_XIAO-20220618-v1.19.1.uf2"
cp SEEED_XIAO-20220618-v1.19.1.uf2 /media/$USER/Arduino/

It is also possible to compile your own firmware in order to ensure security and support for the latest features, but this is not necessary.

Software Development

SEEED XIAO SAMD21 pin assignment table

PinGPIOXiao Pin nameIRQADC
2PA02020
4PA04144
10PA1021018
11PA1131119
8PA084*16
9PA095917
40PB082682
41PB09793
7PA07877
5PA05955
6PA061066
18PA18RX_LED2*
30PA30SWCLK10*
31PA31SWDIO11*
19PA19TX_LED3*

Upload your code

Upload the codes by clicking the "Run current script" button. For the first time, Thonny will ask where you want to save your codes file. Both This Computer and MicroPython device are fine.

If you want to use the program offline, you should save the program to XIAO SAMD21

Press and hold Ctrl + Shift + S at the same time , then select save to MicroPython device

GPIO TEST (LED)

We need to prepare:

Copy the following codes to Thonny.

We can see that the blue RX_LED is lit up and blinks once per second

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 Control Relays

We need to prepare:

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)

Human detection for automatic control

We need to prepare:

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 Support

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))

Familiarity with micropython allows you to do more , we are looking forward to creating more value for you. Feel free to share your projects with us too!

MicroPython Device Console

Our partner Neil has written a command line console program for XIAO using MicroPython. With this programme you can easily upload, download and delete files. We thank him for his contribution to XIAO!

Tech Support & Product Discussion

Thank you for choosing our products! We are here to provide you with different support to ensure that your experience with our products is as smooth as possible. We offer several communication channels to cater to different preferences and needs.

Loading Comments...