Embedded Programming Basics
Embedded Programming Basics¶
Understand the concepts of embedded systems and set up the Arduino development environment.
Learning Objectives¶
- Understand embedded system concepts
- Understand microcontrollers (MCU)
- Set up Arduino development environment
- Write and run first program
Prerequisites¶
- C language basic syntax
- Functions and variables
1. What is an Embedded System?¶
Definition¶
An Embedded System is a computer system designed to perform specific functions.
General Computer:
βββββββββββββββββββββββββββββββββββββββ
β Can run various programs β
β Web browser, games, documents, etc.β
β User has free control β
βββββββββββββββββββββββββββββββββββββββ
Embedded System:
βββββββββββββββββββββββββββββββββββββββ
β Performs only specific purposes β
β Washing machine, microwave, car ECUβ
β Dedicated hardware + software β
βββββββββββββββββββββββββββββββββββββββ
Embedded Systems Around Us¶
| Field | Examples |
|---|---|
| Home Appliances | Washing machine, refrigerator, air conditioner, microwave |
| Automotive | ECU, ABS, airbag, navigation |
| Medical Devices | Blood pressure monitor, thermometer, MRI, insulin pump |
| Communication | Router, smartphone, set-top box |
| Industrial | Factory automation, robots, PLC |
| IoT | Smart home, wearables, sensors |
Characteristics of Embedded Systems¶
1. Limited Resources
- Small memory (KB ~ MB)
- Slow CPU (MHz range)
- Limited storage
- Low power consumption
2. Real-time Requirements
- Must respond within specific time
- Example: Airbag must deploy within tens of ms after crash detection
3. Reliability
- Stable operation 24/7/365
- Errors can have critical consequences
4. Dedicated Hardware
- Design optimized for specific purpose
2. Microcontroller (MCU)¶
MCU vs MPU¶
MPU (Microprocessor Unit):
βββββββββββββββββββββββββββββββββββββββ
β Contains only CPU core β
β Requires external RAM, ROM, I/O β
β Example: Intel Core, AMD Ryzen β
β High performance, general computing β
βββββββββββββββββββββββββββββββββββββββ
MCU (Microcontroller Unit):
βββββββββββββββββββββββββββββββββββββββ
β CPU + RAM + ROM + I/O integrated β
β One Chip Solution β
β Example: ATmega328, STM32, ESP32 β
β Low power, specific purpose β
βββββββββββββββββββββββββββββββββββββββ
MCU Internal Structure¶
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β MCU β
β βββββββββββ βββββββββββ βββββββββββ β
β β CPU β β Flash β β SRAM β β
β β Core β β(Program)β β(Variablesβ β
β β β β β β Stack) β β
β βββββββββββ βββββββββββ βββββββββββ β
β β
β βββββββββββ βββββββββββ βββββββββββ β
β β GPIO β β Timer β β UART β β
β β(Digital β β(Timer/ β β(Serial β β
β β I/O) β β PWM) β β Comm) β β
β βββββββββββ βββββββββββ βββββββββββ β
β β
β βββββββββββ βββββββββββ βββββββββββ β
β β ADC β β I2C β β SPI β β
β β(Analog β β (Bus β β (High β β
β β Input) β β Comm) β β Speed) β β
β βββββββββββ βββββββββββ βββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββ
Main Memory Types¶
| Memory | Characteristics | Usage |
|---|---|---|
| Flash | Non-volatile, fast read, slow write | Program code storage |
| SRAM | Volatile, fast read/write | Variables, stack, heap |
| EEPROM | Non-volatile, byte-level write | Configuration storage |
3. Introduction to Arduino¶
What is Arduino?¶
Arduino is an open-source hardware platform designed to make embedded development easy to start.
Arduino Components:
βββββββββββββββββββββββββββββββββββββββ
β 1. Hardware (Board) β
β - ATmega328P MCU β
β - USB connection β
β - Power circuit β
β - Pin headers β
βββββββββββββββββββββββββββββββββββββββ€
β 2. Software (IDE) β
β - Code editor β
β - Compiler β
β - Upload tool β
βββββββββββββββββββββββββββββββββββββββ€
β 3. Libraries β
β - Sensors, motors, displays β
β - Rich examples β
βββββββββββββββββββββββββββββββββββββββ
Main Arduino Boards¶
| Board | MCU | Flash | SRAM | Pins | Features |
|---|---|---|---|---|---|
| Uno | ATmega328P | 32KB | 2KB | 14+6 | Most basic, beginner |
| Nano | ATmega328P | 32KB | 2KB | 14+8 | Small, breadboard |
| Mega | ATmega2560 | 256KB | 8KB | 54+16 | Many pins, large projects |
| Leonardo | ATmega32U4 | 32KB | 2.5KB | 20+12 | USB HID support |
Arduino Uno Pin Layout¶
βββββββββββββββββββββββ
β USB Port β
βββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββ
β AREF GND 13 12 11 10 9 8 β
β [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] β β Digital pins
β β
β βββββββ β
β β β ATmega328P β
β β β β
β βββββββ β
β β
β [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] β
β RESET 3.3V 5V GND GND Vin β β Power
β β
β [ ] [ ] [ ] [ ] [ ] [ ] β
β A0 A1 A2 A3 A4 A5 β β Analog pins
βββββββββββββββββββββββββββββββββββββββββββββ
Digital pins (0~13):
- 0, 1: Serial (TX, RX) - Serial communication
- 3, 5, 6, 9, 10, 11: PWM capable (~ marked)
- 13: Built-in LED connected
Analog pins (A0~A5):
- Analog input (ADC)
- Can also be used as digital pins
4. Development Environment Setup¶
Method 1: Arduino IDE Installation (For Real Hardware)¶
Windows / macOS¶
- Visit https://www.arduino.cc/en/software
- Download version for your OS
- Run installer
macOS (Homebrew)¶
brew install --cask arduino-ide
Linux (Ubuntu/Debian)¶
# Method 1: apt
sudo apt update
sudo apt install arduino
# Method 2: Flatpak
flatpak install flathub cc.arduino.IDE2
Method 2: Wokwi Simulator (Learning Without Hardware)¶
Wokwi is a free tool to simulate Arduino in your browser.
- Visit https://wokwi.com
- Click "Start Creating"
- Select "Arduino Uno"
- Start coding immediately!
Wokwi Advantages:
- Free, no installation required
- Various component simulation (LED, button, sensors, etc.)
- Circuit diagram visualization
- Code sharing
- Real-time debugging
Method 3: VS Code + PlatformIO (Advanced)¶
- Install VS Code
- Install PlatformIO extension
- Select Arduino Uno when creating new project
# Install PlatformIO CLI (optional)
pip install platformio
5. First Program: Blink¶
Arduino Program Structure¶
// Basic Arduino program structure
// Global variables, constants
const int LED_PIN = 13;
// setup(): Runs once at program start
void setup() {
// Initialization code
pinMode(LED_PIN, OUTPUT);
}
// loop(): Runs infinitely after setup()
void loop() {
// Repeatedly executed code
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
Comparison with Standard C¶
// Standard C program
int main(void) {
// Initialize
init_hardware();
// Infinite loop
while (1) {
// Repeated execution
do_something();
}
return 0; // Actually never reached
}
// Arduino program (same structure)
void setup() {
// Initialize (beginning of main)
}
void loop() {
// Same as inside while(1)
}
// Arduino framework provides main():
// int main() {
// setup();
// while(1) loop();
// }
Blink Example Detailed Explanation¶
// blink.ino - LED blinking
// Pin number where LED is connected (Arduino Uno built-in LED)
const int LED_PIN = 13;
void setup() {
// Set pin mode
// OUTPUT: Output mode (sends voltage)
// INPUT: Input mode (reads voltage)
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Turn LED on
// HIGH = 5V (or 3.3V) output
digitalWrite(LED_PIN, HIGH);
// Wait 1000 milliseconds (1 second)
delay(1000);
// Turn LED off
// LOW = 0V (GND) output
digitalWrite(LED_PIN, LOW);
// Wait 1 second
delay(1000);
// When loop() ends, it starts again from the beginning
}
Running on Wokwi¶
- Visit https://wokwi.com
- "New Project" β Select "Arduino Uno"
- Enter code:
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // LED_BUILTIN = 13
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
- Click green "Start Simulation" button
- Confirm that board's LED is blinking
6. Main Arduino Functions¶
Digital I/O¶
// Set pin mode
pinMode(pin, mode);
// mode: INPUT, OUTPUT, INPUT_PULLUP
// Digital output
digitalWrite(pin, value);
// value: HIGH (5V), LOW (0V)
// Digital input
int value = digitalRead(pin);
// Returns: HIGH or LOW
Time Related¶
// Wait milliseconds
delay(ms);
// Wait microseconds
delayMicroseconds(us);
// Time elapsed since program start (milliseconds)
unsigned long time = millis();
// Time elapsed since program start (microseconds)
unsigned long time = micros();
Serial Communication¶
// Initialize serial (typically 9600 or 115200)
Serial.begin(baudrate);
// Output data
Serial.print("Hello"); // Without newline
Serial.println("World"); // With newline
Serial.print(123); // Print number
// Input data
if (Serial.available() > 0) {
char c = Serial.read();
}
7. Practice Project: Various Blink Patterns¶
Project 1: Variable Speed Blink¶
// LED that gets faster
const int LED_PIN = 13;
int delayTime = 1000; // Starting delay
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(delayTime);
digitalWrite(LED_PIN, LOW);
delay(delayTime);
// Decrease delay (minimum 50ms)
delayTime -= 50;
if (delayTime < 50) {
delayTime = 1000; // Reset
}
}
Project 2: SOS Signal¶
// Morse code SOS (... --- ...)
const int LED_PIN = 13;
const int DOT = 200; // Dot length
const int DASH = 600; // Dash length
const int GAP = 200; // Gap between signals
const int LETTER_GAP = 600; // Gap between letters
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void dot() {
digitalWrite(LED_PIN, HIGH);
delay(DOT);
digitalWrite(LED_PIN, LOW);
delay(GAP);
}
void dash() {
digitalWrite(LED_PIN, HIGH);
delay(DASH);
digitalWrite(LED_PIN, LOW);
delay(GAP);
}
void loop() {
// S: ...
dot(); dot(); dot();
delay(LETTER_GAP);
// O: ---
dash(); dash(); dash();
delay(LETTER_GAP);
// S: ...
dot(); dot(); dot();
delay(LETTER_GAP * 3); // Long gap between words
}
Project 3: Using millis() (Asynchronous Blink)¶
delay() stops the program, but using millis() allows other tasks to run.
// Blink LED without delay()
const int LED_PIN = 13;
unsigned long previousMillis = 0;
const long interval = 1000; // 1 second
int ledState = LOW;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
// Check if interval time has passed
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Toggle LED state
ledState = (ledState == LOW) ? HIGH : LOW;
digitalWrite(LED_PIN, ledState);
}
// Can do other tasks here!
// Example: Read sensors, check buttons, etc.
}
Project 4: Multiple LED Control¶
You can test by connecting external LEDs in Wokwi.
// Sequential lighting of 3 LEDs
const int LED1 = 11;
const int LED2 = 12;
const int LED3 = 13;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop() {
// Turn on only LED1
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(300);
// Turn on only LED2
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
delay(300);
// Turn on only LED3
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
delay(300);
}
8. Debugging with Serial Monitor¶
Basic Serial Output¶
void setup() {
Serial.begin(9600); // Start serial communication
Serial.println("Arduino started!");
}
void loop() {
static int count = 0;
count++;
Serial.print("Count: ");
Serial.println(count);
delay(1000);
}
Using Serial Monitor in Wokwi¶
- Start simulation
- Click "Serial Monitor" tab on the right
- Check output
Monitoring Variable Values¶
const int LED_PIN = 13;
int blinkCount = 0;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
Serial.println("=== Blink Counter ===");
}
void loop() {
blinkCount++;
digitalWrite(LED_PIN, HIGH);
Serial.print("LED ON - Count: ");
Serial.println(blinkCount);
delay(500);
digitalWrite(LED_PIN, LOW);
Serial.println("LED OFF");
delay(500);
}
Exercises¶
Exercise 1: Heartbeat LED¶
Create a pattern where the LED blinks twice quickly like a heartbeat, then pauses.
Exercise 2: Countdown¶
Output a countdown from 10 to 1 on the serial monitor, and when it reaches 0, blink the LED 3 times.
Exercise 3: Random Blink¶
Use the random() function to make the LED blink at irregular intervals.
// Hint
int randomDelay = random(100, 1000); // Random value between 100~999
Exercise 4: Binary Counter¶
Use 4 LEDs to display 0~15 in binary. - 0 = 0000 (all LEDs off) - 5 = 0101 (LED2, LED4 on) - 15 = 1111 (all LEDs on)
Key Concepts Summary¶
| Term | Description |
|---|---|
| Embedded System | Dedicated computer system for specific functions |
| MCU | Chip integrating CPU + memory + peripherals |
| GPIO | General Purpose Input/Output pins |
| Flash | Non-volatile memory for program code |
| SRAM | Volatile memory for variables |
| setup() | Initialization code (runs once) |
| loop() | Repeatedly executed code (infinite loop) |
Wokwi Project Link Example¶
You can run a basic Blink project directly on Wokwi: - https://wokwi.com/projects/new/arduino-uno
Next Steps¶
Once you've mastered Arduino basics, proceed to the next document: - 15. Advanced Bit Operations - Core embedded technology