Microcontrollers

A microcontroller is a complete computer on one chip, built to control hardware. Learn its architecture — CPU, memory, clock, GPIO, timers, ADC, communication peripherals, interrupts and DMA — and how to budget power for battery devices.

A microcontroller (MCU) combines a processor core, flash memory for the program, RAM for data, and a rich set of peripherals — digital I/O pins, timers, analogue-to-digital converters, serial interfaces — on a single chip that costs anywhere from cents to a few dollars.

MCUs are inside washing machines, cars (dozens per vehicle), inverters, meters, drones and every IoT device. They read sensors, run control algorithms and drive actuators, often with hard real-time deadlines and tiny power budgets.

An analogy

An MCU is like a small factory: the CPU is the manager, flash is the instruction manual, RAM is the workbench, peripherals are specialised machines (timers, ADCs, UARTs) that work in parallel, and interrupts are the phone that rings when something needs attention.

What's inside

These blocks appear in almost every MCU.

GPIO electrical limits

Pins run at the MCU supply (3.3 V or 5 V) and can typically source/sink 4–25 mA each, with a total package limit. Driving LEDs directly is fine with a series resistor; relays, motors and solenoids need transistors or drivers. Inputs must never exceed the supply rail unless marked 5 V-tolerant.

Interrupts, timers and DMA

Interrupts let hardware events (a timer overflow, a byte received, a pin change) pre-empt the main loop immediately. Timers generate precise periodic events and PWM without CPU involvement. DMA moves data between peripherals and memory (ADC samples, UART buffers) while the CPU does other work.

Low-power design

Battery devices spend > 99 % of their time asleep. MCUs offer sleep, stop and standby modes drawing µA or nA, waking on timers or pins. Battery life depends on average current, so minimise wake time, run fast then sleep, and power down sensors between readings.

Choosing an MCU and the real-time view

Selection weighs core performance, memory, peripherals (e.g. motor-control timers, high-resolution ADCs, CAN-FD), package, power, toolchain and supply. Real-time behaviour depends on worst-case interrupt latency and execution time, not average speed; an RTOS helps structure complex firmware into prioritised tasks.

Formulas

Worked example: 1-second timer

Given: f_clk = 72 MHz; PSC = 7199, ARR = 9999

Find: Tick rate and interrupt period.

Answer: 10 kHz tick, 1 Hz interrupt

Worked example: Reading an ADC

Given: 12-bit ADC, V_ref = 3.3 V; Code = 2482

Find: Input voltage.

Answer: 2.00 V

Toggling an LED from a timer interrupt (STM32-style C)

#include "stm32f1xx.h"

void TIM2_IRQHandler(void) {
    if (TIM2->SR & TIM_SR_UIF) {      // update (overflow) event?
        TIM2->SR &= ~TIM_SR_UIF;      // clear the flag
        GPIOC->ODR ^= (1u << 13);     // toggle PC13 (on-board LED)
    }
}

int main(void) {
    RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;   // clock GPIOC
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;   // clock TIM2
    GPIOC->CRH = (GPIOC->CRH & ~(0xFu << 20)) | (0x2u << 20); // PC13 output 2 MHz

    TIM2->PSC = 7200 - 1;     // 72 MHz / 7200 = 10 kHz tick
    TIM2->ARR = 10000 - 1;    // 10 000 ticks = 1 s
    TIM2->DIER |= TIM_DIER_UIE;
    NVIC_EnableIRQ(TIM2_IRQn);
    TIM2->CR1 |= TIM_CR1_CEN; // start

    for (;;) { __WFI(); }     // sleep until the next interrupt
}

The timer runs in hardware; the CPU sleeps (WFI) and wakes only for the 1 Hz interrupt — the core pattern of efficient firmware.

Common mistakes

Applications

Key takeaways

Going further