Skip to content

Week 4: Embedded Programming

Week 4 Assignment:
  1. Group Assignment

    1. Demonstrate and compare the toolchains and development workflows for available embedded architectures
  2. Individual Assignment

    1. Browse through the data sheet for a microcontroller
    2. Write and test a program for an embedded system using a microcontroller to interect (with input &/or output devices)
    3. Extra credit:
      • Assemble the system
      • Try different languages &/or development environments

INDIVIDUAL ASSIGNMENT

DATA SHEET FOR A MICROCONTROLLER

Note

I chose to use the XIAO ESP32-C3 as my microcontroller for this assignment. I also looked at the XIAO RP2040 and compared the information between them. The data sheet can be found here: XIAO Data Sheet.

XIAO ESP32-C3

XIAO ESP32-C3 is a budget-friendly and high-performance RISC-V development board that supports WiFi and Bluetooth.

ESP32-C3 Information
  1. Architecture : RISC-V (processor: 32-bit, 160MHz)
  2. Memory : 400KB SRAM, 4MB Flash
  3. Peripherals

    • Digital I/O : 11 Pins (can be used as PWM pins (Pulse Width Modulation) -> varying the duration, such as light brightness, sound loudness, and motor stong etc)

    • Analog I/O : 4 Pins (can be used as ADC pins (Analog to Digital Converter))

    • Interfaces : I2C, UART, SPI, I2S

  4. Programming Languages : Arduino/MicroPython/CircuitPython

  5. Wireless Connectivity : WiFi and Bluetooth 5 (LE)
  6. Onboard : Reset and Boot button, Charge LED
  7. Power : 5V (Type-C), 3.7V Li-ion (Battery)

XIAO RP2040

The RP2040 is the first microcontroller chip designed by Raspberry Pi. It is a low-power microcontrollers with a good performance.

RP2040 Information
  1. Architecture : Dual-core ARM Cortex-M0+ (processor up to 133MHz)
  2. Memory : 264 KB SRAM, 2MB Flash
  3. Peripherals

    • Digital I/O : 11 Pins (can be used as PWM pins)

    • Analog I/O : 4 Pins (can be used as ADC pins (Analog to Digital Converter))

    • Interfaces : I2C, UART, SPI, SWD Bonding pad, Type-C

  4. Programming Languages : Arduino/MicroPython/CircuitPython

  5. Wireless Connectivity : No
  6. Onboard : Reset and Boot button, User LED, RGB LED, Power LED
  7. Power : 5V (Type-C)

Comparison between XIAO ESP32-C3 and XIAO RP2040

Description XIAO ESP32-C3 XIAO RP2040
Architecture RISC-V Dual-core ARM Cortex-M0+
Processor 32-bit, 160MHz up to 133MHz
Memory 400KB SRAM, 4MB Flash 264 KB SRAM, 2MB Flash
Peripherals 11 D I/O (PWM), 4 A I/O (ADC), 11 D I/O (PWM), 4 A I/O (ADC),
I2C, UART, SPI, I2S I2C, UART, SPI, SWD
Programming Languages Arduino Arduino
MicroPython MicroPython
CircuitPython CircuitPython
Wireless Connectivity WiFi and Bluetooth 5 (LE) No
Onboard Reset and Boot button Reset and Boot button
Charge LED User, RGB, Power LED
Power 5V (Type-C) 5V (Type-C)
3.7V Li-ion (Battery)

EMBEDDED SYSTEM USING A MICROCONTROLLER

In this part, I experimented with lighting an LED bulb with and without a button using different programming languages: CircuitPython, MicroPython and Arduino.

XIAO ESP32C3 with CircuitPython

Source: docs.circuitpython.org

About CircuitPython

CircuitPython is a programming platform designed to run on microcontroller boards. It uses Python as its programming language. One of its advantages is that it does not require upfront desktop downloads; users can write and edit code using any text editor. Since CircuitPython is simple to use, it is suitable for beginners and educational purposes.

Information

I followed the instruction from wiki.seeedstudio.com

Step 1: Installing CircuitPython

  1. Open terminal and write:

    pip install esptool
    

  2. Download the firmware binary file (CircuitPython 10.0.3) from Bin File

  3. Connect the XIAO ESP32C3 on Mac: Hold down the BOOT button on XIAO ESP32-C3 to enter into bootloader mode and plug the USB Type-C cable into PC.

  • List available serial ports:
ls /dev/cu*

Example:

