Microcontrollers: CPU, Memory and Peripherals
Understand what runs inside a microcontroller and how firmware turns sensor inputs into useful outputs.
Think of a small workshop: the CPU follows instructions, memory holds plans and measurements, and peripherals are dedicated machines that keep working between CPU visits.
A microcontroller combines a processor, memory and peripheral circuits on one chip. The processor executes instructions from a program. Flash typically retains the program when power is removed, while RAM holds changing values such as samples, counters and temporary results. A reset starts execution from a defined entry sequence; it does not resume wherever the previous run stopped.
Peripherals connect software to the physical world. GPIO reads logic levels, an ADC converts voltage into a digital code, and timers count clock ticks. A hardware timer can generate pulses without executing one software instruction per edge. This distinction explains why a busy CPU may delay a software action while a correctly configured hardware waveform continues.
In a simple superloop, initialize the hardware once, then repeatedly read inputs, update state and write outputs. Each part must finish soon enough for the next task to run. A blocking sensor read can delay button detection or motor control even if the processor is otherwise fast. Start by listing the required response times, not just the processor clock speed.
Board names and chip names are different. A development board adds a power regulator, programming connection and pin headers around the microcontroller. Check the actual chip, supply voltage, memory size and pin capabilities before moving code between boards. A pin that supports PWM or interrupts on one board may not provide the same function on another.
Understand what runs inside a microcontroller and how firmware turns sensor inputs into useful outputs.
Core ideas
- The CPU executes instructions; peripherals perform specialized hardware work.
- Flash and RAM serve different purposes.
- Choose a sampling and response schedule before writing the main loop.
Sample interval = 1 / sample rate200 samples/s → 5 ms per sampleWork through a practical example
1A temperature logger samples at 200 Hz, so a new sample is due every 5 ms.
2Reserve RAM for a buffer: 100 samples stored as 16-bit values require 200 bytes for sample data.
3Convert the reading, update the display less frequently, and transmit buffered data when the interface is ready.
4Measure the worst-case loop time. If a display update takes 12 ms, it cannot run as a blocking operation in every 5 ms sampling cycle.
Assuming a faster clock automatically solves blocking code, insufficient RAM or incorrect voltage levels.