4. Embedded Programing¶
This week I learned how to upload code to a micro controller and simulate it. For gorup work I talked about how to upload the code for one of the microcontrollers I used during the week in Arduino and MicroPython.
Group Work¶
Files¶
Heres the files for Week 4. This includes the codes in MicroPython and Arduino for the ESP32C3 and RP2040 respectivly. It also includes the firmware downloaded for both microcontrollers from MicroPython.
ESP32C3¶
For my first microcontroller I used on I have used before which was the ESP32C3. Its an amazing microcontroller and I had used a similar one in the ESP32S3 in a past project.
This is the pinout for the microcontroller
Datasheet¶
- What chip?
- ESP32-C3
- What speed (indicated in MHz)?
- Up to 160 MHz
- What flash/SRAM memory (for storing programs)?
- 400 KB SRAM
- 4 MB Flash
- How many GPIO pins?
- 11 GPIO Pins
- How many Analog pins?
- 3 Analog pins (A0, A1, A2)
- What communication protocols does the board “speak” (UART, ISP, SPI, I2C, I2S, etc.)?
- UART
- SPI
- I2C
- BLE (Bluetooth Low Energy)
- Wi-Fi
- Does it have an ADC?
- Yes, 12-bit ADC
Arduino¶
For what the microcontroller would do, I had it simulate how a pull down resitor works with a button and LED. How it works is that thiers a positive and negtive power wire running to one side of a button, the negative power wire is opposite to a wire which runs to a port on the ESP thats able to be given commands. This is the input port and when the button isnt pressed the value of it is 0. When the button is pressed the positive wire is able to send power to all parts of the button, but the negative power wire has a 1000 ohm resistor so the only place where the power runs is to the input port. When this happens the value changes to 1. This causes the LED to turn on through a second port that was set as an output that would turn to high when the value of input was 1. To make sure all of this worked I first teseted it in Wokwi so I would be able to get the code right without worrying about the hardware. Once that was set up I mimicked a code example of a pull down resitor for arduino and made it work a ESP32C3.
Heres what the design looks like in WokWi
Heres what the code looked like:
#define LED_PIN D2
#define BUTTON_PIN D4
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
pinMode(BUTTON_PIN, INPUT_PULLDOWN);
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == HIGH) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
// Small delay for debounce and readability
delay(100); }
What each part does¶
Defiing pins:¶
For defiing the pins this allows the pinouts on the microcontroller being used to havea more routine name when putting it in the code.
Void Setup¶
Here is where the functions of the pins is decided and it can be seen that the names gvien to the pinouts are starting to be used. For the pinMode part thats just defining the LED pin as an OUTPUT which means it reacts to certain things happening in the code. The digitalWrite acts as the thing that makes the OUPUT pin react when a certatin thing happens. In this case when the button pin is read as high instead of low which causes the led to go high instead of low.
Installing through Arduino¶
To install this code onto the ESP32C3 through arduino I first had to go through the board manager to download the esp libraies. After that I went through the mac terminal to figure out which com port I needed to use through this command (). After I had all of that setup I was able to copy the code from Wokwi since both use CC+ roots which is a way to program microcontrollers.
I ran into the error: exit status 1 when uploading the code. On the serial monitor it said that the issue didnt origiante from an issue in the microcontroller but instead either in the computer or cord. The first thing I did was replace the USB-C cable with a diffrent one. This didnt work so the second thing I did was go to a diffrent computer in the Fab Lab but still ran into the same issue. It suggested it might be an issue with the soldering of the pinheads on the ESP since I had it setup to work on a breadboard and the soldering job I had done had a bit to be desiered due to the tin solder I used. After fixing it and going to a diffrent computer I ran into then issue again. At this point I was very annoyed with this issue and asked Mr. Dubik for help. He suggested running it on one more computer before doing anything else so I did that and it worked. For some reason of all the computers in the Fablab the three that I went to all had issues with thier USB ports. Im bewildered by this experiance and I think it shows how difficult engineering can be at times.
Micro Python¶
ESP32C3 is very annoying to setup for Micropython. First off you need to download the generic firmware for the ESP32C3. Afcter that I had to go to chatgpt since it made no since how you add the firmware to it. To add the firmware to the ESP the frist thing I had to do was setup the terminal. To do this I pip installed esptool which allowed me to do all the installing firmware stuff. After that I ran a command to give me the port that the ESP was plugged into, once I had that I was able to flash the firmware onto the ESP. The first issue I ran into was that I still had code on the ESP when I had used it through arduino so what I had to do was erase the flash memory on the ESP. To do that it needed to be in bootloader mode. Once that was done the second issue I ran into was that it didnt recognize the device in Thonny. To fix this issue I configured the interpritor to be for esp and to look for the port I had found earlier. Then another issue arrised called error 6 which said that the read failed. I was very tired of all these issues at this point since I had been troubleshooting for multiple hours now so I decided to give up for a bit and come back later. When I did comeback
MicroPython Code¶
from machine import Pin
import time
LED_PIN = 2
BUTTON_PIN = 3
led = Pin(LED_PIN, Pin.OUT)
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_DOWN)
while True:
button_state = button.value()
if button_state == 1:
led.value(1)
else:
led.value(0)
time.sleep(0.1)
Define the GPIO pins for the LED and button¶
LED_PIN = 2 # On the Xiao ESP32C3 you have to use the GPIO pins for LEDs
BUTTON_PIN = 3 # Choose GPIO3 for the button (check your Xiao ESP32C3 pinout)
Set up the LED pin as an output and the button pin as an input with pull-down resistor¶
led = Pin(LED_PIN, Pin.OUT) # OUTPUT becomes .OUT and PIN reffers to the pinout that was defined earlier
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_DOWN)
Main loop¶
while True:
button_state = button.value()
if button_state == 1: # If the button is pressed (HIGH)
led.value(1) # Turn the LED on
else:
led.value(0) # Otherwise, turn the LED off
time.sleep(0.1) # Small delay for debounce and readability (100 ms)
RP2040¶
The second microcontroller that I used was the RP2040. This was another XIAO model so I was able to use mostly the same setup of the breadboard.
Datasheet¶
- What chip?
- RP2040
- What speed (indicated in MHz)?
- 133 MHz
- What flash/SRAM memory (for storing programs)?
- 2 MB Flash
- 264 KB SRAM
- How many GPIO pins?
- 11 GPIO pins
- How many Analog pins?
- 4 Analog pins (A0–A3)
- What communication protocols does the board “speak” (UART, ISP, SPI, I2C, I2S, etc.)?
- UART
- I2C
- SPI
- I2S
- PWM
- Does it have an ADC?
- Yes, 12-bit ADC
Arudino¶
For the most part the actions taken for importing the code for the RP2040 were very similar to the ESP32C3 ones even using the same code for the most part. The only thing that was diffrent was that I didnt use the computers that had the issue with uploaing code and I changed the pinouts based on the pinouts of the RP2040. It was also helpful that for the mostpart the pinouts are mostly the same including the power and gnd ports. This lead to little change in the wiring on the breadboard and a quick win for me.
Micro Python¶
The RP2040 was much easier to setup for micropython compared to the ESP32C3. First off all I didnt have to run any commands through my terminal. and their was no issues with wiping the flash. To add the firmware to the RP2040 all I had to do was download the file from the MicroPython website to my desktop. From their I plugged in the RP2040 and set it to bootloader mode. This caused the drive to be added to my desktop. The only thing I had to do afterwards was put the downloaded firmware into the drive of the RP2040 and it worked.
Defining Pins¶
define LED_PIN A2 becomes LED_PIN = 26. The pin becomes directly assoanged and the pins go from analog to micropython¶
Void Setup¶
pinMode(LED_PIN, OUTPUT) becomes Pin(LED_PIN, Pin.OUT) and digitialWrite(LED_PIN, LOW) becomes led.value(0) which sets the LED pin to LOW ie off. pinMode(BUTTON_PIN, INPUT_PULLDOWN) becomes Pin(BUTTON_PIN, Pin.IN, Pin.PULL_DOWN) which is similar to the change in how you write the LED pins output code.
Void Loop¶
int buttonState = digitalRead(BUTTON_PIN) becomes button.value() which reads the button state 1 = HIGH and 0 = LOW. if (buttonState == HIGH) changes to if button_state == 1 which as rercently mentioned means that the button value is high. digitialWrite(LED_PIN, HIGH) changes to led.value(1) which once again means high so the LED turns on. when the button state == 0 then digitalWrite(LED_PIN, LOW) occurs and becomes led.value(0) which means low and the LED turns off in micropython.
Workflow¶
Arduino¶
-
Write your code in Arduino in the code language CC+ and plug in the ESP into the computer
-
Click on the tools tab and if need go to library manager to add the nessicary libraies for the code. Afterwards select the board tab. If you don’t have the esp board tab go to the board manager and type into the search board ESP. For the XIAO ESP32C3 it is closer to the bottom compared to the top so it will take a while to find.
-
While under the tool tab go over to the port tab and find the port that the ESP is plugged into. For a mac make sure its not either of the bluetooth ports and for a desktop computer make sure it isnt com 1.
-
Once all that is done under the tools tab you can either verify the code or imideatly upload it since the upload also verifies the code.
MicroPython¶
-
The first thing you have to do is download the latest firmware for the ESP32C3 on the Micro Python website.
-
After you do that you nedd to make sure you have esptool installed through python. This requires the downloading of python to do so. This is required so that your able to flash the firmware directly to the ESP32C3.
- Once you have that done check the port that your ESP is plugged into using the command: ls /dev/cu.*
- With this command you can erase the flash memory of the ESP if nessicary with this command: esptool.py –chip esp32c3 –port /dev/cu.usbmodem14201 erase_flash (Its nessicary when you have already uploaded code to the ESP already in any way.)
- Once that is done you can flash the firmware to the ESP with this command: esptool.py –chip esp32c3 –port /dev/cu.usbmodem14401 –baud 460800 write_flash -z 0x1000 ESP32_GENERIC_C3-20241129-v1.24.1.bin