Programming microcontrollers like the Arduino Uno (ATmega328P) or ESP32 opens the door to hardware automation. Arduino programs (called "sketches") are written in C/C++ and execute inside a continuous bare-metal execution environment without an operating system.

Anatomy of an Arduino Sketch: setup() and loop()

blink_serial.inocpp
// Built-in LED pin definition
const int LED_PIN = LED_BUILTIN; 
 
// Executed ONCE when board powers up or resets
void setup() {
    // Initialize GPIO pin as output
    pinMode(LED_PIN, OUTPUT);
    
    // Initialize serial communication at 115200 baud
    Serial.begin(115200);
    Serial.println("Arduino System Initialized successfully.");
}
 
// Executed CONTINUOUSLY in an infinite loop
void loop() {
    digitalWrite(LED_PIN, HIGH); // Turn LED on
    Serial.println("LED Status: ON");
    delay(1000);                 // Wait 1 second (1000 ms)
 
    digitalWrite(LED_PIN, LOW);  // Turn LED off
    Serial.println("LED Status: OFF");
    delay(1000);                 // Wait 1 second
}

Key Function Breakdown:

  • `pinMode(pin, mode)`: Configures a specific GPIO pin to act as an INPUT or OUTPUT.

  • `digitalWrite(pin, state)`: Sets pin output voltage to 5V/3.3V (HIGH) or 0V (LOW).

  • `Serial.begin(speed)`: Configures UART hardware baud rate for transmitting debug messages to the IDE Serial Monitor.