Uni-Sine
Computer ScienceElectronics

What is a microcontroller?

Microcontrollers are often shortened to MCU, for microcontroller unit. A device with a microcontroller inside, doing one job, is called an embedded system. Almost anything with buttons and a battery has one, and many devices have several.

What is inside a microcontroller?

A microcontroller packs the main parts of a computer onto one piece of silicon.

PartWhat it doesOn an Arduino Uno's ATmega328P
Processor (CPU core)Runs the program's instructions one by one8-bit AVR core at 16 MHz
Flash memoryStores the program, and keeps it when the power is off32 KB, of which 0.5 KB holds the bootloader
SRAMHolds variables while the program runs. Lost when the power goes2 KB
EEPROMSmall store for settings that must survive power cuts1 KB
Digital pins (GPIO)Read a button as on or off, or switch an LED on or off14 on the Uno board
Analog-to-digital converter (ADC)Turns a voltage, like a sensor's output, into a number10-bit, 6 inputs on the Uno
Timers and PWMKeep time and make pulses that dim LEDs or set motor speed6 PWM pins on the Uno
Communication (UART, I2C, SPI)Talk to a computer, sensors, screens and other chipsOne of each

The Uno's figures come from Arduino's tech specs. Compare that 2 KB of working memory with a laptop's 16 GB: the laptop has about 8 million times as much. Microcontrollers don't need more, because they run one small program and no operating system.

How does a microcontroller work?

When power arrives, the microcontroller starts running the program in its flash memory straight away. There is no boot screen, because there is no operating system to load. Most programs follow the same shape: set things up once, then repeat a loop forever.

  1. Read inputs: check buttons, read sensors, receive messages.
  2. Decide: compare the readings with what should happen. Is the water hot enough? Has the timer run out?
  3. Set outputs: switch a heater, turn a motor, update a display.
  4. Repeat, often thousands of times a second.

Here is the classic first program for an Arduino, called a sketch. It flashes the small LED on pin 13 once a second.

void setup() {
  pinMode(13, OUTPUT);      // pin 13 is an output
}

void loop() {
  digitalWrite(13, HIGH);   // LED on
  delay(500);               // wait 500 ms
  digitalWrite(13, LOW);    // LED off
  delay(500);
}

setup() runs once at power-up and loop() runs over and over until the power goes. A microcontroller can also respond to events instantly with interrupts: a button press can pause the loop, run a short piece of code, and then return to where it was.

Talking to the real world

The processor only understands numbers, so the pins translate. A digital pin reads a voltage as HIGH or LOW. An ADC measures a voltage in steps: the Uno's 10-bit ADC turns 0 to 5 V into a number from 0 to 1023, so a reading of 512 means about 512 × 5 ÷ 1023 = 2.50 V. Going the other way, PWM switches a pin on and off very fast. On for a quarter of the time, an LED looks a quarter as bright.

Microcontroller vs microprocessor

A microprocessor, like the processor in a laptop, is only the CPU. It needs separate memory chips, storage and other chips around it, and an operating system to run programs. A microcontroller has its memory and input and output built in.

MicrocontrollerMicroprocessor
What is on the chipCPU, memory and input and outputMainly the CPU
RunsOne program, usually with no operating systemAn operating system and many programs
Typical speed1 to a few hundred MHzSeveral GHz
MemoryKilobytes to a few megabytesGigabytes, on separate chips
PowerMilliwatts, microwatts when asleepWatts to over a hundred watts
Start-upInstantSeconds, while the operating system loads
Typical costUnder a dollar to a few dollarsTens to hundreds of dollars
Used inRemote controls, appliances, sensors, carsLaptops, desktops, servers

Microcontroller vs Raspberry Pi

A Raspberry Pi 5 is not a microcontroller. It is a single-board computer: a microprocessor with gigabytes of memory that runs Linux, with a desktop and a web browser. Raspberry Pi also makes the Pico, which is a microcontroller board. As a rule of thumb, use a microcontroller to control hardware cheaply, reliably and on batteries, and a Raspberry Pi when you need a full computer, for example to run a web server or process camera images. Our Raspberry Pi course covers that side.

A bare chip is fiddly to use, so beginners start with a development board, which adds a USB port, a power regulator and labelled pins.

BoardChipSpeedWorking memoryLogic levelGood for
Arduino Uno R3ATmega328P, 8-bit16 MHz2 KB5 VFirst projects, the biggest pile of tutorials
ESP32 DevKitESP32, dual-core 32-bitUp to 240 MHz520 KB3.3 VWi-Fi and Bluetooth projects
Raspberry Pi PicoRP2040, dual-core Arm Cortex-M0+133 MHz264 KB3.3 VMicroPython, cheap and fast

Figures come from Espressif's ESP32 datasheet and Raspberry Pi's Pico documentation. Newer versions keep the same idea with faster chips, such as the Uno R4 and the Pico 2. The logic level matters: a 3.3 V board can be damaged by a 5 V signal on one of its pins. Our guide to Arduino compares the Uno and the ESP32 in more detail.

Try matching a project to the right board, then press Mark to check it in our Micro Controllers course.

