Skip to content

Electronics

What is a microcontroller?

A microcontroller is a small computer on a single chip. Alongside its processor it holds memory for a program, memory for variables, timers and a set of pins that read buttons and sensors and switch lights and motors. It runs one program, by itself, for as long as it has power.

What's inside the chip

  • CPU: fetches the program's instructions and carries them out, one after another
  • Flash memory: keeps the program, even with the power off
  • RAM: holds the variables while the program runs
  • Timers: counters fed by a steady clock, so the chip can measure time
  • ADC: an analog-to-digital converter that turns a voltage into a number
  • Input and output pins: each one set by the program to read a voltage or to drive one

The Arduino Uno's chip, the ATmega328P, has a 16 MHz clock, 32 KB of flash and just 2 KB of RAM. That's tiny next to a laptop, and plenty for reading a sensor and running a motor.

How the program runs

You write the program on a computer, then upload it: it's copied into the chip's flash, and the chip starts running it straight away. Arduino programs have two parts. setup() runs once at power-on, to get things ready. loop() then runs again and again, forever.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);   // once
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);                     // on 100 ms
  digitalWrite(LED_BUILTIN, LOW);
  delay(900);                     // off 900 ms
}

One pass of that loop takes 100 + 900 = 1000 ms, so the LED flashes once a second: 1 Hz.

Live experiment

Watch a program run, line by line

A heartbeat program: setup() runs once and says Hello, then loop() flashes L for 100 ms and waits 900 ms, forever. Run it in real time or step a line at a time. Then move the Hello line into loop() and watch the serial monitor.

Try it yourself: free lesson

“What's a microcontroller?” is lesson 1 of 6 in The Core, Electronics World 8. Free with an account. Lessons unlock in order, so you’ll do the 49 before it first.

Microcontroller or microprocessor?

A microprocessor, like the one in a laptop or phone, is mostly CPU. It relies on separate chips for gigabytes of memory and storage, and runs an operating system that juggles many programs. A microcontroller keeps everything on one chip, can cost under a dollar, uses a few milliamps, and starts running its one program in a fraction of a second.

Where you'll find them

Almost everything with a button has one: microwaves, washing machines, keyboards, remote controls, thermostats and electric toothbrushes. A modern car has dozens, each running one job like the windows, the brakes or the engine.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“What's a microcontroller?” is lesson 1 of 6 in The Core, Electronics World 8. Free with an account. Lessons unlock in order, so you’ll do the 49 before it first.

Or start from the very first lesson, no account needed

Work it out

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The timers inside a microcontroller are built from…
    Show the answer

    Answer: counters. A timer is a counter fed by a steady clock, like World 7's ripple counter. Count the ticks and you've measured a time.

    • Why not “low-pass filters”? A low-pass filter smooths a signal but can't measure time to the tick. Timers are counters fed by the chip's clock.
    • Why not “Zener diodes”? A Zener diode holds a fixed voltage; it can't count anything. A timer counts the ticks of a steady clock.
  2. Question 2 of 3Which part of the board runs your program?
    Show the answer

    Answer: The black chip in the middle. The black chip is the microcontroller, the tiny computer. USB brings power and the program in, the pins are its hands, and L is just one small light on pin 13.

    • Why not “The USB socket”? The USB socket only brings power and the program in. The chip in the middle stores the program and runs it.
    • Why not “L, the little light”? L is just a small LED on pin 13 that the program can switch. It doesn't run anything.
    • Why not “The pins along the edges”? The pins are the chip's connections to the outside world. They read and drive signals; the chip does the work.
  3. Question 3 of 3After you tap Upload, your program runs…
    Show the answer

    Answer: On the board. Upload copies the program onto the board. From then on the board runs it by itself, line by line, even with the computer unplugged.

    • Why not “On the internet”? Nothing goes online. Upload copies the program down the USB cable into the board's own memory.
    • Why not “On your computer”? The computer is only used to write the program and send it. After the upload, the board runs it alone, even unplugged.
    • Why not “Inside the LED”? An LED only gives light. The program is stored and run by the microcontroller chip.
Keep practising: free lesson

“What's a microcontroller?” is lesson 1 of 6 in The Core, Electronics World 8. Free with an account. Lessons unlock in order, so you’ll do the 49 before it first.

Questions people ask

What is the difference between a microcontroller and a microprocessor?

A microprocessor is just the CPU: it needs separate chips for memory and storage, and usually runs an operating system, like the one in a laptop. A microcontroller puts a smaller CPU, memory, storage and input-output pins on one cheap chip that runs one program.

Is an Arduino a microcontroller?

An Arduino is a board built around one. The Arduino Uno's microcontroller is an ATmega328P, which runs at 16 MHz with 32 KB of flash for the program and 2 KB of RAM. The board adds USB, a power supply and headers for the pins.

Is a Raspberry Pi a microcontroller?

The usual Raspberry Pi boards aren't. They use a microprocessor with gigabytes of separate RAM and run Linux. The Raspberry Pi Pico is the exception: it's built around the RP2040, a microcontroller.

Every topic