Skip to content
Tactiverge

Inside a microcontroller

The processor core, flash, RAM, peripherals and clock packed onto one small chip, what happens when it powers up, and how it differs from the computer on your desk.

Lesson 1 of 6 · 25 min · Self-paced · Free

A computer you never see

A robot gripper has to read its force sensor hundreds of times per second, decide whether the grip is tight enough, and adjust a motor, without ever freezing, rebooting for updates or waiting for a mouse click. The device doing that job is almost never a laptop. It is a microcontroller: a complete, small computer on a single chip, often smaller than a fingernail and cheap enough to put inside a toothbrush.

Microcontrollers are everywhere: in washing machines, keyboards, thermostats and servo-driven robot joints. You already know from the digital electronics module how bits, gates and flip-flops behave. A microcontroller is what you get when you organise enormous numbers of them with purpose.

The five main parts

Every microcontroller, whichever company makes it, contains the same basic building blocks.

1. The CPU core. The processor runs your program one instruction at a time, endlessly repeating a cycle: fetch the next instruction from memory, decode what it means, execute it. Instructions are tiny steps such as "add these two numbers" or "jump back to line 12 if the result was zero". The core holds a few registers, very fast storage slots built from flip-flops, where it keeps the numbers it is working on right now. Cores are described by how many bits they handle at once: 8-bit, 16-bit and 32-bit cores are all common.

2. Flash memory. This is where your program lives. Flash is non-volatile: it keeps its contents when the power is off, which is why a microcontroller runs the same program every time you switch it on. Typical sizes range from a few kilobytes to a few megabytes.

3. RAM. Random-access memory holds the variables your program changes while running: sensor readings, counters, the current state of a motor. RAM is volatile: everything in it vanishes when the power goes. It is also small, often between 1 KB and a few hundred KB. Your laptop has around a million times more.

4. Peripherals. These are dedicated hardware blocks that handle jobs the CPU would be slow or clumsy at:

  • GPIO (general-purpose input/output) pins that can be switched high or low, or read
  • Timers that count clock pulses, measure time and generate signals
  • ADCs (analog-to-digital converters) that measure voltages
  • Communication blocks such as UART, I2C and SPI for talking to sensors and other chips
  • PWM outputs for dimming lights and controlling motor speed

You will meet most of these later in this module. The important idea is that peripherals work in parallel with the CPU. A timer keeps counting while the CPU does arithmetic.

5. The clock. A clock signal, a square wave, steps the whole chip forward. It might come from a small quartz crystal outside the chip (accurate) or from an internal oscillator (cheaper, less accurate). Clock speeds are usually measured in megahertz (MHz), millions of cycles per second.

Worked example: how much time is a clock cycle?

Suppose a microcontroller runs at 16 MHz. One cycle takes:

1 / 16 000 000 Hz = 0.0000000625 s = 62.5 ns

If your control loop must run 1,000 times per second (1 kHz), each pass has 1 ms available. In cycles:

16 000 000 cycles/s × 0.001 s = 16 000 cycles per loop

If simple instructions take one or two cycles, that is several thousand instructions per loop: plenty for reading a sensor and some arithmetic, nowhere near enough for processing a camera image. Quick budgets like this are a core embedded habit.

How the CPU talks to peripherals

Here is a neat trick. Peripherals are controlled through special memory locations called registers (a second meaning of the word). The CPU writes a number to a particular address, and instead of storing it like RAM, the hardware reacts: a pin goes high, a timer starts. Reading another address tells you whether a button pin is high or low. This is called memory-mapped I/O. Each chip's datasheet lists these addresses and what each bit means. You will learn to read those register descriptions in the programming foundations module.

What happens at power-up

When power arrives, a reset circuit holds the chip still until the voltage is stable. Then the CPU reads a fixed location in flash, the reset vector, which tells it where the start-up code is. That start-up code sets up the stack, copies the starting values of variables into RAM, sets the rest to zero, and calls your main function. There is no operating system to return to, so a microcontroller program normally ends in a loop that runs forever. Power off, power on, and the whole sequence repeats exactly.

How it differs from your computer

  • Speed: tens to hundreds of MHz, against several GHz in a laptop.
  • Memory: kilobytes of RAM, against gigabytes.
  • Operating system: usually none (called bare metal), or a tiny real-time operating system. Your code owns the whole chip.
  • Timing: predictable. With no background updates or other programs competing, the same code takes the same time every pass. Engineers call this deterministic.
  • Power: a few milliamps while running and often microamps while asleep, so it can run for months on small batteries.
  • Direct hardware access: pins connect straight to the outside world.

A single-board computer sits in between: a full processor with an operating system on a small board. Robots often use both: a single-board computer for vision and planning, and microcontrollers for fast, reliable jobs such as motor control. The robotic system architecture module discusses how to split the work.

One well-known example: the original Arduino Uno board uses an 8-bit ATmega328P microcontroller running at 16 MHz with 32 KB of flash and 2 KB of RAM. That is enough to run thousands of hobby robots.

Recap

A microcontroller is a whole computer on one chip: a CPU core that fetches and executes instructions, flash that keeps the program, RAM that holds changing variables, peripherals that do hardware jobs in parallel, and a clock that paces it all. It starts from the reset vector, runs main forever, and trades raw power for low cost, low energy use and predictable timing.

Project

Paper project: a memory and time budget

Imagine a microcontroller with 32 KB of flash, 2 KB of RAM (2,048 bytes) and a 16 MHz clock. You want to store the last 500 force readings, each a 2-byte number, so you can send them to a laptop later.

  1. How many bytes of RAM does the buffer need? What fraction of RAM is that?
  2. Engineers like to keep spare RAM for the stack and other variables. Would you be comfortable with this design? What would you change if you needed 2,000 readings?
  3. You sample at 500 readings per second. How many clock cycles are available between readings?

Show every step and its units.

Deliverables

  • The RAM calculation with units and the fraction of RAM used
  • A short judgement on whether the design leaves enough margin, with one alternative
  • The number of clock cycles between readings

Knowledge check

Check your understanding

  1. 1. Your program's code must still be there after the power is switched off and on again. Where is it stored?
  2. 2. A microcontroller runs at 8 MHz. How long is one clock cycle?
  3. 3. What does memory-mapped I/O mean?
  4. 4. Why do many robots use a microcontroller for motor control even when they also carry a powerful single-board computer?
  5. 5. What happens first when a microcontroller comes out of reset?

Go further