Microcontroller
A whole computer on one chip, with timers and converters built in to touch the real world.
A microcontroller is a processor core, program memory, working memory and a set of peripherals on a single chip costing very little. The peripherals are the clever part: timers that make precise pulses, converters that turn voltages into numbers, and pins that switch things on and off, all running while the core sleeps.
- 8 components
- 2 worked equations
- 1 live model
- 2 safe experiments
1Exploded view
Take the microcontroller apart.
Tab to a numbered part (or tap it) to jump to its description. The same parts as a list:
2Major components
What every part does, and how.
- 01
Package
The black plastic body with metal pins that protects the silicon die.
how →Thin bond wires connect pads on the die to the pins; the package also spreads heat.
- 02
Processor core
Fetches and executes instructions from flash, one after another.
how →Typically a 32-bit core running at tens to hundreds of megahertz, with interrupts that let peripherals call for attention.
- 03
Flash memory
Holds the program, even with the power off.
how →Charge trapped on an insulated gate sets each bit; it survives for years without power.
- 04
SRAM
Working memory for variables and the stack.
how →Each bit is a small latch of transistors: fast, but lost when power goes.
- 05
Analogue-to-digital converter
Turns a pin voltage into a number.
how →A successive-approximation converter tries one bit at a time, from the most significant down, comparing against fractions of a reference voltage.
- 06
Timers and PWM
Count clock ticks to make precise delays, measure pulses and generate PWM.
how →A counter runs from 0 to TOP and resets; the output pin flips when the count passes a compare value.
- 07
GPIO and serial
Pins that read buttons, drive LEDs and talk to other chips over UART, I²C or SPI.
how →Each pin is a register bit; writing it sets the voltage, reading it samples the voltage.
- 08
Die and lead frame
The silicon die sits on a metal pad; the lead frame becomes the pins.
how →Everything above is drawn on one piece of silicon a few millimetres across.
3System relationships
Where the energy and information go.
Each block is a part (or the world outside). Solid lines carry energy, dashed lines carry information, dotted lines carry matter such as water or air.
Flows, in order
- Sensor voltage sends information to ADCinformation: voltage
- ADC sends information to Coreinformation: counts (interrupt)
- Flash sends information to Coreinformation: instructions
- Core sends information to SRAMinformation: read / write
- Core sends information to Timers + PWMinformation: duty cycle
- Core sends information to GPIO + serialinformation: pin states
- Supply sends energy to Coreenergy: supply
- Timers + PWM sends energy to Pinsenergy: PWM out
- GPIO + serial sends information to Pinsinformation: signals
4Physical principles
The physics doing the work.
Sampling and quantisation
An ADC measures at instants (sampling) and rounds to one of 2ⁿ levels (quantisation). Both limit what the program can know.
Timers and PWM
Switching a pin on and off fast, and varying the fraction of time it is on, gives an average voltage that motors and LEDs respond to.
Interrupts
Peripherals raise interrupts so the core can sleep until something happens, saving most of its energy.
5Mathematical foundations
The equations, with real numbers in them.
PWM frequency and resolution
f_PWM = f_clk / (p · (TOP + 1)), bits = log₂(TOP + 1)
- f_clk
- timer clock [Hz]
- p
- prescaler [–]
- TOP
- counter top value [counts]
Worked example
- 1.f_PWM = 16 000 000 / (8 × 2000) = 1000 Hz
- 2.bits = log₂(2000) ≈ 10.97
- PWM frequency
- 1,000 Hz
- Duty-cycle resolution
- 10.97 bits
AssumesA simple up-counting timer that resets at TOP.
ADC code and step size
code = ⌊V_in / V_ref · 2ⁿ⌋, LSB = V_ref / 2ⁿ
- V_in
- input voltage [V]
- V_ref
- reference voltage [V]
- n
- resolution [bits]
Worked example
- 1.code = ⌊1.65 / 3.3 × 4096⌋ = 2048
- 2.LSB = 3.3 / 4096 = 0.000806 V ≈ 0.806 mV
- Code
- 2,048 counts
- Step size
- 0.806 mV
AssumesAn ideal converter with no offset, gain error or noise.
6Computing architecture
The computer inside the system.
A Harvard-style core: instructions come from flash while data lives in SRAM, and peripherals are controlled by writing to memory-mapped registers.
Layer 1
Firmware
A super-loop or a small real-time operating system, plus interrupt handlers.
Layer 2
Peripheral registers
Each timer, ADC and serial port is configured by writing bits at fixed addresses.
Layer 3
Clock and power
Clock trees can be gated off per peripheral; sleep modes cut current to microamps.
7Engineering trade-offs
Every choice costs something.
Higher PWM frequency
Gain:Smoother current, inaudible to people.
Cost:Fewer counts per period, so coarser duty-cycle steps, and more switching loss.
More ADC bits
Gain:Finer voltage steps.
Cost:Slower conversions and noise may swamp the extra bits anyway.
Sleep between tasks
Gain:Battery life measured in months or years.
Cost:Wake-up delay, and more careful firmware design.
8 · Try it · runs in your browser
Tune a PWM timer
Pick a clock, prescaler and TOP value. See the PWM frequency and how many bits of duty-cycle resolution you get, and find the trade-off.
- PWM frequency
- 1,000.0 Hz
- Duty-cycle resolution
- 10.97 bits
- Period
- 1,000.0 µs
Fine for LEDs; may whine in a motor.
ModelUp-counting timer that resets at TOP; real timers also offer centre-aligned modes that halve the frequency.
Go further with a related simulation: Battery sensor node: sample, sleep, send
9Suggested safe experiments
Things to try, safely.
Every experiment here is virtual or a household-safe observation. Never open real devices, batteries or power supplies, never work on mains electricity, and never look at the Sun.
Observation
PWM you can see
- Look at a dimmed LED light through a phone camera in slow-motion mode.
- Many dimmed LEDs flicker on camera even though they look steady to you.
- Explain it with PWM frequency and the camera's frame rate.
Safety: Look at ordinary household lamps from a normal distance; do not open light fittings.
Virtual
Count the steps
- In the demonstration, set a 16 MHz clock and prescaler 1.
- Find the largest TOP that still gives at least 20 kHz.
- How many bits of resolution remain?
Safety: Runs entirely in your browser.
10Keep learning
Lessons, courses and videos.
Lessons
Courses
Concepts in the Atlas
- Inside a microcontroller
- Timers and pulse-width modulation
- Analog-to-digital conversion
- Interrupts and event-driven code
- Serial communication: UART, I2C and SPI
Field Guide
Videos (open on YouTube)
- What is a Microcontroller? (opens in a new tab)Microchip Technology, Inc. · Length: see YouTube
- ABCs of ADCs - What is an Analog to Digital Converter (ADC) (opens in a new tab)Microchip Technology, Inc. · Length: see YouTube
- PWM - Pulse Width Modulation (Basics, Circuit, Working, & Waveforms) Explained (opens in a new tab)Engineering Funda · Length: see YouTube