Week 04: Embedded Programming
Assignment
Group Assignment:
• Demonstrate and compare the toolchains and development workflows
for available embedded architectures
Individual Assignment:
• Browse through the data sheet for a microcontroller
• Write and test a program for an embedded system using a microcontroller
to interact (with input &/or output devices)
and communicate (with wired or wireless connections)
• Extra credit: assemble the system
• Extra credit: try different languages &/or development environments
Timetable for the week

Individual Assignment
This week I started with my individual assignment. I browsed through a bunch of different microcontrollers and decided to go with the ESP32 C3. I went through its datasheet and found a lot of really interesting stuff. Here is the link to the datasheet: ESP32 C3 Datasheet
After reading the datasheet, I learned that the ESP32 C3 is actually really powerful for how small it is.
The ESP32-C3 is a cheap but capable RISC-V based microcontroller that's basically the upgraded version of the ESP8266. It has a 160 MHz single-core processor, built-in 2.4 GHz Wi-Fi, and Bluetooth 5 (LE) with long-range support. What's cool about it is that it uses very little power and has some solid security features like Secure Boot and Flash Encryption.
Next, I wanted to make something that I could also use for my final project while still completing this week's assignment. So I decided to make a simulation on Wokwi where I get input from a flex sensor and display the values on an OLED screen. But before that I started with the basics of Arduino programming first.
Here are some videos I watched to learn the basics:
Arduino 101- Crash Course w/ Mark Rober
After watching those, I started working on Wokwi to make a simulation. I made a simple circuit and wrote a basic program to blink an LED.
I made the circuit using the following components:

Then I wrote this simple program to blink an LED:

And here's the result, yay our LED is blinking!

After that I wanted to try it in real life. So I gathered the components and made the actual circuit:

I downloaded the Arduino IDE from this link: Arduino IDE
This is what the Arduino IDE looks like:

I then rewrote the code to make it work in real life:

And my LED is actually blinking in real life now!
Here is the link to my Wokwi LED Simulation: LED Simulation
After learning the basics, I moved on to working with the flex sensor, OLED screen, and ESP32 C3. I first learned about the input and output devices and their operating voltage. Operating voltage is basically the amount of electrical power (in volts) that a device needs to actually work properly. The operating voltage for the flex sensor and the OLED screen is 3.3V to 5V. For the ESP32 C3 it's 3.3V - 3.6V, but we can power it using 5V through the dedicated pin (VCC/5V).
I started designing the circuit on Wokwi, but turns out it doesn't support flex sensors yet.
So I switched to Tinkercad, but they don't have the ESP32-C3 board either, and the OLED screen wasn't available there too. To work around this, I just used an Arduino Uno and an LCD in the simulation to test the logic and connections. I'll switch back to the ESP32-C3 and OLED display when I build the actual circuit for my final project.
I opened Tinkercad and made the circuit:

Then I gathered the components and built the real circuit:


To use the LCD (I2C), I added the LiquidCrystal_I2C library from the library manager in Tinkercad.

After that I learned how to get the raw data from the flex sensor and convert it to degrees. I also learned how to use the LCD (I2C) display. I used Claude AI to help me understand both of these things. Click here to view the chat with Claude
Then I started writing the code for the simulation. I didn't use AI to write the code - I just used what I had learned so far. Here's the code I wrote:
#include
// Initialize LCD with I2C address 0x20 LiquidCrystal_I2C lcd(0x20, 16, 2);
// Flex sensor connected to analog pin A0 const int flexPin = A0;
// Variables for sensor readings int flexValue = 0; // Raw data from flex sensor int flexAngle = 0; // converted values in Degree.
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Display startup message
lcd.setCursor(0, 0);
lcd.print("Flex Sensor");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void loop() { flexValue = analogRead(flexPin); // this will read the RAW values from the flex sensor.
// Map the sensor value to an angle (0-90 degrees)
// The map() function re-maps a number from one range to another, typically translating a raw sensor value to a different range
flexAngle = map(flexValue, 256, 62, 0, 90);
flexAngle = constrain(flexAngle, 0, 90);
// Display on LCD
lcd.setCursor(0, 0);
lcd.print("Flex Value:");
lcd.print(" "); // Clear previous value
lcd.setCursor(12, 0);
lcd.print(flexValue);
lcd.setCursor(0, 1);
lcd.print("Angle: ");
lcd.print(" "); // This line will clear the previous value
lcd.setCursor(7, 1);
lcd.print(flexAngle);
lcd.print((char)223); // This prints the degree symbol
// Small delay for stability
delay(100);
}
The simulation is working! Here's the result, yay!!

