Week 04: Embedded Programming¶
My electrical training stopped at breadboards. So while I am eager to learn about microcontrollers, I find them very intimidating. Mostly the vocabulary and abbreviations confuse me.
I have made this document to keep some helpful definitions on hand. microcontroller definitions
My greatest difficulty has been figuring out how to get my programs onto the microcontrollers, which language, and through what software.
Mr. Dubick recommended we watch these Arduino Tutorials by Paul McWhorter. I have been watching them at every opportunity during this week, while I am cooking, brushing my teeth... I find myself holding my breath when I upload a program, and carrying a large thermos of iced chai tea (as I don't like coffee) like it is a security blanket/ good luck token.
Here is a link to Arduino Programming Language (API) It is a good reference to find how to way in which to write your arduino code.
Here is a helpful link to the types of programs used with the Raspberry Pi microcontrollers, specifically the RP2040. Fab Lab ULB by Nicholas Decoster
Weekly Assignment:¶
Group assignment:
- Browse through the data sheet for your microcontroller
- Compare the performance and development workflows for other architectures
Each member of my group chose one microcontroller to research. I chose the XIAO-RP2040
Our compiled notes can be found here.
Individual assignment:
- Write a program for a microcontroller development board to interact (with an ideally fab-able board) and target a specific architecture, and target the widest possible range
Mr. Dubick ran through this slideshow with us on how to set up micropython.
We installed a code that would allow our future codes to run without unplugging and replugging the chip, or hitting the reset button when run in vscode.
When running the "Hello World" blinking light code, I recieved an error.
I asked Claude to explain what happened.
It was a warning not an error, the code worked fine. However because the code only works if you open it through the specific file in vscode. Which is how it was intended to run.
- must close out of all open files in vscode
- open file PR2040-PROJECTS
- go to "terminal" in the top bar -> "Run Task"
- choose "upload to pico"
To run a program on RP2040 I must run it through vscode
When I first plugged the microcontroller XIAO-RP2040 into my computer 2 lights lit up. I had power. - make sure to use a powercable that can send and recieve data as well as power
I ran the main.py code I then changed the code to increase the length of the blink
Note that because of task.json only code from main.py will run with the task "upload to pico."
I made another .py file to hold the code I wanted to test before changing the main.py file. I called it blinky-dance.py. My goal was to have the light box step, slow, quick, quick, and repeat. Through trial and error I learned that led.toggle() must be the code to switch the output to either on or off, with the first set of code being off. The time.sleep(.5) is the length of time that the led will either be on or off depending on the toggle.
I observed that the light on the chip turns on opposite what I was expecting. I tested this out by running my code with both the chip's led (pin 25) and my led (pin 0) at the same time. You can see in the video that they are opposite eachother.
I later read that the user led is inverted. So my results are as expected.
My code blinky-dance.py has every led off command at .5 seconds. The led then turns on for 1.5 second, slow, then .5 seconds, quick, and another .5 seconds, quick. It then repeats itself.
I wanted to use variables instead of times to make editing easier. I went to w3schools.com to see the coding language. Furthermore, I added a count down code so that the program would only run 10 cycles. I didn't like seeing the program continue on and on as I adjusted my code.
I had trouble figureing out what pin number to use. I now understand that the gray lables P# are the numbers for micropython pin reference.
I then used 4 leds 2 red and 2 green to represent the dancer's feet when steping forward or back.
from machine import Pin
import time
print("Hello World")
ledl1 = Pin(7, Pin.OUT)
ledl2 = Pin(0, Pin.OUT)
ledr1 = Pin(2, Pin.OUT)
ledr2 = Pin(1, Pin.OUT)
t = 10
off = .5
slow = 1.5
quick = .5
while t > 1:
ledl1.toggle()
time.sleep(slow)
ledl1.toggle()
ledr1.toggle()
time.sleep(quick)
ledl1.toggle()
ledr1.toggle()
time.sleep(quick)
ledl1.toggle()
ledr2.toggle()
time.sleep(slow)
ledl2.toggle()
ledr2.toggle()
time.sleep(quick)
ledl2.toggle()
ledr2.toggle()
time.sleep(quick)
ledr2.toggle()
t -= 1
I then added 2 more leds so that the quick steps would be taken together as you would while dancing.
from machine import Pin
import time
print("Hello World")
ledl1 = Pin(7, Pin.OUT)
ledl2 = Pin(0, Pin.OUT)
ledr1 = Pin(2, Pin.OUT)
ledr2 = Pin(1, Pin.OUT)
ledl3 = Pin(4, Pin.OUT)
ledr3 = Pin(6, Pin.OUT)
t = 10
off = .5
slow = 1.5
quick = .5
while t > 1:
ledl1.toggle()
time.sleep(slow)
ledl1.toggle()
ledr1.toggle()
time.sleep(quick)
ledl3.toggle()
ledr1.toggle()
time.sleep(quick)
ledl3.toggle()
ledr2.toggle()
time.sleep(slow)
ledl2.toggle()
ledr2.toggle()
time.sleep(quick)
ledl2.toggle()
ledr3.toggle()
time.sleep(quick)
ledr3.toggle()
t -= 1
XIAO ESP32C6: Our class ran though this slideshow tutorial on setting up the XIAO ESP32C6 in class together.
In this tutorial we: 1. Downloaded Arduino IDE 2, free tool that edits, compiles, and uploads your code 1. go to arduino.cc/en/software 2. click the macOS download (.dmg) 3. open the .dmg file 4. drag Arduino IDE 2 to Applications 5. On first launch, macOS may show a security warning, click "Open Anyway"
- installed the esp32-c6 board package to Arduino IDE 2
- go to File -> Preferences -> "Additional boards manager URLs" field and paste https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
- open the Board Manager, there is an icon on the left sidebar that looks like a computer chip
-
search for "esp32", choose "esp32 by Espressif Systems", and click install
-
connect
- plug in board
- need usb-c data cable
- "Select Board and Port" at the bottom of the Board/Port Dropdown menu
- The IDE can auto-detect your board. If you see it, click on it
if you don't see it, try another usb cable, you may be using a charge only cable
- if you don't see it, search for it, select it, and pick your port from the list on the right
- The IDE can auto-detect your board. If you see it, click on it
- uploaded our sketch
- a "sketch" is the Arduino word for program. Each one has a setup() and loop()
- we copied and pasted this code
const int ledPin = 15; // Built-in LED
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
Serial.println("ESP32-C6 Blink!");
}
void loop() {
digitalWrite(ledPin, LOW); // ON
delay(300);
digitalWrite(ledPin, HIGH); // OFF
delay(300);
}
- use serial monitor and plotter to debug code and visualize sensor in real time
- make sure your board and port are in the top dropdown menu (see step 3)
- click the Verify button that looks like a check to check the code
- you can see where the problems are, and fix them
- click the Upload button it looks like an arrow or Ctrl + U
- no need to unplug or reset the microcontroller, the program does it automatically
I ran the given code to blink the light at equal intervals. Then adjusted the time the pin would be set to low. Then ran a third test that changes one of the blinks to be short to help see the change better.
blinky light test 1 (high 1000, low 1000)
blinky light test 2 (high 1000, low 200)
blinky light test 3 (high 1000, low 200, high 1000, low 1000)
This should have adjusted the time the led was off. However after reviewing the videos, it looks like the time the LED was on changed instead. Could there be a flipflop somewhere? Yes, the user led runs inversly.
After testing the XIAO-2040, we soldered it and the XIAO-ESP32-C6 to there legs so that we could use a breadboard to experiment more easily.

