Watch on Instagram

Circuit Bytes · Episode 55 · Electronics · Microcontrollers

"What a microcontroller is"

A computer needs a processor, memory and a clock. In a laptop those are separate chips; a microcontroller puts them all on one cheap chip, plus input and output pins, and runs one program in a loop. They hide in microwaves, remotes and washing machines, and a modern car has dozens.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

What a computer needs

Every computer, big or small, needs three things. A processor does the math and makes decisions (inside it are adders like the one from episode 52). Memory holds the program and the data it is working on (stored as bits, like the one-bit flip-flop in episode 53). A clock keeps every part in step (the square wave from episode 54).

Separate chips or one chip

In a laptop those parts are separate: a big processor chip, separate memory chips and support chips, all wired together on a circuit board. A microcontroller squeezes a small processor, its memory and its clock circuit onto one chip. Its memory comes in two kinds: flash holds the program and keeps it when the power is off, and RAM holds working data while it runs. Many microcontrollers have a built-in oscillator for the clock; others use an external crystal, so "a clock on the chip" is a slight simplification. Simple ones cost from well under a dollar to a few dollars each when bought in volume.

Pins: the way to the outside world

Around the edge of the chip are pins. An input pin reads the outside world: is this button pressed, how warm is this sensor? An output pin switches something on or off: a light, a buzzer, a motor. A pin can light a small LED by itself, but buzzers and motors usually need more current than a pin can give, so the pin switches a transistor or a driver chip that does the heavy lifting (episode 26). Most microcontrollers also carry extras such as timers and analog-to-digital converters for reading sensors (episode 47).

One program, forever in a loop

A microcontroller usually runs a single program that never ends. It checks its inputs, decides, sets its outputs, and goes back to the top: is the button pressed? Then turn the light on. Is it not? Then go round again. "Thousands of times a second" is a safe floor; at megahertz clock speeds a short loop like this can run far faster. That is why they are everywhere a simple job needs doing: microwaves, remotes, washing machines, toys and thermostats. A modern car has dozens of control units, each with its own microcontroller, and luxury and electric cars can have over a hundred.

Try it yourself

A microcontroller runs at 16 MHz, and most of its instructions take 1 cycle each. Roughly how many million instructions can it run each second? If its button-checking loop is 50 instructions long, about how many times a second can it check the button?

Good to know

  • A microcontroller is a single integrated circuit with a processor core, memory (flash or EEPROM for the program, RAM for data) and programmable input/output pins.
  • Many also include timers and analog-to-digital converters.
  • Many MCUs have an internal oscillator; others use an external crystal.
  • A laptop's processor relies on separate memory chips; phones use a system-on-a-chip, which is larger and sits between the two.
  • Output pins can light an LED directly, but buzzers and motors are usually driven through a transistor switch or driver.
  • Counts vary widely: a typical modern car has dozens of control units; luxury and electric cars can have over a hundred.
  • The "two cycles per instruction" in the comment question is made up for the problem; many small MCUs run most instructions in one cycle, and some older designs take twelve.

Answer to the comment question

A microcontroller runs at 8 MHz, and each instruction takes 2 cycles. How many million instructions per second is that? 4 million instructions per second. 8 MHz means 8,000,000 clock cycles every second. Each instruction uses 2 of them, so 8,000,000 ÷ 2 = 4,000,000 instructions per second.

Test your knowledge

Quick quiz: "What a microcontroller is"

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What three things does every computer need?
2What makes a microcontroller different from a laptop's processor?
3What does an input pin do?
4How does a typical microcontroller program run?

Sources

Video transcript

Can a whole computer fit on one cheap chip?

A computer needs a processor to do the math, memory to hold its program and data, and a clock to keep time.

In a laptop, those are separate chips. A microcontroller puts them all on one chip, plus pins that reach the outside world.

Input pins read buttons and sensors. Output pins switch lights, buzzers and motors.

Its program runs in a loop, thousands of times a second: is the button pressed? Then turn the light on.

They're in microwaves, remotes and washing machines, and a modern car has dozens.

A microcontroller runs at eight megahertz, and each instruction takes two cycles. How many million instructions per second is that? Comment your answer below!

That's your byte.

More from Circuit Bytes

All Circuit Bytes lessons