Skip to content

Week6. Embedded programing

Group assignment:

Browse through the datasheet for your microcontroller Compare the performance and development workflows for other architectures Document your work to the group work page and reflect on your individual page what you learned

Individual assignment:

Write a program for a microcontroller development board to interact (with local input &/or output devices) and communicate (with remote wired or wireless devices)

Group assignment:

browse through the data sheet for your microcontroller compare the performance and development workflows for other architectures

Group assignment can be viewed at this link. - week6 Group assignment

Our group assignment this week was to compare different micro main control boards, and we chose A and B. we are reading the XIAO RP2040 microcontroller datasheet from here and also XIAO-ESP32C3 microcontroller datasheet from here

Alt text Alt text


XIAO-RP2040 and XIAO-ESP32C3 are two compact development boards produced by different manufacturers, each equipped with distinct microcontrollers. They have differences in performance and development workflows:

XIAO-RP2040:

Produced by Seeed Studio, this development board is powered by the Raspberry Pi RP2040 microcontroller. The RP2040 features a dual-core Arm Cortex-M0+ processor, with clock speeds of up to 133MHz. It has 264KB of SRAM and 2MB of onboard flash storage. For development, it supports MicroPython, C/C++, and can utilize the Raspberry Pi’s development ecosystem, including the official Pico SDK and Thonny IDE. Its performance is suited for handling tasks of moderate complexity, such as simple I/O operations, and low to moderate data processing tasks. XIAO-ESP32C3:

Manufactured by Seed Studio and equipped with Espressif’s ESP32-C3 microcontroller. The ESP32-C3 features a single-core RISC-V processor and offers Wi-Fi and Bluetooth LE (Low Energy) functionalities. It contains 400KB SRAM and 4MB of flash memory. Development-wise, it can use the Espressif’s ESP-IDF development framework and also supports the Arduino IDE, MicroPython, and other development environments. Its standout feature in terms of performance is its wireless connectivity, making it ideal for IoT projects that require network connections. Regarding development workflows, both boards support development using the Arduino IDE, providing developers with a familiar programming environment. However, when using their proprietary development tools and SDKs, there are differences. The XIAO-RP2040 may be more focused on general-purpose microcontroller application development when used with the Raspberry Pi’s Pico SDK, while the XIAO-ESP32C3, when used with the ESP-IDF, may lean towards applications that require network connectivity.

In conclusion, the choice of development board depends on the specific requirements of your project. If there’s a need for handling more complex tasks or wireless connectivity, the XIAO-ESP32C3 might be the preferred option. If the tasks are relatively simple, or if there’s a specific requirement for the dual-core CPU of the RP2040, then the XIAO-RP2040 could be a better choice. Both offer flexibility and a variety of tools for development, allowing developers to pick the most suitable environment based on preference and familiarity.


Individual assignment:

write a program for a microcontroller development board that you made, to interact (with local input &/or output devices) and communicate (with remote wired or wireless devices)

extra credit: use different languages &/or development environments extra credit: connect external components to the board

Programming preparation

I used the week4 board for programming, with the buttons on it as input and the LED as output. As shown in the picture Alt text

Install Arduino Alt text

Open Arduino IDE, search for rp2040, and find that there is no such installation package. Alt text

You can find the installation link in seeedstudio’s wiki. Follow the path mentioned in the wiki and copy the link provided.

Alt text

Then press OK and it will be installed automatically. Alt text then we can find the package of RP2040, click install Alt text

then select the board Alt text From now on, the preparations are complete.

Program my MCU board

I’m going to design a program where the LED flashes light of different colors continuously. When the button is pressed, the LED light stops flashing and stays in the current color light effect.

  1. how to make the led flashes light of different colors?

I check the schematic diagram of XIAO RP2040; Alt text