At home, after making the box-step blinking light program for the XIAO RP2040, I wanted to make the XIAO ESP32C6 also run the program. Seeing the 2 microcontrollers sitting on the same breadboard made me want to make them dance together.
To do this I needed to change the code from micropython to aurduino C++ and run it through the aurduino app. (I have since found out that I could use micropyhon to run the code through vscode as I did with the XIAO PR2040.)
I drew out a diagram of the pinouts for the two microcontrollers so that I could chang ethe pins of my led variables to match the XIAO ESP32C6 so that I could easily replace the XIAO RP2040 and keep the led wiring.
I declared my variables as "const int", which means they have an integer value that does not change. They are declared above the void loop so that they do not get renamed each time the loop runs.
Then set up the void setup() { } in which I define the pinMode(pin, OUTPUT) using the variables defined above for the pins. It is important to use variables so that you can change the code more easily.
Then void loop(){ }. In this area the commands are repeated in a loop.
const int ledl1 = 23; // Built-in LED
const int ledl2 = 16;
const int ledl3 = 20;
const int ledr1 = 19;
const int ledr2 = 17;
const int ledr3 = 22;
const int slow = 1000;
const int quick = 500;
void setup() {
pinMode(ledl1, OUTPUT);
pinMode(ledl2, OUTPUT);
pinMode(ledl3, OUTPUT);
pinMode(ledr1, OUTPUT);
pinMode(ledr2, OUTPUT);
pinMode(ledr3, OUTPUT);
Serial.begin(115200);
Serial.println("ESP32-C6 Dance!");
}
void loop() {
digitalWrite(ledl1, HIGH); // ON
delay(slow);
digitalWrite(ledl1, LOW); // OFF
digitalWrite(ledr1, HIGH); // OFF
delay(quick);
digitalWrite(ledl3, HIGH); // OFF
digitalWrite(ledr1, LOW); // OFF
delay(quick);
digitalWrite(ledl3, LOW); // OFF
digitalWrite(ledr2, HIGH); // OFF
delay(slow);
digitalWrite(ledr2, LOW); // OFF
digitalWrite(ledl2, HIGH); // OFF
delay(quick);
digitalWrite(ledl2, LOW); // OFF
digitalWrite(ledr3, HIGH); // OFF
delay(quick);
digitalWrite(ledr3, LOW); // OFF
}
Problems:
- I had a loose wire, and a crossed wire that caused an led not to light, and 2 leds to light at once respectivly. They were both quickly fixed by checking connections.
- The speed was too fast. I changed the constants slow to 1000 and quick to 500. This was easy, because I used variables in my code.
Next step: When I get more resistors I can add 6 more leds, and set up both microcontrollers to run together.
Class Notes¶
Embedded Programming Class Notes