Introduction to Microcontrollers
A microcontroller (MCU) is a self-contained computer on a single integrated circuit: processor core, flash memory (program storage), SRAM (working memory) and programmable I/O peripherals — all on one chip. Where a CPU (or SBC like Raspberry Pi) runs a full OS, an MCU typically runs a single program loop directly on the hardware, with response times measured in microseconds. This determinism makes MCUs ideal for real-time control, embedded sensing and battery-powered IoT.
MCU vs Microprocessor vs SBC
A microprocessor (MPU) is just the CPU core — it needs external RAM, Flash, power regulation and peripherals to function. A microcontroller integrates all of these on one chip. A Single-Board Computer (SBC like Raspberry Pi) has a powerful MPU, runs Linux, connects to screens and networks, but costs more, draws more power and boots in seconds. MCUs cost ¢25–$5, boot in microseconds, run on button batteries and control hardware in real time. The right tool depends on the application.
| Type | Example | OS | Boot Time | Power | Best For |
|---|---|---|---|---|---|
| MCU | STM32F103 | None (bare-metal) | < 1 ms | 10–500 mW | Real-time control, IoT |
| MCU (RTOS) | ESP32-S3 | FreeRTOS | < 100 ms | 50–500 mW | IoT with Wi-Fi/BT |
| MPU + SBC | Raspberry Pi 5 | Linux | 10–20 s | 5–25 W | Edge AI, Linux apps |
GPIO, Timers and Interrupts
GPIO (General Purpose Input/Output) pins can be configured as digital input, digital output or alternate-function (SPI, I2C, UART, PWM). As an output, setting the pin high drives it to Vcc (3.3V typically), low to GND. As an input, the pin reads an external voltage and returns 0 or 1. Internal pull-up and pull-down resistors prevent floating inputs from reading noise. Timers are hardware counters that tick at a programmable rate; they generate interrupts or PWM signals independently of the main loop. Interrupts are hardware events that immediately suspend the main program and run a handler function — essential for real-time response (button press detected in microseconds).
- ▸Never directly source/sink more than the pin's rated current (typically 8–25 mA). Use a transistor or motor driver for heavier loads.
- ▸ADC (Analog-to-Digital Converter) pins sample analog voltages (0 to Vcc) and convert them to a digital number.
- ▸DAC (Digital-to-Analog Converter) pins output a programmed analog voltage level.
Communication Protocols: UART, SPI, I2C
UART (Universal Asynchronous Receiver-Transmitter) is the simplest: two wires (TX, RX), asynchronous, point-to-point. Used for debug consoles, GPS modules, Bluetooth serial. SPI (Serial Peripheral Interface) is synchronous, full-duplex, 4 wires (MOSI, MISO, SCK, CS). Very fast (up to 50+ MHz). Used for high-speed displays, SD cards, flash chips. One CS line per device limits scalability. I2C (Inter-Integrated Circuit) is synchronous, half-duplex, 2 wires (SDA, SCL). Addressable — many devices share the same bus, each with a unique 7-bit address. Slower than SPI (400 kHz standard, 3.4 MHz high-speed). Used for sensors, EEPROMs, real-time clocks.