My particular task for FabAcademy's fourth week was to look through the microcontroller datasheet. In order to interact (with local input and/or output devices) and communicate (with a remote wired or wireless connection), I will write a program for a microcontroller and simulate its operation. Implementing programming protocols is what I'm going to study.
Writing software that communicates directly with hardware in circumstances with limited resources is known as embedded programming, or EP. Sensors, motors, displays, and communication modules are among the gadgets that are controlled by microcontrollers or embedded processors. Due to limited hardware capabilities, programs—which are frequently written in C or C++—need to be optimized for speed, memory utilization, and power efficiency. Real-time operations are common for embedded systems, necessitating prompt and dependable reactions to outside events. Firmware is the permanent program stored in flash memory, and developers examine and troubleshoot it using debugging tools like serial monitors and JTAG/SWD interfaces. Protocols like I2C, SPI, and UART, as well as wireless techniques like Bluetooth and Wi-Fi, are frequently used to communicate with other devices. IoT, robotics, automotive systems, industrial automation, and consumer electronics are just a few of the fields where EP is vital. Power management is particularly important for battery-powered applications.
Embedded programming involves writing software for specialized computers (like microcontrollers and microprocessors) that are embedded within other devices or systems. These systems are designed to perform specific functions, unlike general-purpose computers that handle a wide range of tasks. Embedded programming is crucial in various industries, including automotive, consumer electronics, and industrial automation.
Example: An Arduino board connected to an LED.
Example (Arduino – Blink LED):
void setup() {
pinMode(13, OUTPUT); // Set pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // Turn on LED
delay(1000); // Wait 1 second
digitalWrite(13, LOW); // Turn off LED
delay(1000); // Wait 1 second
}
main()
or setup()
function.Your code controls the physical components:
Step | Action |
---|---|
Hardware Setup | Arduino + LED on pin 13 |
Write Code | LED blink code in Arduino IDE |
Compile | Translates C++ into machine code |
Upload | Program is flashed onto Arduino |
Run | LED blinks on and off every second |
The ATmega328P serves as the foundation for the Arduino Uno R3 microcontroller board. Because of its open-source nature, cost, and ease of use, it is frequently utilized for electronics projects and prototyping.
For coding and uploading programs to my microcontroller, I am using the Arduino IDE. It is a user-friendly open-source platform that supports various boards like the ESP32-C3. With the help of its built-in libraries and community support, I can easily write, verify, and upload code to test different hardware functionalities
Wokwi is an online simulator designed for developing and testing Arduino, ESP32, Raspberry Pi Pico, and other microcontroller-based projects. It allows users to create circuits, write code, and see real-time simulations in a browser without the need for physical hardware. Wokwi supports popular components like LEDs, sensors, displays, and more, making it an excellent tool for prototyping and learning embedded systems quickly and interactively.
As part of the embedded programming assignment, I worked with the ESP32-C3 module integrated with the MPU6050 sensor. I explored I2C communication to read accelerometer and gyroscope data. This setup helped me understand real-time motion sensing and sensor interfacing, laying the foundation for integrating it into my final project.
To interface the MPU6050 sensor with my ESP32-C3 board, I used the Adafruit MPU6050 library in the Arduino IDE. This library simplifies communication with the sensor by providing easy-to-use functions for accessing accelerometer and gyroscope data. After installing the library through the Library Manager, I was able to write and upload code to read motion and orientation values effectively.
Arduino library for the MPU6050 sensors
#include <Adafruit_MPU6050.h> // Include the library for MPU6050 sensor
#include <Adafruit_Sensor.h> // Include Adafruit sensor support
#include <Wire.h> // I2C communication library
Adafruit_MPU6050 mpu; // Create an MPU6050 sensor object
sensors_event_t event; // Define a variable to hold sensor data
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud
pinMode(D2, OUTPUT); // Set pin D2 as OUTPUT (e.g., for LED or other output)
pinMode(D3, OUTPUT); // Set pin D3 as OUTPUT
Serial.println("");
Serial.println("Hello, XIAO ESP32-C3!"); // Welcome message
Serial.println("Welcome to Wokwi :-)");
while (!mpu.begin()) { // Keep trying until MPU6050 initializes
Serial.println("MPU6050 not connected!"); // Error message
delay(1000); // Wait for 1 second before retrying
}
Serial.println("MPU6050 ready!"); // MPU6050 successfully initialized
}
void loop() {
// The following commented lines were for testing LEDs on different pins
// Serial.println("Red");
// digitalWrite(D2, HIGH); // Turn on LED on D2
// delay(500); // Wait for 0.5 seconds
// digitalWrite(D2, LOW); // Turn off LED
// Serial.println("Green");
// digitalWrite(D3, HIGH); // Turn on LED on D3
// delay(500);
// digitalWrite(D3, LOW); // Turn off LED
// Serial.println("Blue");
// digitalWrite(D4, HIGH); // This line would cause an error unless D4 is defined
// delay(500);
// digitalWrite(D4, LOW);
mpu.getAccelerometerSensor()->getEvent(&event); // Read acceleration data
Serial.print("[");
Serial.print(millis()); // Print current time in milliseconds
Serial.print("] X: ");
Serial.print(event.acceleration.x); // Print X-axis acceleration
Serial.print(", Y: ");
Serial.print(event.acceleration.y); // Print Y-axis acceleration
Serial.print(", Z: ");
Serial.print(event.acceleration.z); // Print Z-axis acceleration
Serial.println(" m/s^2"); // Print units
delay(500); // Wait for half a second before next reading
}
To assist with embedded programming, I utilized AI coding tools to help write and debug the code for interfacing the ESP32-C3 with the MPU6050 sensor. These tools provided suggestions, reduced development time, and improved accuracy in setting up I2C communication, making the programming process more efficient and insightful.
Copyright @2025 © Uthaya Velraj. All Rights Reserved.
Designed by Myself