Embedded programming ๐
Assignment Objectives ๐
This weekโs assignment was all about getting up close and personal with microcontrollers. The tasks included:
- ๐ Browsing the datasheet for a microcontroller
- ๐ป Writing and simulating a program that:
- ๐๏ธ Interacts with local input/output devices
- ๐ก Communicates via wired or wireless connections
- โก Extra credit: Testing it on a development board
- ๐พ Extra extra credit: Experimenting with different languages and development environments
My Microcontroller Journey ๐ ๏ธ
I've spent years working with Arduino Uno, crafting conceptual projects like:
- ๐ถ A device that taps into brain waves to generate music
- ๐ค Depth-sensing robots using IR sensors
- ๐ Line-following bots powered by ultrasonic sensors
But this week, I stepped out of my comfort zone and explored new boards and environments. My mission? Write some code, simulate output, and see where things break (and hopefully get fixed).
Group Assignment: Exploring Different Boards ๐
As part of the group assignment, we explored different microcontroller workflows. Hereโs a quick overview of the boards we worked with as documented by Samruddhi:
Arduino Uno ๐๏ธ
- Processor: ATmega328P (8-bit)
- Ease of Use: Beginner-friendly, massive community support
- Programming Environment: Arduino IDE
- Strengths: Simple, reliable, tons of libraries
- Weaknesses: Limited processing power, lacks built-in Wi-Fi/Bluetooth
Xiao ESP32 C3 ๐ก
- Processor: ESP32-C3 RISC-V (32-bit)
- Ease of Use: Moderate, some learning curve with ESP-IDF
- Programming Environment: Arduino IDE, ESP-IDF, MicroPython
- Strengths: Wi-Fi and Bluetooth built-in, small form factor
- Weaknesses: Fewer I/O pins compared to full-size ESP32
RP2040 โก
- Processor: Dual-core ARM Cortex-M0+
- Ease of Use: Moderate, requires understanding of PIO (Programmable I/O)
- Programming Environment: Arduino IDE, MicroPython, C/C++
- Strengths: Fast, flexible, powerful GPIO handling
- Weaknesses: No built-in Wi-Fi/Bluetooth, more complex setup
Comparing Development Workflows โ๏ธ
Feature | Arduino Uno ๐๏ธ | Xiao ESP32 C3 ๐ก | RP2040 โก |
---|---|---|---|
Processor | ATmega328P (8-bit) | ESP32-C3 RISC-V (32-bit) | Dual-core Cortex-M0+ |
Wi-Fi/Bluetooth | โ No | โ Yes | โ No |
GPIO Pins | 14 Digital, 6 Analog | 11 Digital, 4 Analog | 30 GPIO |
Programming Environments | Arduino IDE | Arduino IDE, ESP-IDF, MicroPython | Arduino IDE, MicroPython, C/C++ |
Best For | Simple projects, prototyping | IoT applications, wireless projects | High-performance embedded projects |
Datasheets
I went through the datasheet of both the microcontrollers that I used in this week. You can check them out here:
SEEED XIAO RP2040
XIAO ESP32
Pinouts
ES{32 pinout}
RP2040 pinout
RP2040: Blinky Beginnings โจ
- ๐ Started with the RP2040 board
Interface of the working environment
- ๐ Explored project templates to understand pin configurations
- ๐ก Made an LED blink as a basic first project
- ๐ฎ Configured an accelerometer to change Neopixel colors based on orientation
Circuit diagram
- ๐คฆโโ๏ธ Debugging ensued, but no outputโwill revisit in Electronics Week
Code error
Failed to find library
Xiao ESP32 C3: Getting in the Groove ๐
- ๐๏ธ Played with the default LED sequence
- ๐ Modified the text output
- ๐ Changed the LED order to better understand the board
- ๐ Tested Neilโs push-button code in Wokwi (didnโt work ๐)
- ๐ง Added an external push button to get a text output on press
- ๐ก Attempted to use the button to light an LED, but Wokwi had traffic overload
Traffic went down
Now that Wokwi has a little less traffic, its time to pickup where I left off. This time i will only be working with the ESP32 and using ChatGPT to help with the code, with some manual debugging ofcourse!
Basic Input
I connected a push button on Digital pin 2 of the ESP32 and connected the other end to the GND pin. I also activated the internal pullup by adding it in the code. And this is how it went:
Code
#define BUTTON_PIN D2 // GPIO pin connected to button
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP); // use internal pull-up resistor
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
Serial.println("Button Pressed");
} else {
Serial.println("Button Released");
}
delay(200); // debounce delay
}
Sensors and output devices
Next I paired a Light dependend resistor (LDR) module to sense light around and mapped its readings to the colour output of a neopixel ring. I did the colour change in a sweep movement and when I simulated the code I tried the output on extreme sides of the light exposure.
Code
#include <Adafruit_NeoPixel.h>
#define NEOPIXEL_PIN D5
#define NUM_PIXELS 16
#define LDR_PIN D1 // A0 on Xiao ESP32-S3
Adafruit_NeoPixel strip(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
int lightLevel = analogRead(LDR_PIN); // 0 to 4095
Serial.print("LDR Value: ");
Serial.println(lightLevel);
// Map light level to base hue
int baseHue = map(lightLevel, 0, 4095, 0, 65535); // Full 16-bit hue range
for (int i = 0; i < NUM_PIXELS; i++) {
// Add hue offset per pixel to create a gradient
uint16_t hue = (baseHue + (i * 1000)) % 65536;
uint32_t color = strip.ColorHSV(hue);
strip.setPixelColor(i, color);
}
strip.show();
delay(100);
}
Enter Tinkercad: Desk Defense System ๐ก๏ธ
Since Wokwi refused to cooperate, I switched to Tinkercad and built something far more important: a Desk Defense System.
- ๐ Used an ultrasonic sensor to detect intruders
- ๐ Mounted the sensor on a servo motor to scan the area
- ๐ฏ Locked onto the โthreatโ when detected
- ๐จ Triggered a buzzer to sound an alarm
- ๐ฅ Future upgrade: Replace buzzer with a piezo sparker to shoot balls of flash paper (because why not?)
Project Files
Basic Input Sensors & Output devices
Final Thoughts ๐ญ
- ๐ข This week was full of new boards, new bugs, and new frustrations
- ๐ Learned a lot and had fun experimenting
- ๐ Next goal: Get the accelerometer project working in Electronics Week
P.S. If you ever see a small robotic turret guarding a desk, just walk away. Trust me. ๐