Embedded Programming
Task:-
Group assignment:
Demonstrate and compare the toolchains and development workflows for available embedded architectures.
Document your work to the group work page and reflect on your individual page what you learned.
Individual assignments:
Browse through the datasheet for your microcontroller.
Write a program for a microcontroller, and simulate its operation, to interact (with local input &/or output devices)
and communicate (with remote wired or wireless connection).
The whole doorbell device (button, circuit, chip, buzzer) that performs the specific task of producing a sound when pressed -Embedded system.
A tiny chip inside the doorbell, like the doorbell's brain - microcontroller.
Button pressed >> Microcontroller detects signal >> Runs a program ( executes a set of instructions stored in its
memory that tells it what to do when something happens.) >> Microcontroller activates buzzer/ speaker >> Sound produced.
Input device: information recieved by the microcontroller from the outside devices like button or switch.
Output device: Device controlled by the microcontroller, the action or signal produced by a system after processing the input.
Getting Started with Seeed Studio XIAO RP2040
rp2040-datasheet
Toolchain and Development Workflow: Arduino IDE and Thonny IDE
Toolchain:
Toolchain is a set of software tools used to write, compile, upload, and run a program that work together to convert source code into an executable program for specific hardware.
It usually includes:
Editor - write the code
Compiler - convert code to machine code
Assembler - convert assembly to machine code
Linker - join all compiled parts
Uploader - send program to the device
Debugger / Serial tools - test and debug the program
Arduino IDE
I have used the Arduino IDE earlier, and it is a user-friendly software that allows us
to write and upload code to microcontrollers.
From my previous experience, I knew that when using different boards, it is often necessary to
install the required board packages and set the correct port, as I had faced this issue before.
Earlier, I was not aware of the Additional Board Manager URL concept.
This time, I learned about it in more depth, along with many other tools, shortcuts,
and features available inside the IDE, which helped me better understand how different
third-party boards are supported and configured.
Setting up Arduino IDE