/dev/cu.Bluetooth-Incoming-Port     /dev/cu.usbmodem11201
/dev/cu.JabraEvolve75SE             /dev/cu.wlan-debug

  • Erase Flash:
esptool.py --chip esp32c3 --port /dev/cu.usbmodem11201 erase_flash
  • Write Flash:
esptool.py --chip esp32c3 --port /dev/cu.usbmodem11201 --baud 460800 write_flash -z 0x0 adafruit-circuitpython-seeed_xiao_esp32c3-en_US-10.0.3.bin

Tips

Replace /dev/cu.usbmodem11201 with your port name, and adafruit-circuitpython-seeed_xiao_esp32c3-en_GB-10.1.3.bin with your bin file name

Step 2: Choosing an Editor for CircuitPython

I am using Thonny to upload and edit files. Other editor option: CircuitPython Web Code Editor.

  1. First I opened terminal and asked python to install Thonny:

    pip install thonny
    #open thonny after installation
    thonny
    
  2. After the installation finishes, this will be show up, and you need to choose the port name:

  3. Download the library bundle for CircuitPython from circuitpython.org/libraries. Extract the ZIP file, there will be a folder ‘lib’ with various .mpy files.

  4. Open Thonny -> View -> Files, copy the necessary .mpy files from the lib folder to CircuitPython device/lib. For this time, we need these 4 files: adafruit_ssd1306, adafruit_bus_device,adafruit_register, adafruit_framebuf.mpy.

  5. Edit the code, and then run the code:

This code is derived from wiki.seeedstudio.com:

Turn on LED
import microcontroller
import digitalio
import time

led = digitalio.DigitalInOut(microcontroller.pin.GPIO7)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True  # turn on LED
    time.sleep(1)
    led.value = False  # turn off LED
    time.sleep(1)

This code was generated with ChatGPT assistance:

Turn on LED with Button (Press Once=On, Press Again=Off)
import microcontroller
import digitalio
import time

led = digitalio.DigitalInOut(microcontroller.pin.GPIO7)
led.direction = digitalio.Direction.OUTPUT

button = digitalio.DigitalInOut(microcontroller.pin.GPIO6)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP

led_state = False
last_button_state = True

while True:
    current_button_state = button.value

    # Detect button press (transition from not pressed to pressed)
    if last_button_state and not current_button_state:
        led_state = not led_state
        led.value = led_state
        time.sleep(0.2)  # debounce delay

    last_button_state = current_button_state

XIAO ESP32-C3 with MicroPython

Source: esploradores.com

About MicroPython

MicroPython can be calles as the “mini-version” of Python 3 that runs on microcontrollers and embedded systems. It works well for both beginners and experienced ones.

Information

I followed the instruction from wiki.seeedstudio.com

Step 1: Installing MicroPython

  1. Open terminal and write:

    pip install esptool
    

  2. Download the firmware binary file (v1.27.0 (2025-12-09).bin): Bin File

  3. Connect the XIAO ESP32C3 on Mac: Hold down the BOOT button on XIAO ESP32-C3 to enter into bootloader mode and plug the USB Type-C cable into PC.

  4. List available serial ports:

ls /dev/cu*

Example:

/dev/cu.Bluetooth-Incoming-Port  /dev/cu.usbmodem11201
/dev/cu.JabraEvolve75SE          /dev/cu.wlan-debug
  • Erase Flash:
esptool.py --chip esp32c3 --port /dev/cu.usbmodem11201 erase_flash
  • Write Flash:
esptool.py --chip esp32c3 --port /dev/cu.usbmodem11201 --baud 460800 write_flash -z 0x0 ESP32_GENERIC_C3-20251209-v1.27.0.bin

Tips

Replace /dev/cu.usbmodem11201 with your port name, and ESP32_GENERIC_C3-20251209-v1.27.0.bin with your bin file name

Step 2: Choosing an Editor for MicroPython

I chose Arduino Lab for MicroPython to write and edit the code. It is a lightweight editor for Micropython programming language. The project is sponsored by Arduino.

It is very straightforward to use. You just need to download and open the app, connect to the microcontroller, edit the code, and run it.

Others editors for MicroPython: Thonny, Visual Studio Code pymakr, uPyCraft.

  1. Arduino Lab for MicroPython can be used either as a web version or a desktop version. This time, I used the desktop version for Mac:

  2. Open the downloaded desktop version:

  3. Connect Arduino Lab for MicroPython to XIAO ESP32-C3:

    Tips

    If you can’t connect and see the message “Could not connect to the board. Reset it and try again,” unplug the ESP32-C3, then plug it back in. After that, the connection should work.

  4. Edit the code, and then run the code:

