4. Embedded Programming¶
Weekly Objective¶
- Group assignment:
- demonstrate and compare the toolchains and development workflows for available embedded architectures
- individual assignment:
- browse through the data sheet 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 connections)
- extra credit: test it on a development board
- extra credit: try different languages &/or development environments
Useful links¶
Vocab¶
There are a lot of jargons that I first met. I decided to record it for future reference. I used ChatGPT to help me learn these terms (The easiest way!).
- Development Board: a hardware that includes microcontrollers (MCU) and essential ports like ports, communication interfaces, etc.
Example: The chips I used in this week are all development boards. They are really easy to use and help us program conveniently.
- Development Environment: a set of tools, libraries that help us program microcontrollers
Example: Arduino IDE and Thonny
- Tool Chain: Before we have these easy programming softwares, we needed to set up libraries, tools, linkers before programming. The flow of setting up these components is called toolchain.
Tips: Apparently we do not need to do this anymore thanks to technology. So now the toolchain is mainly the board + the port used in which software.
-
Flash: store data even when power is removed (non-volatile memory)
-
RAM Memory: memory that is used to store data temporarily. Once power isturned off, the data in RAM is lost. Allows fastaccess to data (volatile memory)
- Communication Tool: protocols used to transfer data between electronic devices (microcontrollers) and embedded systems (your laptop).
- Types of Communication Tools:
- I2C: allows multiple devices to communicate each other using two wires: SDA and SCL (Only transmit and receive at a time)
- UART: allows two devices to communicate using TX (Transmit) and RX (Receive) (can transmit and receive simultaneously)
- SPI: communication between one master device and multiple slave devices. It is commonly used in embedded systems for high speed communication between microcontrollers, sensors, etc.
- ADC (Analog-to-Digital Converter): converts an analog signal (continuous signal such as voltage) into a digital signal (discrete signal).
- GPIO Pins: basic digital pins on a microcontroller that can be configured as either inputs or outputs.
Code example:
pinMode(13, OUTPUT) // set GPIO pin 13 as output
- Analog Pins: pins are used for reading annalog signals (continuous coltage signals); connected to an ADC.
Code example:
int sensorValue = analogRead(A0); // Read the value from analog pin A0
11. Digital Pins: pins that can only read binary values: HIGH(1) or LOW(1).Code example:
pinMode(D2, OUTPUT); // Set digital pin 2 as output
Group Assignment–Elle Hahn, Cooper Cumbus, Noah Smith, Zijia Fang¶
I was assigned the RP2040 for my group assgnment. I simulated RP2040 in Wokwi and programmed it using both C++ and MicroPython. I made a circuit that makes the LED light up using a push buttton. I also did the development flow and the toolchains for RP2040.
You can see the full documentation here.
ATtiny 412 with C++¶
In Pre-Fab, we soldered the Attiny 412 onto a pre-made board since it is really tiny. Attiny 412 is very cheap. Since it is very old, it needs a little more steps to program it. I used Arduino IDE to program ATtiny 412.
Hardware Setup:
- ATtiny 412 x1
- Arduino Uno x1
Step 1: Make Arduino Uno a proper programmer (change uno to jtag)¶
First, I needed to download a older version of ARduino IDE like 1.8.19.
Since the default Arduino IDE does not have ATtiny as a board, I need to download it from somewhere else. I went to Files –> Preference. Under Preference, I put the link Mr.Dubick provided into the Addional Board Manager URL. Now Arduino IDE is able to identify ATtiny.
After we have the board, we need the right library to make Arduino able to communicate with ATtiny. I downloaded the jtag2udpi library from Teddy Warner’s Website.
Then I opened the jtag2 project file in the Arduino. Under Tools –> Board, select Arduino Uno. More importatnly, select the Programmer as jtag2updi.
Then a new window will pop up. Click verify and upload buttons to check if the Arduino becomes an official programmer.
Step 2: Programmer –> ATtiny 412¶
After setting up Arduino, go back to the Arduino Sketch page (the old window) and change the Board to “ATtiny 412/402/212/202” and still set the programmer to “jtag2updi”.
Ps: Don’t forget to use the port OTHER THAN COM1!
Step 3: Circuit Steup (One LED Blinking)¶
In order to set up the circuit correctly, I need to understand the PINOUT. Since I was programming ATtiny through Arduino Uno, so I needed to match the pinout of ATtiny with Arduino Uno. (GND to GND, VCC to 5V, OUTPUT pin to UPDI)
- Pin 1 on ATtiny 412 is VCC which is where the power is going in. SO Pin 1→ 5V Pin on Arduino
- Pin 8 on ATtiny 412 is GND. SO Pin 8 → GND Pin on Arduino
- Pin 6 on Attiny 412 is UPDI. SO Pin 6 → Pin 6 on Arduino (UPDI Pin as well)
- Resistor connected to Pin 3 (where the LED is connected). SO Pin3 → Pin 1 on Arduino (the signal pin should be Pin 1)
The Circuit Layout is like this:
The code is:
Video of the LED blinking:
Step 4: LEDs Alternating Blinking¶
I added another LED into the circuit.
The Circuit Layout:
I modified the code so that it would be for two LEDs.
The video of two LEDs blinking:
Step 5: 3-LED Blinking Stoplight¶
Th first LED was red, then yellow. I decided to add another green LED so I made a stoplight!
The circuit looked like this:
This is the code for the stoplight:
The video of it working:
XIAO ESP32-C3 with C++¶
Step 1: Simulate in Wokwi using C++¶
Mr.Dubick told us to test the code in Wokwi first so I did that.
Then I got the C++ code from ChatGPT and copied amd pasted it into Wokwi. Here is the video od the simulation.
Step 2: Program the real circuit using Arduino IDE¶
I built the circuit following the pinout and it looked like this.
Then I went to Arduino IDE and selected the right board and port and uploaded the code.
Note: I used school computer which already had esp-c3 board installed. Otherwise, I need to followed this tutorial and got the board installed so that Arduino can program it.
However, the result was not what I expected. The LED did not light up. I consulted my fellew student Noah Smith and he pointed out that my resistor was on the same row which means the resistor was not doing its job, causing the LED to flow. After I fixed it, the code did work. Hoever, I found out that when I held the button, the LED would blink like the simulation video.
Noah said it should be my code problem, so I checked the code and found out that the code was telling the LED to stay on for only 0.5 second and then it would turn off for 0.5 second.
The code for Blinking:
int buttonPin = D1; // button connected to pin D1
int ledPin = D2; // LED connected to pin D2
void setup() {
pinMode(buttonPin, INPUT); // Set button pin as input (no internal pull-up resistor used)
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read the button state
if (buttonState == HIGH) { // Button is pressed (HIGH with pull-down resistor)
digitalWrite(ledPin, HIGH); // Turn LED on
delay(500); // Wait for 500ms
digitalWrite(ledPin, LOW); // Turn LED off
delay(500); // Wait for 500ms
}
}
Then I changed my code and uploaded again and worked perfectly.
The Corrected C++ Code:
int buttonPin = D1; // button connected to pin D1
int ledPin = D2; // LED connected to pin D2
void setup() {
pinMode(buttonPin, INPUT); // Set button pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read the button state
if (buttonState == HIGH) { // Button is pressed
digitalWrite(ledPin, HIGH); // Turn LED on
} else {
digitalWrite(ledPin, LOW); // Turn LED off
}
}
Video of the LED working correctly:
XIAO ESP32-C3 with MicroPython¶
Step 1: Wokwi SImulation¶
I asked ChatGPT to change the C++ code to MicroPython. I first tested my code in Wokwi to make sure it was not the code problem if the circuit did not work.
Step 2: Setting up Thonny¶
I used Thonny as the programmer for MicroPython.
I downloaded Thonny and then folllowed this tutorial to set up everything I need.
- Install esptool using pip in Python.
Notes: I am a Windows user. Be sure to use Administrator version of Command Prompt to install esptool.
- Download bin file from python firmware. I downloaded from here.
- Go to Device Manager and check the port name.
- Go to Command Prompt and type in commands
First, type in Erase Flash command:
esptool.py --port COMX erase_flash
Note: Change the COMX to the right port name such as COM5
Then, navigate to the correct directory and flash the bin file
cd %USERPROFILE%\Downloads
esptool.py --port COMX write_flash -z 0x1000 ESP32_GENERIC_C3-20230602-v1.23.0.bin
Note Again: Change to the correct bin file name!
After I did all of the step above, I went to Thonny and change to the correct options in Interpreter tab.
However, when I pasted my code into Thonny, it did want to upload the code.
It had the error like this:
Device is busy or does not respond. Your options:
- wait until it completes current work;
- use Ctrl+C to interrupt current work;
- reset the device and try again;
- check connection properties;
- make sure the device has suitable MicroPython / CircuitPython / firmware;
- make sure the device is not in bootloader mode.
I checked and did every option Thonny gave but it did not solve the problem. I asked ChatGPT and it told me that I need to change the driver. So then I spent the next 4 hours figuring out how to do the driver thing.
After I restarted my computer 7 times, I successfully made Windows install the driver ChatGPT suggested.
Then I got this yellow warning sign, suggesting that the driver did not work properly.
After suggestions from ChatGPT gave no progress, I asked Noah Smith again, hoping he could fix th driver problem. Then he told me that I did not need the driver to program ESP32-C3 using MicroPython. Then I reopened Thonny, and surprisingly found out that Thonny stopped give me the error message.
I immediately uploaded my code I got from ChatGPT and it worked perfectly well.
MicroPython Code:
from machine import Pin
import time
buttonPin = Pin(3, Pin.IN) # Button connected to pin 3 as input
ledPin = Pin(4, Pin.OUT) # LED connected to pin 4 as output
while True:
buttonState = buttonPin.value() # Read the button state
if buttonState == 1: # Button is pressed
ledPin.value(1) # Turn LED on
else:
ledPin.value(0) # Turn LED off
time.sleep(0.01) # Small delay to debounce
Then I got this:)
XIAO RP2040 with C++¶
Step 1: Wokwi SImulation¶
Since Wokwi does not have RP2040 as the chip, I used Raspberry Pi Pico instead because they are very similar. I used the pinout of Pi Pico to build the simulation. Then I asked ChatGPT to write the code for me.
C++ Code for RP2040:
const int buttonPin = D1; // Button connected to pin D1
const int ledPin = D2; // LED connected to pin D2
void setup() {
pinMode(buttonPin, INPUT_PULLDOWN); // Set the button pin as input with pull-down resistor
pinMode(ledPin, OUTPUT); // Set the LED pin as output
}
void loop() {
if (digitalRead(buttonPin) == HIGH) { // If button is pressed (HIGH due to pull-down resistor)
digitalWrite(ledPin, HIGH); // Turn LED on
} else {
digitalWrite(ledPin, LOW); // Turn LED off
}
delay(100); // Small delay to avoid excessive CPU usage
}
Then I got this:
Step 2: Building real circuit with Arduino IDE¶
I used the same circuit as ESP32-C3. I just replace the C3 chip with RP2040.
I followed this tutorial to set up the board in Arduino.
I went to File –> Preference and copied the Board URL the tutorial provided me to the Additional Board Manager URL
Then, I went to Tool –> Boards –> Boards Manager and searched up RP2040. I downloaded the “Pi Pico/ RP2040” one since it included RP2040 while I can program Pi Pico using that board manager as well.
I selected RP2040 as my board.
Then I select the right port. Usually it is any port other than COM1
Notes: Never use COM1 as your port!
Then I copied the code into Arduino. However, I needed to chnage the pins in the code since I needed to use the pinout of RP2040 other than Pi Pico.
I uploaded the modified code and then my LED worked!
XIAO RP2040 with MicroPython¶
Step 1: Wokwi Simulation¶
I asked ChatGPT to generate the MicroPython code for me. Then I copied the code into Wokwi to check if the code worked. I still used Raspberry Pi Pico for the simulation
Micropython Code for RP2040
from machine import Pin
import time
buttonPin = Pin(3, Pin.IN, Pin.PULL_DOWN) # Button connected to GPIO 15 with pull-down resistor
ledPin = Pin(5, Pin.OUT) # LED connected to GPIO 14
while True:
if buttonPin.value() == 1: # Button is pressed (1 because of pull-down resistor)
ledPin.value(1) # Turn LED on
else:
ledPin.value(0) # Turn LED off
time.sleep(0.1) # Small delay to avoid excessive CPU usage
Step 2: Programming in Thonny¶
I used the same circuit as ESP32-C3
Then I started to set up Thonny using this tutorial.
Fisrt, I went to Tools –> Options –> Inerpreter and then selected the correct interpreter and port
Next, I connected RP2040 with USB C cable. Be sure to hold the “B” button on RP2040 to make it in Bootloader mode.
Then, I went to “Install or update MicroPython” and then select the folowing options.
After clicking “Install”, I copied the MicroPython code into Thonny and then click the green button on the top (“Run” Button)
Then I got this!
Data Sheet: XIAO RP2040 and XIAO ESP32-C3¶
I went to this website and got an overview of all the XIAO series chips, including XIAO RP2040 and XIAO ESP32-C3. Below is a comparison table of the two chips.
Name | XIAO RP2040 | XIAO ESP32-C3 |
---|---|---|
Chip | RP2040 | ESP32C3 |
Speed | 133MHz | 160MHz |
Flash | 2MB (onboard) | 4MB (chip) |
RAM Memory | 264KB SRAM | 400KB SRAM |
GPIO Pins | 14 | 11 |
Analog Pins | 4 (A0-A3) | 4 (A0-A3) |
Communication Protocols | I2C/UART/SPI | I2C/UART/SPI |
ADC (Analog-to-Digital Converter) | 4 ADC inputs (GPIO 26 - GPIO 29) | 4 ADC inputs (A0-A3) |
Bluetooth | NO | YES |
WiFi | NO | YES |
Pros | large on-chip memory, cheap, provide more programs to save and run and less power | WiFi/Bluetooth, suitable for wireless wearable applications |
Summary: If you need to build a project using WiFi or Bluetooth, XIAO ESP32-C3 is a good choice. If you need a chip for a lot of programs, then RP2040 is the best fit.
Interesting fact: In the overview Datasheet, engineerings states that ESP32-C3 are not able to program with MicroPython. However, I did it 2 days ago. Who knows? Everything is possible:)