Microcontroller Architecture
CPU, memory, registers, interrupts, and peripherals
Skills taught: embedded.mcu-arch
Inside a Microcontroller
A microcontroller packages everything a computer needs onto a single chip. The ATmega328P inside an Arduino Uno is a good reference: 8-bit AVR CPU running at 16 MHz, 32 KB of flash (program storage), 2 KB of SRAM (runtime variables), and 1 KB of EEPROM (persistent small data).
ATmega328P — Arduino Uno Architecture
──────────────────────────────────────
CPU: 8-bit AVR @ 16 MHz (most instructions 1 clock cycle)
Flash: 32 KB — stores your compiled program (non-volatile)
SRAM: 2 KB — runtime stack + heap + globals (volatile)
EEPROM: 1 KB — survives power-off, ~100k write cycles
Timers: 3 (Timer0 8-bit, Timer1 16-bit, Timer2 8-bit)
ADC: 10-bit, 6 channels (A0-A5)
PWM: 6 channels (pins 3,5,6,9,10,11)
UART: 1 hardware (pins 0,1 — shared with USB)
I2C: 1 (pins A4=SDA, A5=SCL)
SPI: 1 (pins 10=SS, 11=MOSI, 12=MISO, 13=SCK)
Interrupt pins: 2 hardware (pins 2, 3), all pins via PCINT
The Three Memory Types
- Flash (Program Memory)
- Non-volatile storage for your compiled program. The ATmega328P has 32 KB. It survives power-off, but can only be written ~10,000 times (don't write to it at runtime). Your .ino sketch compiles to AVR machine code stored here.
- SRAM (Static RAM)
- Volatile runtime memory for variables, the call stack, and heap. The ATmega328P has 2 KB — enough for about 500 integers. It disappears when power is removed. SRAM overflow is the #1 cause of mysterious Arduino crashes.
- EEPROM
- 1 KB of non-volatile storage for small persistent values (calibration data, device ID, WiFi credentials on larger MCUs). Limited to ~100,000 write cycles — don't write to it in a tight loop.
#include <EEPROM.h>
#include <avr/pgmspace.h>
// BAD: This string eats SRAM
const char msg1[] = "Booting sensor system v1.0.0...";
// GOOD: Store string in flash with F() macro
// void setup() { Serial.println(F("Booting sensor system v1.0.0...")); }
// Store a constant table in flash (not SRAM) with PROGMEM
const PROGMEM uint8_t LOOKUP[16] = {0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225};
// Read EEPROM — persistent config
void loadConfig() {
byte deviceId = EEPROM.read(0); // address 0
if (deviceId == 0xFF) { // 0xFF = unprogrammed
deviceId = 42;
EEPROM.write(0, deviceId); // write once
}
Serial.print("Device ID: "); Serial.println(deviceId);
}
// Check free SRAM
int freeMemory() {
extern int __heap_start, *__brkval;
int v;
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
}
void setup() {
Serial.begin(115200);
loadConfig();
Serial.print("Free SRAM: "); Serial.println(freeMemory());
}
Warning: On Arduino Uno, SRAM is only 2 KB. String concatenation, large arrays, and recursive functions eat SRAM fast. Use F("literal") for Serial.print strings, and PROGMEM for constant tables. Watch free SRAM with freeMemory() — below 200 bytes, your sketch will crash unpredictably.
Interrupts: Responding to Events Instantly
An interrupt is a hardware signal that pauses the main program, runs an ISR (Interrupt Service Routine), then resumes exactly where it left off. This lets you respond to button presses, timer ticks, or serial data instantly — without polling. The ATmega328P has two external interrupt pins (2 and 3) and 23 internal interrupt sources.
volatile bool buttonPressed = false; // volatile: modified in ISR
// ISR — keep it SHORT, no Serial, no delay
void IRAM_ATTR onButtonPress() {
buttonPressed = true; // just set a flag
}
void setup() {
Serial.begin(115200);
pinMode(2, INPUT_PULLUP);
// Attach interrupt: pin 2, ISR, trigger on FALLING edge
attachInterrupt(digitalPinToInterrupt(2), onButtonPress, FALLING);
}
void loop() {
if (buttonPressed) {
buttonPressed = false; // clear flag
Serial.println("Button pressed!");
}
// Other work happens here uninterrupted
}
Tip: ISRs must be as short as possible — set a flag, then handle it in loop(). Never call Serial, delay(), or malloc() inside an ISR. Use the `volatile` keyword for any variable shared between an ISR and main code.
Polling vs Interrupts
- Polling: Continuously check a pin or register in the main loop
- Interrupts: Hardware notifies CPU when event occurs
Frequently Asked Questions
What happens when SRAM runs out?
Stack and heap collide, causing random crashes or data corruption. This is a common Arduino bug. Monitor free SRAM with freeMemory() — keep at least 200 bytes free. Use the F() macro for string literals.
How many interrupts can Arduino Uno handle simultaneously?
Only one ISR executes at a time. If a second interrupt fires while an ISR is running, it queues until the first ISR returns. For this reason, keep ISRs extremely short.
What is the difference between EEPROM and flash?
Both are non-volatile (survive power-off). Flash stores your program and can hold 32 KB; EEPROM is a tiny (1 KB) byte-addressable area for persistent config data. EEPROM has ~100,000 write cycles; flash supports ~10,000 full erase cycles.