Skip to content
Tactiverge

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.

plate 08 · microcontroller, isometric, exploded

Tab to a numbered part (or tap it) to jump to its description. The same parts as a list:

  1. 1Package
  2. 2Processor core
  3. 3Flash memory
  4. 4SRAM
  5. 5Analogue-to-digital converter
  6. 6Timers and PWM
  7. 7GPIO and serial
  8. 8Die and lead frame

2Major components

What every part does, and how.

  1. 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.

  2. 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.

  3. 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.

  4. 04

    SRAM

    Working memory for variables and the stack.

    how →Each bit is a small latch of transistors: fast, but lost when power goes.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

Sensor voltageADCFlashprogramSupply≈ 3.3 VCoreSRAMvariablesTimers + PWMGPIO + serialPinsthrough the package
Energy (solid)Information (dashed)Matter (dotted) Source Sense Decide Convert Store Output

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.

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. 1.f_PWM = 16 000 000 / (8 × 2000) = 1000 Hz
  2. 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. 1.code = ⌊1.65 / 3.3 × 4096⌋ = 2048
  2. 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.

  1. Layer 1

    Firmware

    A super-loop or a small real-time operating system, plus interrupt handlers.

  2. Layer 2

    Peripheral registers

    Each timer, ADC and serial port is configured by writing bits at fixed addresses.

  3. 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.

16 MHz
÷8
1,999 counts
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.

31,250.00.06365,535Counter TOP (counts)Hz

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.

  1. Observation

    PWM you can see

    1. Look at a dimmed LED light through a phone camera in slow-motion mode.
    2. Many dimmed LEDs flicker on camera even though they look steady to you.
    3. 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.

  2. Virtual

    Count the steps

    1. In the demonstration, set a 16 MHz clock and prescaler 1.
    2. Find the largest TOP that still gives at least 20 kHz.
    3. How many bits of resolution remain?

    Safety: Runs entirely in your browser.