This code is derived from wiki.seeedstudio.com:

Turn on LED
from machine import Pin
import time

# Define the LED pin
led = Pin(7, Pin.OUT)  # Use the correct GPIO number instead of D10

# Blink the LED in a loop
while True:
    led.value(1)   # Turn the LED on
    time.sleep(1)  # Wait for a second
    led.value(0)   # Turn the LED off
    time.sleep(1)  # Wait for a second

This code was generated with ChatGPT assistance:

Turn on LED with Button (Press Once=On, Press Again=Off)
from machine import Pin
import time

# Define pins
led = Pin(7, Pin.OUT)      # LED on GPIO7
button = Pin(6, Pin.IN, Pin.PULL_UP)  # Button on GPIO6 with internal pull-up

led_state = False
last_button_state = 1  # Button is high when not pressed (pull-up)

while True:
    current_button_state = button.value()

    # Detect button press (transition from high to low)
    if last_button_state == 1 and current_button_state == 0:
        led_state = not led_state  # Toggle LED state
        led.value(led_state)
        time.sleep(0.2)  # Debounce delay

    last_button_state = current_button_state

XIAO ESP32-C3 with Arduino IDE

Source: arduino.cc

About Arduino IDE

Arduino IDE is used to write, compile and upload code. The Arduino IDE supports the languages C and C++. It is available in V1 and V2 versions. This time I am using Arduino V1.

Information

I followed the instruction from wiki.seeedstudio.com

Step 1: Erasing Flash

Because I previously programmed MicroPython on the XIAO ESP32-C3, I need to erase the flash first.

Connect the XIAO ESP32C3 on Mac: Hold down the BOOT button on XIAO ESP32-C3 to enter into bootloader mode and plug the USB Type-C cable into PC.

  • List available serial ports:
ls /dev/cu*

Example:

/dev/cu.Bluetooth-Incoming-Port  /dev/cu.usbmodem11201
/dev/cu.JabraEvolve75SE          /dev/cu.wlan-debug
  • Erase Flash:
esptool.py --chip esp32c3 --port /dev/cu.usbmodem11201 erase_flash

Step 2: Editing Code with Arduino IDE

Tips

Firstly, I downloaded the latest Arduino IDE 2.3.7 software, but somehow it doesn’t work on Step 3 below. Therefore, I downloaded Arduino Legacy IDE (1.8.19), and it works.

  1. Download Arduino IDE software and launch the desktop app (Arduino Legacy IDE (1.8.19))

  2. Arduino -> Settings -> Preferences -> Additional boards manager URLs -> copy this URL: https://jihulab.com/esp-mirror/espressif/arduino-esp32.git

  3. Tools -> Board -> Boards Manager, type the keyword “esp32” in the search box, select the latest version of esp32, and install it:

    Note

    I didn’t manage to install the latest version (3.3.7), so I installed the older version (3.0.0).

  4. Navigate to: Tools -> Board -> ESP32 Arduino -> select “XIAO_ESP32C3”.

  5. Choose the port: Tools -> Port -> /dev/cu.usbmodem11201

  6. Edit the code, and then run the code:

Turn on LED
// define led according to pin diagram in article
const int led = D10; // there is no LED_BUILTIN available for the XIAO ESP32C3.

void setup() {
  // initialize digital pin led as an output
  pinMode(led, OUTPUT);
}

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on 
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off
  delay(1000);               // wait for a second
}

SET UP THE CIRCUIT BOARD

Turn on LED

  1. Prepare all the required components:

    • 1 × XIAO ESP32-C3
    • 1 × LED
    • 1 × resistor
    • 1 × Breadboard
    • 2 × Jumper wires
    • 1 × USB Type-C cable (to power/program the board)
  2. Soldering the XIAO ESP32-C3

  3. Arranging the components on the board:

    • I follow this arrangement:

    Source: wiki.seeedstudio.com

    • This is the actual arrangement:

Turn on LED with Button

  1. Prepare all the required components:

    • 1 × XIAO ESP32-C3
    • 1 × LED
    • 1 × resistor
    • 1 x Button
    • 1 × Breadboard
    • 4 × Jumper wires
    • 1 × USB Type-C cable (to power/program the board)
  2. Soldering the XIAO ESP32-C3

  3. Arranging the components on the board: