Week 4 Embedded Programming
Week Objectives:
-
Use a microcontroller datasheet
-
Program a board to interact and communicate
-
Describe the programming process
-
Do the Xiao RP2040 for this week's group project
Xiao-RP2040
The Seeed Studio XIAO RP2040 is a small but powerful microcontroller using the RP2040 chip.
This week I programmed a input and output, to keep it simple since I am still pretty new to the XIAO RP2040, I used a button as an input and LED as an output. I wanted to press the button and then the LED turns on, then when I release the button the LED turns off.
XIAO RP2040 Setup
This week I recieved a fresh XIAO RP2040 from the factory. I needed to flash the micropython firmware onto it. I went to micropython to find the firmware. I downloaded the most recent .uf2 file then flashed it to the XIAO RP2040. To flash it, I held down the BOOTSEL button while plugging it into my PC. This put the RP2040 into boot mode. The RP2040 apperared as a drive in my File Explorer. I put the .uf2 file into the RP2040 and the RP2040 was setup with micropython.
XIAO RP2040 Pinout

I found this pinout from here. This week, I will be using the micropython pins.
Wowki
Wowki is a free browser based electronics simulator. The only problem is, Wowki does not have a Xiao RP2040. Luckly, Wowki has the Xiao ESP32C6 which looks identical to the RP2040 so I was able to layout my circuit.

I designed this circuit in wowki. I want P27 to read high, then when the button is pressed, it switches to ground and reads low because it is at 0V. Then I can have a simple if else statement saying P27 is 1, led is off, and if P27 is 0, led is on.
Because I have little to no experience with coding, I consulted microsoft copilot to generate my code. I asked it:
I am a fab academy student and this week I need to use a Xiao RP2040 along with an input and output. I have a 4 pin pushbutton (A and B style) and an LED. I wired my LED to P26 and GND and the pushbutton A pin to P27 and the the pushbutton B pin to GND. Could you generate micropython code so that when the Pushbutton is pressed the led turns on and then when the button is released the LED turns off?
Copilot responded with:
import machine
import time
button = machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_UP)
led = machine.Pin(26, machine.Pin.OUT)
while True:
if button.value() == 0: # pressed (pulled to GND)
led.value(1)
else:
led.value(0)
time.sleep(0.01)
This week I used VSCode to compile and upload the code to the RP2040. To set up VSCode for this, I created a new folder then in the folder I had a .vscode and then under than I had tasks.json. On the same level as .vscode I had main.py. Mr. Dubick gave us the code for tasks.json. This would allow us to go to terminal, then run task, then all of the code would be uploaded to the RP2040.
Here is the code for tasks.json:

I put the code that Copilot generated into main.py:

I once I saved the code, I went to terminal, then run task, and then the code was sent to the rp2040 through a USB cable.
XIAO ESP32C6
while the Xiao ESP32C6 looks identical to the Xiao RP2040, they are entirely different chips.

I found this pinout from here. This pinout helped me figure out how to wire everything and how to code it.
I wanted to have the same basic pushbutton and LED but I wanted to do something a bit different. I wanted the pushbutton to be a on/off toggle.
Since ESP32s use C++/C natively, I would use the pins like D0, D1, D2 etc.
Wowki
Because the RP2040 and ESP32C6 have the same pinout, I could just use the wiring from my RP2040 wowki design. Additionally, using my pushbutton as a toggle would not change the wiring at all, only software.
Coding the XIAO ESP32C6
The esp32 uses C and C++ natively. I decided to use C++ because I hear about it a lot.
Arduino IDE
I downloaded Arduino IDE to upload and compile my code. Downloading and installing was very easy and straight forward.
Next, I added the board url
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
I put it in file > preferences > additional board URLs.
Next I opened the board manager and searched ESP32 by Espressif Systems.
After this, the Xiao ESP32C6 is setup on Arduino IDE.
Plug in your ESP32C6 through a usb cable that can transfer data NOT JUST POWER. Now press the dropdown, go to select other board and port... then select the ESP32C6.
After this, all that is left is to put in your code.
Mr. Dubick helped me figure out how to set up the XIAO ESP32C6 on arduino IDE.
Code Generation
Because I had never used C or C++, I used microsoft copilot to generate my code. I said "I am a Fab Academy student and I need to use a XIAO ESP32C6, an LED, and a A and B style pushbutton. I want to use the pushbutton as a toggle for the LED to turn on and off. The A pin on the pushbutton is wired to D1 and the B pin is wired to GND. The LED is wired to D0. Generate simple code in C++ that fits this criteria."
Copilot gave me this code:
const int LED_PIN = 0; // D0
const int BUTTON_PIN = 1; // D1
bool ledState = false;
int lastReading = HIGH; // last raw reading
int stableState = HIGH; // debounced state
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
// If the reading changed, reset debounce timer
if (reading != lastReading) {
lastDebounceTime = millis();
}
// If stable for long enough, accept it
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != stableState) {
stableState = reading;
// Button press detected (LOW)
if (stableState == LOW) {
ledState = !ledState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
}
}
}
I put this into Arduino IDE and compiled and uploaded it to the ESP32C6.
Running the Code ESP32C6
As you can see, the code works!
ATtiny 412
For the ATtiny 412, I decided to browse a datasheet.
-
8 Bit CPU
-
Runs at up to 20 mhz
-
2 or 4 KB of self programmable memory
-
64B or 128B of EEPROM
-
128B or 256B of SRAM
-
Has Power on Reset
-
Has Brown out Detection
-
Has Sleep Modes
-
One 16-bit Timer/Counter Type A with Dedicated Period Register
-
One 16-bit Timer/Counter type B with Input Capture
-
One 12-bit Timer/Counter type D Optimized for Control Applications
-
One 16-bit Real Time Counter (RTC) Running from External Crystal or Internal RC Oscillator
-
One USART with Fractional Baud Rate Generator, Auto-baud, and Start-of-frame Detection
-
Configurable Custom Logic (CCL) with Two Programmable Lookup Tables
-
Analog Comparator (AC) with Low Propagation Delay
-
10-bit 115ksps Analog to Digital Converter (ADC)
-
8-bit Digital to Analog Converter (DAC)
-
Five Selectable Internal Voltage References: 0.55V, 1.1V, 1.5V, 2.5V and 4.3V
-
6 Programmable I/O Lines
-
8-pin SOIC150
Raspberry Pi Pico 2W

I found this pinout here.
Arduino IDE
I decided to use Arduino IDE to code my pico.
Code
Because I had used pushbuttons for the past two microcontrollers, I decided to use a potentiometer.
I used this tutorial to teach me how to setup and use the pico 2W in Arduino IDE.
I had 3.3V on one end and GND on the other end. In the middle I had a pin going to GP26. My LED was wired to GP1. I wanted to use C++ code so I consulted microsoft copilot. I tolg it:
"I am a Fab Academy student and I am using a Raspberry Pi Pico 2W. I am using a a potentiometer and a LED. I want the potentiometer to be able to communicate with the pico and then it sends and output to the led. The outside pins are wired to 3.3v and GND and the middle pin is wired to GP26. The LED is wired to GP1. Please generate me code that fits these requirements in C++"
Copilot gave me this code:
const int potPin = 26; // GP26 = ADC0
const int ledPin = 1; // GP1 for LED
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
int potValue = analogRead(potPin); // 0–4095 on Pico 2 W
int brightness = map(potValue, 0, 4095, 0, 255);
analogWrite(ledPin, brightness); // PWM output
delay(5); // small delay for stability
}
I pasted the code into Arduino IDE and uploaded it to the pico 2W and it works!
Here is the what it looks like:
Link to Group Project
Reflection
Overall, I didn't find this week to be too hard. I had used the Xiao RP2040 before this week so I didn't find it to be to hard. The Esp32 and Raspberry pi pico were new but the process of using them felt very similar to the RP2040. This week was very helpful and will definitely help me in upcoming weeks where I will use microcontrollers.