Skip to content

4. Embedded programming

Group assignment:

  • Compare the performance and development workflows for other architectures.

To see our group assignment click here

Individual assignments:

  • Consult the microcontroller's datasheet
  • Write and test a program for an embedded system using a microcontroller interact (with input and/or output devices) and communicate (via wired or wireless connection)
  • Bonus: assemble the system
  • Bonus: try different programming languages and/or development environments

Introduction to the project

For this embedded programming work, I will use a XIAO RP2040 from Seeed Studio. This choice is motivated by its extremely compact format (stamp format), its low cost, and, above all, the power of its ESP32-C3 SoC based on the RISC-V architecture.

Analysis of the Datasheet : XIAO RP2040

Presentation of XIAO RP2040

Image

The Seeed Studio XIAO RP2040 is an ultra-compact development board designed for connected projects. Equipped with a single-core 32-bit RISC-V processor, it measures only 21 x 17.5 mm, making it ideal for wearables or miniature objects.

Thanks to its low energy consumption and integrated LiPo battery charger, it is perfectly suited for portable systems. Versatile, it supports multiple programming languages, including C/C++ (Arduino), MicroPython, and CircuitPython.

Descriptive technique

Category Specification Details
Board Name XIAO RP2040 Developed by Seeed Studio
Microcontroller RP2040 Dual-core ARM Cortex-M0+
CPU Frequency Up to 133 MHz 32-bit dual-core processor
SRAM 264 KB On-chip SRAM
Flash Memory 2 MB External QSPI Flash
Operating Voltage 3.3 V Logic level: 3.3 V
Input Voltage (VIN) 5 V Via USB Type-C
GPIO Pins 11 Digital I/O Multiplexed functions
Analog Inputs 4 ADC Channels 12-bit ADC resolution
PWM Channels Up to 16 PWM channels Flexible PWM slices
Communication Interfaces UART, I2C, SPI Multiple hardware interfaces
USB Interface USB Type-C USB 1.1 with device and host support
Onboard LED Yes User programmable LED
Onboard Reset Button Yes Reset button available
Antenna No No wireless connectivity onboard
Security Features ROM Bootloader UF2 drag-and-drop programming
Low Power Modes Sleep & Dormant modes Suitable for low-power applications
Dimensions 21 mm × 17.5 mm Ultra-compact form factor
Mounting Type SMD castellated pads Breadboard-friendly design
Programming Support Arduino, MicroPython, CircuitPython, C/C++ SDK Wide ecosystem support
Typical Applications Embedded systems, Robotics, Education, Prototyping Compact high-performance MCU board

Hardware Configuration and Components

To implement and program the Seeed Studio XIAO RP2040, we utilized a specific set of components to test the microcontroller's input/output (I/O) interactions and communication capabilities:

List of Used Components:

  • Microcontroller: Seeed Studio XIAO RP2040 (RISC-V Architecture with integrated Wi-Fi/Bluetooth).
  • Connection Interface: A USB-C cable for both power supply and code uploading (data transfer).
  • Output Peripherals: * An external LED (as the board does not feature a built-in programmable user LED).
  • A 220 resistor to protect the LED.
  • Input Peripherals: A push-button (to test digital interrupts).

Software and Programming Languages To explore the full potential of the XIAO RP2040 and meet the "Bonus" requirements of the week, I experimented with multiple development environments and programming languages:

1. Arduino IDE (C++)

The Arduino IDE was my primary environment for its reliability and extensive library support.

  • Language: C++.

  • Use Case: Implementing standard logic and hardware interactions.

  • Benefit: It simplifies the management of the RP2040 board manager and serial communication.

Download the Arduino software for free from the official Arduino website. We downloaded version 2.3.7.

Image

  • On Windows: run the downloaded .exe file.
  • Click on Install, then wait until the process is complete. Once finished, click on Finish.
  • Open the Arduino IDE. The main interface appears with:
A toolbar (Check, Upload, New, Open, Save)
A code editing area
A console for displaying compilation messages

Image

The Arduino Integrated Development Environment (IDE) allows you to write and modify a program, from the compiler to machine language, and then transfer it to the memory of various microcontrollers. This transfer (upload) is done via the computer's USB port.

After installing the Arduino IDE, we add the Seeed Studio XIAO RP2040 board package so that we can program it directly from within the Arduino environment.

  • Go to File > Preferences and enter the URL below in the Additional Boards Manager URL field:
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

Image

Image

  • Go to Board Manager, type rp2040, and download the map outlined in red in the image below.

Image

  • Go to the Tools tab and follow the diagram below to choose the right card.

Image

1.1 Programming

For programming the Seeed Studio XIAO RP2040 board, we opted to control an LED (turning it on or off) via a push button or the serial monitor commands in the Arduino IDE.

1.1.1 the wiring

For the wiring of our LED and push button, we chose the components already soldered onto the FAB-XIAO board, such as the user LED and the reset or BOOT button, as shown below.

Image


! Note:
* The LED is connected to pin **D6**.
* The button is connected to pin **D7**.
1.1.2 Arduino Code 1 : Turn an LED on or off with a button

Code Source : Arduino_xiao_rp2040_program_1.ino

