Skip to content

Week 04: Embedded Programming

My electrical training stopped at breadboards. So while I am eagar to learn about microcontrollers, I find them very intimidating. Mostly the vocabulary and abriviations 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.

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. blinking light error 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. 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.

soldering legs onto XIAO-ESP32-C6

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.

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

RP2040 Projects

RP2040 Blink Project