4. Embedded Programming¶
This week I learned how to program mirco-controllers.
Individual assignments¶
- Browse through the datasheet 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 connection)
Downloads¶
Websites Used¶
Tutorials¶
Files¶
Students Who Helped Me¶
Programming Microcontrollars¶
Using tinkerCAD and Wokwi to simulate things and using C++ in arduino. We always use a simulator because if the code works on that then we know that if we run into a problem it’s the hardware.
Datasheet¶
1. What chip?
ESP32-C3
2. What speed(MHz)?
Operates at up to 160 MHz
3. What flash/SRAM memory (for storing programs)?
Flash Memory: Typically 4 MB (external SPI flash), though this depends on the specific module or board configuration.
SRAM: 400 KB total, with:
- 320 KB available for general use.
- 16 KB as a cache for flash memory.
- 64 KB reserved for system use.
4. How many GPIO pins?
22 General-Purpose Input/Output (GPIO) pins (GPIO0 to GPIO21)
- Not all of them are available for general use
- some are reserved for specific functions like boot mode selection, USB, or flash memory access.
GPIO | Best Uses | Notes |
GPIO0 | Boot mode selection, I2C, PWM | Must be pulled high for normal boot |
GPIO1 | USB Serial/JTAG, UART TX | Avoid using if USB debugging is needed |
GPIO2 | ADC, PWM, I2C, GPIO | General-purpose use |
GPIO3 | USB Serial/JTAG, UART RX | Avoid using if USB debugging is needed |
GPIO4, GPIO5 | ADC, PWM, SPI, I2C | General-purpose use |
GPIO6, GPIO7, GPIO8, GPIO9, GPIO10, GPIO11 | ADC, PWM, I2C | General-purpose use |
GPIO12 | SPI MOSI, PWM | Used in SPI interface |
GPIO13 | SPI MOSO, PWM | Used in SPI interface |
GPIO14 | SPI CLK, PWM | Used in SPI interface |
GPIO15 | SPI CS, PWM | Used in SPI interface |
GPIO16, GPIO18 | UART RX, I2C, PWM | General-purpose use |
GPIO17, GPIO19 | UART TX, I2C, PWM | General-purpose use |
GPIO20, GPIO21 | PWM, I2C | General-purpose use |
5. How many Analog pins?
GPIO2, GPIO3, GPIO4, GPIO5, GPIO6, GPIO7
6. What communication protocols does the board “speak” (UART, ISP, SPI, 12C, 12I, ect.)?
The ESP32-C3 supports these communication protocols:
- UART (for serial communication)
- I2C (for sensors and peripherals)
- SPI (for high-speed devices like displays and SD cards)
- I2S (for audio input/output)
- CAN (TWAI®) (for automotive/industrial use)
- USB Serial/JTAG (for programming/debugging)
7. Does it have an ADC?
The ESP32-C3 has ADC with:
- 2 × 12-bit SAR ADCs
- Up to 6 ADC channels (GPIO2, GPIO3, GPIO4, GPIO5, GPIO6, GPIO7)
- Supports 0–3.3V input range
Important Notes¶
While programing, it is important to test the board first doing hello world. Hello World in programming is the starter code that everyone uses to test. That is a test print to make sure everything is working. However, when dealing with hardware, the hello word code is blink an LED. Therefore, that is what I did before each of the microcontrollers. It’s also important to not run anything on yout hardware until an example code works.
About Button Pull Down Resistors¶
In a Button Pull Down Resistor, the switch is always set to 0. When you press the switches, the current has two places to go: either back to the GND or to the led pin. The GND has a 10k resistor that power doesn’t go through because it’s too high, therefore it goes to the led switching it to 1. A button down pull resistor, it is the opposite where it is always at 1 and then when pressed it goes to 0.
Seeed Esp32C3:¶
When I began to create this project, I followed the tinkerCAD outline for the hardware that our teacher gave us. Then the code is where I messed up. I went straight to chatGPT and it had me use internal pull-up resistors. I don’t know what those are but the code had me using that and I didn’t know. I have learned to start basic and then go to chatgpt if I need help. If I followed my teacher’s directions, then tinkerCAD gave me code and the hardware setup. When I ran the code through chat. It messed up the board somehow so I decided to just rebuild it. Before I did it in Wokwi, I made sure it worked in tinkerCAD this time. Once it did, I moved it to the Wokwi. Then once I started over in that, it worked.
Once I got it in wokwi working, I switched over to Arduino and tested it through there. When testing it in Arduino, we used physical hardware. This meant that we needed to solder our own Seeed Xiao Esp32C3. We soldered headers onto it and stuck that into a breadboard. Then following the simulator, I copied the wiring exactly. Then went into the Arduino that I downloaded and pasted my code into there. Then under tools I went to board and selected my board and port I was using. Then I compiled my sketch and uploaded it and it worked.
Xiao RP-2040¶
from machine import Pin
import time
# Define pins
buttonPin = Pin(2, Pin.IN) # D0 corresponds to GPIO16
ledPin = Pin(3, Pin.OUT) # D1 corresponds to GPIO5
while True:
buttonState = buttonPin.value()
if buttonState == 1:
ledPin.value(1) # Turn LED off
else:
ledPin.value(0) # Turn LED on
time.sleep(0.1) # Small delay to debounce the button
code for the micropython
Arduino Uno¶
For the Arduino Uno I followed the same steps as the Seee Esp32C3 and used simular code. For the Esp32C3 I had to change the code a little bit from the TinkerCad code but when I went back to tinkerCad for the Ardunio Uno, I noticed that the simulator was alreay using an Arduino Uno. This meant that I could copy and paste the coded exacly. Once thing I noticed I did need to change is that it used the built in LED on the board. I didnt want to use that LED, and I wanted to use an actual LED, therefore i added an LED to the breadboard. Then I pinned it to number 3 on the board and changed that in the code. Afte that I had to fugure out my port. I wasn’t sure what COM I was using so I tested all the choices until I figured out I was using COM 7. Once I had that set, I uploaded it and it worked.
// Define the pin number (change this according to your setup)
int buttonState = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(3, OUTPUT);
}
void loop()
{
// read the state of the pushbutton value
buttonState = digitalRead(2);
// check if pushbutton is pressed. if it is, the
// buttonState is HIGH
if (buttonState == HIGH) {
// turn LED on
digitalWrite(3, HIGH);
} else {
// turn LED off
digitalWrite(3, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}
Code for Arduino Uno
Attiny 412¶
The first thing that was needed to do was to get the jtag from teddy warners website. Once we did that we followed what teddy warner said todo once the jtag was downloaded. THis included going to the file we downloaded and the opening it in Arduino IDE. After that step it was to upload the jtag to the Arduino Uno. Then I created a new sketch for the actual code for the attiny 412. Then the important part was to remember to change the board nad programmer. If we didn’t do this then you have to re-make the jtag a programmer. Once I changed the board to the right board based on what I was doing, the I uploaded the code and once it succesfully uploaded thats when I began adding the wiring. It was important to just get it uploaded first because like my teacher says “you need to start small before you go bigger.” After the wiring I re-uploaded some code that had the right pins and had it do what I wanted it todo. Then it worked perfectly.