Post

ENG | XAIO-RP2040 (Seeed Studio) and MicroPython

Brief guide for using MicroPython and XIAO-RP2040. Display, RTC and buzzer examples.

ENG | XAIO-RP2040 (Seeed Studio) and MicroPython

This will be fast as there are similar articles about Raspberry Pi Pico: Getting starting, Raspberry Pi Pico: Code snippets, nRF52840 Dongle, and XIAO-nRF52840

There is also wiki. Pinout is in Excel file for reasons.

For MicroPython, use one for Raspberry Pi Pico.

To enter bootmode press RESET button while holding BOOT button.

Test LEDs: they are on when pin is grounded.

LEDS

1
2
3
4
5
6
7
8
9
10
11
12
>>> from machine import Pin
>>> blue = Pin(25, Pin.OUT)
>>> red = Pin(17, Pin.OUT)
>>> green = Pin(16, Pin.OUT)
>>> # Go to white
>>> blue.off()
>>> green.off()
>>> red.off()
>>> # Go to black
>>> red.on()
>>> green.on()
>>> blue.on()

I2C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> from machine import Pin, I2C
>>> i2c1 = I2C(1, sda=Pin(6), scl=Pin(7))
>>> # Scan I2C devices
>>> i2c1.scan()
[60, 81]
>>> # Read RTC from board
>>> data = i2c1.readfrom_mem(0x51, 0x02, 7)
>>> data
b'81\x15\x14>&%'
>>> # Try display
>>> import ssd1306
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'ssd1306'

Display driver has to be downloaded as a seprate file ssd1306.py

Buzzer

1
2
3
4
5
>>> from machine import PWM
>>> buzzer = PWM(Pin(29))
>>> buzzer.freq(500)
>>> buzzer.duty_u16(30000)
>>> buzzer.deinit()

Quick RTC, Display and WS2812 LED tests

Some brighter LEDS are accessible via ws2812.py. RTC and Display are on XIAO Expansion Board. Let’s test all at once.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[pavel@marten -=- /tmp]$ mkdir rp2040
[pavel@marten -=- /tmp]$ cd rp2040 
[pavel@marten -=- /tmp/rp2040]$ wget https://files.seeedstudio.com/wiki/XIAO-RP2040/img/micropython/ssd1306.py
Saving 'ssd1306.py'
HTTP response 200  [https://files.seeedstudio.com/wiki/XIAO-RP2040/img/micropython/ssd1306.py]
ssd1306.py           100% [=================================================================>]    4.47K    --.-KB/s
                          [Files: 1  Bytes: 4.47K [4.56KB/s] Redirects: 0  Todo: 0  Errors: 0]
[pavel@marten -=- /tmp/rp2040]$ wget https://files.seeedstudio.com/wiki/XIAO-RP2040/img/micropython/ws2812.py
Saving 'ws2812.py'
HTTP response 200  [https://files.seeedstudio.com/wiki/XIAO-RP2040/img/micropython/ws2812.py]
ws2812.py            100% [=================================================================>]    2.39K    --.-KB/s
                          [Files: 1  Bytes: 2.39K [2.98KB/s] Redirects: 0  Todo: 0  Errors: 0]
[pavel@marten -=- /tmp/rp2040]$ wget https://www.pavelp.cz/assets/files/rtc_pcf8563.py 
Saving 'rtc_pcf8563.py'
HTTP response 200  [https://www.pavelp.cz/assets/files/rtc_pcf8563.py]
rtc_pcf8563.py       100% [=================================================================>]    1.02K    --.-KB/s
                          [Files: 1  Bytes: 1.02K [1.77KB/s] Redirects: 0  Todo: 0  Errors: 0]

[pavel@marten -=- /tmp/rp2040]$ ls -la
total 16
drwxr-xr-x.  2 pavel pavel  100 Jun 14 16:17 .
drwxrwxrwt. 50 root  root  1120 Jun 14 16:17 ..
-rw-r--r--.  1 pavel pavel 1045 Jun 14 16:17 rtc_pcf8563.py
-rw-r--r--.  1 pavel pavel 4584 Feb 10  2022 ssd1306.py
-rw-r--r--.  1 pavel pavel 2457 Feb 10  2022 ws2812.py

[pavel@marten -=- /tmp/rp2040]$ mpremote a1 fs cp rtc_pcf8563.py :
cp rtc_pcf8563.py :
[pavel@marten -=- /tmp/rp2040]$ mpremote a1 fs cp ws2812.py :     
cp ws2812.py :
[pavel@marten -=- /tmp/rp2040]$ mpremote a1 fs cp ssd1306.py : 
cp ssd1306.py :
[pavel@marten -=- /tmp/rp2040]$ mpremote a1
Connected to MicroPython at /dev/ttyACM1
Use Ctrl-] or Ctrl-x to exit this shell
>>> from machine import Pin, I2C
>>> import rtc_pcf8563
>>> i2c = I2C(1, sda=Pin(6), scl=Pin(7))
>>> rtc_pcf8563.init_rtc(i2c)
>>> rtc_pcf8563.read_rtc()
(2025, 6, 14, 16, 23, 26, 6)
>>> import ssd1306
>>> oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
>>> oled.fill(0)
>>> oled.text("Good afternoon!", 0,0)
>>> oled.show()
>>> from ws2812 import WS2812
>>> power.value(1)
>>> led = WS2812(12,1) # pin 12, one led
>>> led.pixels_fill((0,2,3)) # values are up to 255, but BRIGHT
>>> led.pixels_show()

Summary

That’s all. Basically it’s the same as old Raspberry Pi Pico, only differences are:

  • entering boot menu (without unpluging device)
  • USB cable (USB-C)
  • form factor (less GPIO pins, three times smaller)
  • RGB LEDs (good for diagnostics)
  • extra programmable WS2812 LED on board
This post is licensed under CC BY 4.0 by the author.