Here is the link to my Tinkercad simulation
Now let's build the actual circuit in real life! ^-^
I gathered all the components and put the circuit together:

After that I opened the Arduino IDE and added the libraries:

Then I copy-pasted the code from the Tinkercad simulation, connected the Metro board to my computer, and set the port and board type.


After uploading the code it worked perfectly. Yay!!
After learning a lot about Arduino and programming, I wanted to make something for my final project. I wanted to use the XIAO ESP32C3 board and display text when a certain value is reached from the flex sensor. This will help me understand the flex sensor values better, which is a really important component for my final project.
I gathered all the components and made the circuit.


After that I downloaded the following libraries for the OLED display:
Adafruit_GFX.h Adafruit_SSD1306.h

I then added the XIAO ESP32C3 board to the Arduino IDE. This is the link: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
I added the link to the preference URL tab. Then I downloaded the ESP32 from the Board Manager.


After carefully installing all the libraries and components, I uploaded the code to the XIAO ESP32C3 board.


After that I wrote the code. Note that the code wasn't entirely written by me. It was written with the help of Claude AI.
Libraries Used
I used three libraries for this project:
- Wire.h — handles I2C communication between the microcontroller and the OLED
- Adafruit_GFX.h — a base graphics library for drawing text and shapes
- Adafruit_SSD1306.h — specifically for controlling the SSD1306 OLED display
How the Code Works
1. Defining the Screen and Pin
#define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define FLEX_PIN D0
This part sets up the OLED display size (128×64 pixels) and tells the code which pin the flex sensor is connected to.
The OLED_RESET -1 means I'm not using a separate reset pin.
2. Setting the Flex Sensor Ranges
#define IDLE_MIN 350 #define HELLO_MIN 280 #define VISION_MIN 200 #define THANKS_MIN 110
This was probably the most important part I had to figure out.
The flex sensor gives different analog values depending on how much it's bent.
I tested it many times using the Serial Monitor and wrote down the ranges:
| Flex Value Range | What it Shows |
|---|---|
| 350 – 360 | Idle (not bent) |
| 280 – 345 | "HELLO WORLD" |
| 200 – 275 | "I AM VISION VOICE" |
| 110 – 195 | "THANK YOU" |
I stored the phrases in an array so I can call them easily by index number:
const char* words[] = { "HELLO WORLD", "I AM VISION VOICE", "THANK YOU" };
3. The displayWord() Function
void displayWord(const char* word) { display.clearDisplay(); display.setTextColor(SSD1306_WHITE); display.setTextWrap(true);
int textSize = (strlen(word) > 9) ? 1 : 2;
...
}
This function is what actually puts text on the screen.
I added some simple logic here:
- If the phrase is longer than 9 characters, it uses a smaller text size (1) so it fits.
- Otherwise it uses size 2, which looks bigger and cleaner.
I also centered the text on the screen using getTextBounds():
int yPos = (SCREEN_HEIGHT - h) / 2; int xPos = (SCREEN_WIDTH - w) / 2;
4. The Setup Function
void setup() { Serial.begin(115200); pinMode(FLEX_PIN, INPUT);
display.println("VISION");
display.println("VOICE");
delay(2000);
displayIdle();
}
When the board first powers on:
- It shows a startup screen saying "VISION VOICE" for 2 seconds
- Then it switches to the idle message telling the user to bend the sensor
5. The Main Loop
void loop() { int flexValue = analogRead(FLEX_PIN);
if (currentState != lastState) {
lastState = currentState;
}
delay(100);
}
The loop reads the flex sensor value every 100 milliseconds.
Then it checks which range the value falls into and sets a currentState.
Important Improvement
I made the display update only when the state changes:
- Prevents flickering
- Makes the output much smoother
Results

When I bent the flex sensor, there is a certain range of values that the flex sensor gives and the screen displays the corresponding phrase.
---
Group Assignment
For this week's group assignment, we explored the different microcontrollers available in our lab. We decided to focus on three specific boards: the Arduino UNO, XIAO ESP32C3, and ATtiny 44.
Our goal wasn't just to test them, but to actually understand their specifications like their pin configurations and how to program them, and to see how they differ from each other. This helped us get a better understanding of what each board can do.
We used the Arduino IDE to write our code. It let us compile, debug, and upload the program to the board.
Here is the link to our Group Assignment: Group Assignment
Reflection
This week was pretty heavy since I didn't get much time to complete the assignment. But at the same time it was a really interesting week because I learned a lot about embedded programming. I really enjoyed this week. Hoping to learn more about it from now on! ^-^