Alt text and I find that DIN is connect to NEOPIX, and GPIO12 is also connect to GPIO12NEOPIX, so LED`s DIN is connect to GPIO12, same way can tell VCC is connect to GPIO11.


“VCC” stands for the positive power supply voltage. It originally referred to the collector voltage in transistor logic circuits but is now commonly used as a generic term for positive power input in electronics. In some contexts, particularly in digital circuits, it represents the “+” or the positive voltage supply to the circuit.

“DIN” stands for Digital Input. It is an interface or pin for receiving digital signals. In microcontrollers, digital integrated circuits, or other digital devices, DIN typically refers to an input port that receives digital signals, which are binary logic levels, such as 0V representing logic 0 and 5V (or other respective high level) representing logic 1.

“GPIO” refers to pin number 12 of the “General Purpose Input/Output” pins. GPIO pins are versatile pins found on microcontrollers, microprocessors, or other digital integrated circuits that can be programmed as inputs or outputs through software. GPIO pins enable users to control and interface with external circuits, such as reading sensor data (input mode) or driving components like LEDs, relays, etc. (output mode).

GPIO12 is one such GPIO pin designated with the number 12. Different devices may have specific constraints or special functions associated with their GPIO pins, such as internal peripheral multiplexing, so it’s important to consult the specific device’s datasheet when designing with GPIOs.

Content above come from GPT4


then I open Arduino IDE to build a new sketch Alt text


setup(): This function is called when the Arduino is powered on or reset. It is used to initialize variables, pin modes, start using libraries, etc. The code inside the setup() function runs only once after each power-up or reset of the Arduino board. void loop() { // put your main code here, to run repeatedly: }

loop(): After setup() runs and finishes execution, the Arduino calls the loop() function. This function contains code that needs to be executed repeatedly: reading sensors, updating LEDs, sending data, etc. Whatever is coded inside the loop() function will continue to execute over and over again, creating the main part of your program’s behavior. The loop() function repeats indefinitely until the board is powered off or reset.

Content above come from GPT4


In order to make the LED light flash, we need a library called “Adafruit_NeoPixel’” to implement,click the libary button, search “Adafruit_NeoPixel”, then we can find it and install it Alt text

here is the code Generated with GPT4

#include <Adafruit_NeoPixel.h>

int Power = 11;
int PIN = 12;
int ButtonPin = 27;  // 按钮连接的引脚
#define NUMPIXELS 1

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  pixels.begin();
  pinMode(Power, OUTPUT);
  digitalWrite(Power, HIGH);
  pinMode(ButtonPin, INPUT_PULLUP);  // 设置按钮引脚为输入,并启用内部上拉电阻
}

int colorIndex = 0;  // 当前颜色索引

void loop() {
  bool buttonPressed = digitalRead(ButtonPin) == LOW;

  if (buttonPressed) {
    setColor(colorIndex);  // 设置当前颜色
    colorIndex++;  // 移动到下一个颜色
    if (colorIndex > 4) {  // 如果索引超出颜色数组,重置为0
      colorIndex = 0;
    }
    delay(400);  // 延时以便观察颜色变化效果
  }
}

void setColor(int index) {
  uint32_t colors[] = {
    pixels.Color(15, 25, 205),   // 蓝色
    pixels.Color(103, 25, 205),  // 紫色
    pixels.Color(233, 242, 205), // 浅黄色
    pixels.Color(233, 23, 23),   // 红色
    pixels.Color(12, 66, 101)    // 深蓝色
  };

  pixels.clear();
  pixels.setPixelColor(0, colors[index]);  // 设置指定颜色
  pixels.show();
}

Learning points

In the Arduino platform, the code contains 3 parts,

  1. “#include” This part defines what library files we use, which can include multiple libraries;

  2. “void setup()” This part explains The pins, etc. that will be run or recognized when the program starts, this part will only run once in the program at the beginning;

  3. “void loop()” This part is the main program, this part will run under certain conditions , a program that operates in cycles;

About the code learning

#include <Adafruit_NeoPixel.h>

This code refers to the Adafruit NeoPixel library, which is generated to drive different modules. The library can be found in this location on the sidebar “Library Manager”and downloaded and installed. Alt text

int Power = 11;
int PIN = 12;
int ButtonPin = 27;  // 按钮连接的引脚
#define NUMPIXELS 1

These lines define the pins connected to the Arduino. Among them, Power is the pin connected to the LED strip power supply, PIN is the pin that controls the LED data input, ButtonPin is the pin connected to the button, and NUMPIXELS defines the number of LEDs. It is set to 1 here, which means that only one LED is controlled.

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

This line of code creates an Adafruit_NeoPixel object that controls a specified number of LEDs. NEO_GRB + NEO_KHZ800 sets the color order to GRB .And set the communication rate to 800KHz.

void setup() {
  pixels.begin();
  pinMode(Power, OUTPUT);
  digitalWrite(Power, HIGH);
  pinMode(ButtonPin, INPUT_PULLUP);  // 设置按钮引脚为输入,并启用内部上拉电阻
}

The setup() function initializes the LED strip and sets the Power pin as an output, then sets it high to power the LED strip. Set the button pin as an input and enable the internal pull-up resistor

The ButtonPin pin is set as an input and activates the internal pull-up resistor because the button will be connected to ground when pressed, forming a low level.

int colorIndex = 0;  // 当前颜色索引

Define the variable colorIndex to track the currently displayed color.

void loop() {
  bool buttonPressed = digitalRead(ButtonPin) == LOW;

  if (buttonPressed) {
    setColor(colorIndex);  // 设置当前颜色
    colorIndex++;  // 移动到下一个颜色
    if (colorIndex > 4) {  // 如果索引超出颜色数组,重置为0
      colorIndex = 0;
    }
    delay(400);  // 延时以便观察颜色变化效果
  }
}

In the loop() function, it detects whether the button is pressed by reading the status of ButtonPin. If the button is pressed (low level detected), the setColor() function is called to change the color of the LED. The colorIndex is then incremented to switch to the next color, resetting it to 0 if the index exceeds the length of the color array.

Therefore, it will appear that the LED light will light up 4 colors in sequence, and the cycle will continue until the button is pressed, and the LED will stop at the current color.

delay(400) provides a short delay after each color switch so that the color change can be seen.

void setColor(int index) {
  uint32_t colors[] = {
    pixels.Color(15, 25, 205),   // 蓝色
    pixels.Color(103, 25, 205),  // 紫色
    pixels.Color(233, 242, 205), // 浅黄色
    pixels.Color(233, 23, 23),   // 红色
    pixels.Color(12, 66, 101)    // 深蓝色
  };

  pixels.clear();
  pixels.setPixelColor(0, colors[index]);  // 设置指定颜色
  pixels.show();
}

The setColor() function sets the color of the LED on the LED strip according to the passed in index. It first defines an array of colors, each color is created through the pixels.Color() function, which accepts red, green, and blue (RGB) values ​​as parameters.

pixels.clear() clears any previous color settings, pixels.setPixelColor() sets the color of the LED,

pixels.show() updates the LED strip to display the new color.

My doubt: colorIndex > 4 is set like this because there are 4 colors, but the pixels.Color below defines 5 colors. —It turned out later that since indexes start counting from 0, an array with 5 elements (like the colors array) would have indices 0 to 4. So this is make sense.

The effect is achieved

and here is the hero shot Alt text

This week’s course is a good start. I hope to have a greater understanding of programming in the future and to bring my wildest ideas to life.