Pick the Uno, ESP32, Pico or Raspberry Pi for each project, with a spec table to read (ChoiceQuiz).

Press Mark to have your answer marked in the Micro Controllers course.

What are microcontrollers used for?

  • Home appliances: washing machines, microwaves, dishwashers, thermostats and smart plugs.
  • Cars: engine control, airbags, anti-lock brakes, windows, mirrors and dashboard displays each use microcontrollers.
  • Computer accessories: keyboards, mice, game controllers and printers.
  • Health and fitness: fitness trackers, digital thermometers and medical devices such as insulin pumps.
  • Toys and gadgets: remote controls, drones, electronic toys and greetings cards that play a tune.
  • Industry and farming: sensors and controllers in factories, and soil moisture monitors in fields.
  • Hobby projects: weather stations, robots, LED displays and plant waterers built on Arduino, ESP32 and Pico boards.

How microcontrollers are programmed

You write the program on a computer, then upload it to the board over USB, which stores it in the chip's flash memory. The common languages are:

  • C and C++: the most common. The Arduino IDE uses C++ with simple built-in functions like digitalWrite().
  • MicroPython: a small version of Python that runs on the Pico and the ESP32, good for beginners.
  • Assembly: instructions for the processor itself, used when every byte or microsecond counts.

Worked examples

Example 1: reading a sensor

A temperature sensor on an Uno's analog pin gives a reading of 300. What voltage is that? The Uno maps 0 to 5 V onto 0 to 1023, as Arduino's reference describes, so the voltage is 300 × 5 ÷ 1023 = 1.47 V.

On a 12-bit, 3.3 V board like the ESP32 or the Pico, the readings run from 0 to 4095 instead, so a reading of 2048 is 2048 × 3.3 ÷ 4095 = 1.65 V, about half way.

Example 2: dimming an LED

On an Uno, analogWrite() takes a value from 0 (always off) to 255 (always on). What value makes an LED a quarter as bright? 0.25 × 255 = 63.75, so use 64.

Example 3: battery life with sleep

A sensor wakes once a minute, works for 1 second drawing 20 mA, then sleeps for 59 seconds drawing 0.01 mA. How long will a 2,000 mAh battery last?

  1. Average current: (20 × 1 + 0.01 × 59) ÷ 60 = 0.343 mA.
  2. Battery life: 2,000 ÷ 0.343 = about 5,830 hours.
  3. In days: 5,830 ÷ 24 = about 243 days.

Left awake at 20 mA all the time, the same battery would last 2,000 ÷ 20 = 100 hours, about 4 days. Sleeping is why a microcontroller can run a doorbell or a sensor for months.

A short history

Early calculators needed a cheap single chip to run them. At Texas Instruments, Gary Boone and Michael Cochran's 1971 single-chip calculator design led to the TMS1000, a 4-bit microcontroller family that TI announced in 1974, according to the Computer History Museum. Chips like it went into calculators, toys, microwave ovens and games. Today's microcontrollers are mostly 32-bit and far faster, but the idea is the same: one cheap chip that runs one job.

Common mistakes

  • Calling a Raspberry Pi 5 a microcontroller. It is a full computer running Linux. The Pico is Raspberry Pi's microcontroller board.
  • Connecting 5 V signals to a 3.3 V board. It can damage the pin or the whole chip. Check the logic level first.
  • Powering motors straight from a pin. A pin can only supply a small current. Motors need a driver or transistor.
  • Thinking the program is lost when the power goes off. It is in flash memory and runs again at power-up. Only the variables in SRAM are lost.
  • Using delay() for everything. While the board waits, it can't read buttons. Bigger projects check the time with millis() instead.

Common questions

What is an example of a microcontroller?
The ATmega328P on the Arduino Uno, the ESP32, and the RP2040 on the Raspberry Pi Pico are popular examples. Billions of others sit inside appliances, cars and gadgets.
Is Arduino a microcontroller?
Arduino is a company and a family of boards. An Arduino board has a microcontroller on it, such as the ATmega328P on the Uno, plus parts that make it easy to program and wire up.
What is the difference between a microcontroller and a microprocessor?
A microcontroller has its processor, memory and input and output on one chip and runs one program. A microprocessor is mainly a CPU that needs separate memory and storage and runs an operating system.
What language are microcontrollers programmed in?
Mostly C and C++, including the Arduino language, which is C++ with helper functions. MicroPython works on boards like the Pico and the ESP32.
Does a microcontroller need an operating system?
No. Most run a single program directly. Some bigger projects use a small real-time operating system (RTOS) to juggle several tasks, but it is optional.
How much does a microcontroller cost?
Simple chips cost well under a dollar each in bulk. Development boards for learning usually cost a few dollars to around thirty.

Revision cards

Say the answer first, then flip the card to check it.

Where to go next

  • Learn it

    Micro Controllers

    Arduino, ESP32 and programming hardware at the lowest level

    Start the course
Read next
Computer Science · ElectronicsHow to set up a Raspberry Pi as a web server: step by stepSet up a Raspberry Pi as a web server: install the OS, connect over SSH, install nginx, publish your page, add HTTPS and keep it secure.