Download and Install the latest version of Arduino IDE according to my operation system(windows), then launch the Arduino application.
Understanding the code window and tools:
Installing Seeed Studio XIAO RP2040 board package
I added the Seeed Studio XIAO RP2040 board package to the Arduino IDE by following these steps.
First, I opened File >> Preferences >> pasted the provided link in the Additional Boards Manager URLs clipboard.
This step was necessary because the Arduino IDE does not originally contain files for third-party boards like Seeed Studio.
The URL to a JSON file that provides the board definition, required drivers, and toolchain settings,
and also tells the IDE which boards are available and which compiler/tools need to be downloaded.
After adding the URL, Boards Manager >> searched for "Raspberry Pi Pico/RP2040", and installed the package. This package includes support for the Seeed Studio XIAO RP2040 board, Tools >> Board >> selected the required board from the board list.
Next, I connected the Seeed Studio XIAO RP2040 board to my computer (c-type - USB cable).
While connecting the board for the first time, I held the BOOT button and plugged it into the computer so the system could detect it properly.
Then, Tools >> Port and selected the correct port to which the board was connected.
Learning basic codes and logics
Whenever you open new sketches, it defaults has basic template with two main functions: setup() and loop().
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
#define
Used to assign a name to a constant value before the program compiles. No memory used
int
Used to declare an integer variable in the program. Memory used and value can be changed during execution.
#define ledPin D0 /* Defines/assigns that the LED is connected to pin D0.
Wherever the compiler sees ledPin in the code,
it replaces it with D0. */
int ledPin = D0; /* creates a variable named ledPin and assigns
it the value D0.*/
pinMode()
Used to set the pin as INPUT or OUTPUT.
digitalWrite()
Used to turn a device ON or OFF. By setting the value HIGH or LOW.
#define ledPin D3 // Defines/assigns that the LED is connected to pin D3.
int delayTime = 200; /* creates a variable named delayTime and assigns
it the value 200 ms. */
void setup() {
pinMode(ledPin, OUTPUT); // sets the ledPin as an output pin.
}
void loop() {
digitalWrite(ledPin, HIGH); // turns on the LED by setting the ledPin to HIGH.
delay(delayTime); /* waits for the specified delay time (200 ms)
before proceeding to the next instruction. */
digitalWrite(ledPin, LOW); // turns off the LED by setting the ledPin to LOW.
delay(delayTime);
}
Programming: Blink Example Code
To test the board and my setup, I opened the Blink example code from File >> Examples >> Basics >> Blink.
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
In the Blink example, the constant LED_BUILTIN did not work with the XIAO RP2040 because this board does not use the same built-in LED pin definition as some Arduino boards. By checking the XIAO RP2040 pin-mapping in the Seeed Studio wiki, I found that the onboard RGB LEDs are instead:
#define USER_LED_R 25 // Defines/assigns that the red LED is connected to pin 25.
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin USER_LED_R as an output.
pinMode(USER_LED_R, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(USER_LED_R, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(USER_LED_R, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
#define yellow D0 // Defines/assigns that the yellow LED is connected to pin D0.
#define red D1
#define white D2
#define blue D3
#define green D4
#define button D7
int stoppingTime=250; /* creates a variable storing 250 milliseconds
delay between LED switching. */
void setup() { // put setup code here, to run once:
pinMode(yellow, OUTPUT); // sets the yellow LED pin as output.
pinMode(red, OUTPUT);
pinMode(white, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
pinMode(button, INPUT_PULLDOWN); /* sets the button pin as input and
activates internal pulldown resistor. */
}
void loop(){ // put code here, to run repeatedly:
if(digitalRead (button)== LOW){ /* checks the button state. If the state is LOW
(button is pressed), the condition becomes true */
light(); /* calls the light function to execute the LED sequence when
the button is pressed. */
}
else{ /* If the button is not pressed, all the LEDs are turned off by setting
led's state to LOW. */
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(white, LOW);
digitalWrite(blue, LOW);
digitalWrite(green, LOW);
}
}
void light() { /* user-defined function to execute the LED sequence
when the button is pressed. */
digitalWrite(yellow, HIGH); // turns on the yellow LED
delay(stoppingTime); /* waits for the specified stopping time (250 ms)
before proceeding to the next instruction. */
digitalWrite(yellow, LOW); // turns off the yellow LED
digitalWrite(red, HIGH);
delay(stoppingTime);
digitalWrite(red, LOW);
digitalWrite(white, HIGH);
delay(stoppingTime);
digitalWrite(white, LOW);
digitalWrite(blue, HIGH);
delay(stoppingTime);
digitalWrite(blue, LOW);
digitalWrite(green, HIGH);
delay(stoppingTime);
digitalWrite(green, LOW);
}
#define red D1
#define white D2
#define blue D3
#define green D4
#define button D7
bool blinkState = false; // ON / OFF state
bool lastButtonState = LOW;
long tim =0;
long lastTime =0;
bool ledStatus = HIGH;
void setup() {
// initializing all the led pins as output
pinMode(red, OUTPUT);
pinMode(white, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
pinMode(button, INPUT_PULLDOWN);
Serial.begin(9600);
}
void loop() {
tim = millis();
bool currentButton = digitalRead(button);
// detect button press
if (currentButton == HIGH && lastButtonState == LOW) {
blinkState = !blinkState; // toggle state
if (blinkState)
Serial.println("LED Blinking ON");
else
Serial.println("LED Blinking OFF");
delay(200); // small debounce
}
lastButtonState = currentButton;
// blinking control
if (blinkState) {
if(tim-lastTime >= 300)
{
digitalWrite(red, ledStatus);
digitalWrite(white, ledStatus);
digitalWrite(blue, ledStatus);
digitalWrite(green, ledStatus);
ledStatus = !ledStatus;
lastTime =tim;
}
}
else
{
ledStatus =LOW;
digitalWrite(red, ledStatus);
digitalWrite(white, ledStatus);
digitalWrite(blue, ledStatus);
digitalWrite(green, ledStatus);
}
}
Thonny IDE
Thonny IDE is a simple and beginner-friendly Python IDE that is also suitable for embedded programming. It allows you to write and upload code to microcontrollers, especially those that support Python programming.
from machine import Pin, Timer
led = Pin(25, 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=1000, mode=Timer.PERIODIC, callback=fun)
from ws2812 import WS2812
import utime
import machine
power = machine.Pin(11,machine.Pin.OUT)
power.value(1)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
COLORS = (BLACK, RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE)
led = WS2812(12,1)#WS2812(pin_num,led_count)
while True:
print("Beautiful color")
for color in COLORS:
led.pixels_fill(color)
led.pixels_show()
utime.sleep(0.2)