#define buttonPin D7    // the number of the button pin
#define ledPin D6       // the number of the LED pin

int buttonState = 0;    // variable for reading the button status

void setup() {
  // initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);
  // initialize the button pin as an input
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the button value
  buttonState = digitalRead(buttonPin);

  // check if the button is pressed
  if (buttonState == HIGH ) {
    // turn on LED
    digitalWrite(ledPin, HIGH);
  }
  else {
    // turn off LED
    digitalWrite(ledPin, LOW);
  }
}

Connect your board to your PC using a USB Type-C cable, then go to Tools > Port and select the serial port name for the connected Seeed Studio XIAO RP2040, as shown below.

Image


🔥 Council
Some USB cables are only used to power the system and do not allow data transfer. If you do not have a USB cable or do not know if it is compatible with data transmission, check that your USB Type-C cable supports USB 3.1.

Click the Upload button to flash the code onto the board, as shown below. If the upload succeeds, the message will be highlighted in red.

Image

Congratulations! Your code has been successfully compiled and uploaded to your Seeed Studio XIAO RP2040.

1.1.3 Arduino Code 2 : Turning an LED on or off with the Arduino IDE serial monitor

Code Source : Arduino_xiao_rp2040_program_2.ino

#define ledPin  D6    // the number of the LED pin

String msg ; // data recording variable received 

void setup() {

  Serial.begin(115200); // Opens serial port, sets data rate to 115200 bps
  pinMode(ledPin, OUTPUT);
}

void loop() {

  // reply only when you receive data
  if (Serial.available() > 0) {

    msg = Serial.readString();// read the data received   
    msg.trim(); // remove any \r \n whitespace at the end of the String

    if (msg  == "ON") {
      digitalWrite(ledPin, HIGH);
      Serial.println("The LED is ON");
     } 
    else if (msg == "OFF") {
      digitalWrite(ledPin, LOW);
      Serial.println("The LED is OFF");
     }
   }

 }   

2. Thonny IDE (MicroPython)

I used Thonny to explore a more modern, interpreted approach to embedded systems.

  • Language: MicroPython.

  • Use Case: Rapid prototyping and live testing via the REPL (Read-Eval-Print Loop).

  • Benefit: MicroPython allows for high-level scripting, which is excellent for handling Wi-Fi protocols and strings without the complexity of C pointer management.

MicroPython is a lean implementation of Python 3 designed as an interpreter for microcontrollers, featuring a trimmed-down standard library optimized for resource-constrained devices.

For more details, visit the official MicroPython site. To run MicroPython, we used the Seeed Studio XIAO RP2040 board with the Thonny Python IDE.

2.1 Installation Steps

Thonny installation is straightforward: head to https://thonny.org/, download the version matching your operating system (Windows in our case), and proceed as shown below.

Image

After installation,

Image

connect your XIAO RP2040 via USB Type-C, and in Thonny, select the interpreter under Tools > Options > Interpreter (choose "MicroPython (Raspberry Pi Pico)" for RP2040 compatibility).

Image

Flash the MicroPython UF2 firmware by double-pressing the BOOT button on the board, then drag the UF2 file to the mounted drive.

Image

Once the installation is complete, click "Close". If successful, the following confirmation window should appear.

Image

2.2 Programming
2.2.1 Micropython Code 1 : Turn an LED on or off with a button
from machine import Pin, Timer

led = Pin(0, Pin.OUT)
button = Pin(1, Pin.IN)

led.high()

while True:

    if button.value() == 1 :
        led.high()
    else :
        led.low()

Click the Run button to upload the code, as shown below.

Image

Congratulation
This guide covered programming the Seeed Studio XIAO RP2040 via MicroPython with Thonny, focusing on controlling LEDs/buttons via the elements integrated into the FAB-XIAO board.



3. Rust (Advanced Exploration)

I also investigated Rust, an emerging language in the embedded world known for its memory safety and performance.

Language: Rust.

Objective: To leverage the RISC-V architecture of the ESP32-C3 with a modern, "memory-safe" compiled language.

Environment: Command-line tools (cargo-espflash) and VS Code.

3.1 Why Rust?

Rust is a systems programming language developed by Mozilla as a modern alternative to C/C++, emphasizing both high performance and safety.

  • Safe memory management, addressing C's primary weakness through ownership and borrowing rules.
  • Excellent runtime performance, rivaling C++ while surpassing Python.
  • Comprehensive documentation, including a free online book (Rust Book).
  • A helpful compiler that provides actionable suggestions and error explanations.
3.1 Install Rust

Rust is primarily installed on Windows using the official rustup tool. Visit the official Rust website at https://www.rust-lang.org.
Click Get Started, then download rustup-init.exe.

Double-click rustup-init.exe to launch it.

A terminal window will open; simply press Enter to proceed with the default installation, which includes:
- rustc (Rust compiler)
- cargo (package manager)
- rustup (toolchain manager)
- Required MSVC tools.

Image

Image

Restart Terminal Close and reopen PowerShell or Command Prompt.
In the terminal, run:

rustc --version
Image

- https://tutoduino.fr/tutoriels/programmez-en-rust-sur-xiao-rp2040/