Introduction to Microcontrollers
Microcontroller Programming · Embedded Systems & Firmware track
4 min read
In this module
The Computers Inside Everything
Embedded Systems & Firmware told you embedded systems are everywhere. Now you go deeper. This module is about getting your hands dirty: actual microcontroller programming, code that runs directly on silicon, and the constraints that make embedded work feel nothing like application development.
Choosing a Microcontroller
Microcontrollers come in families defined by their CPU architecture, memory, peripherals, and power characteristics:
AVR (Atmel/Microchip): 8-bit RISC. The ATmega328P powers the Arduino Uno. Simple, well-documented, a solid teaching chip. Limited to tens of kilobytes of flash and a few kilobytes of RAM.
ARM Cortex-M: the dominant 32-bit MCU architecture. The family ranges from the tiny M0 (minimum area, minimum power) through M4 (DSP instructions, floating point) to M7 at the high-performance end. STM32, nRF52, LPC, and hundreds of other chip families are all Cortex-M underneath.
ESP32 (Espressif): dual-core Xtensa or RISC-V with WiFi and Bluetooth on-die. The IoT crowd favourite. More capable than traditional MCUs, at the cost of higher power consumption.
RISC-V MCUs: open-architecture upstarts from SiFive, GigaDevice (GD32V), and others. The open ISA carries no licensing fees, which makes a real difference in cost-sensitive applications.
MCU selection depends on: Performance (clock speed, instruction throughput), Memory (flash for code, RAM for data), Peripherals (what built-in hardware you need - ADC, USB, CAN, Ethernet), Power (battery life requirements), Cost (per-unit price at production volume), and Ecosystem (toolchain quality, documentation, community support).
Development Boards
Development boards make MCUs accessible by adding USB connectivity, debug interfaces, pin headers, and sometimes displays or sensors:
- Arduino boards: ATmega or ARM-based. Simplest entry point with the Arduino IDE and libraries.
- STM32 Discovery/Nucleo: ARM Cortex-M boards from ST. Professional development environment with STM32CubeIDE.
- ESP32 DevKit: WiFi/BT development boards. Arduino-compatible or native ESP-IDF framework.
- Raspberry Pi Pico: RP2040 dual-core ARM Cortex-M0+. Low cost with good documentation.
- Nordic nRF52 DK: Bluetooth Low Energy development with ARM Cortex-M4.
Cross-Compilation Toolchains
The MCU cannot compile its own code. There's nowhere near enough memory, and no operating system to host a compiler anyway. You build on your development machine instead, using a cross-compiler that emits code for the target architecture:
# ARM cross-compilation toolchainarm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -c startup.s -o startup.oarm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -c main.c -o main.o# startup.o carries the vector table and reset handler. Plain ld adds no# C runtime start files; linking through gcc would need -nostartfiles.arm-none-eabi-ld -T linker_script.ld startup.o main.o -o firmware.elfarm-none-eabi-objcopy -O binary firmware.elf firmware.bin
# Flash to the deviceopenocd -f interface/stlink.cfg -f target/stm32f4x.cfg \ -c "program firmware.bin 0x08000000 verify reset exit"The linker is where the memory map enters the picture: only the .elf knows where each section lives. Run arm-none-eabi-size firmware.elf to see the split. .text and the initial values of .data end up in firmware.bin; .bss takes up RAM at run time but no bytes in the image, because it's just zeros.
The arm-none-eabi prefix is a target triplet: ARM architecture, none in the vendor slot, and EABI (Embedded Application Binary Interface) in the OS/ABI slot. With no OS named, it means bare metal. The -mthumb flag asks for Thumb-2 instructions, the compact encoding Cortex-M uses.
The Embedded Development Workflow
Application development is a tight loop: compile, run, see output. Embedded work bolts on a few extra steps:
- Write code on your development machine
- Cross-compile for the target architecture
- Flash the binary onto the MCU's flash memory through JTAG, SWD, or a bootloader
- Debug using a hardware debugger (like ST-Link or J-Link) connected to JTAG/SWD pins
- Observe behaviour through debug prints (UART), LED blinks, oscilloscope, or logic analyser
There's no operating system standing by to catch segfaults or print stack traces. A bug might show up as a silent lock-up, a wrong output on a wire, or an unrecoverable state that needs a power cycle to clear. Hardware debugging tools earn their keep here.
On your laptop, a null pointer dereference gives you a segfault and a core dump. On an MCU, it might silently corrupt memory, trigger a hard fault, or reset the device. There is no virtual memory to catch wild pointers, no ASLR, and usually no memory protection. Every pointer operation must be correct because there is nothing to catch your mistakes.