gwordal

Lesson 1 of 6 · 12 min

What is a microcontroller?

A laptop can run thousands of programs, but it cannot read a single push button without an operating system, drivers and a few seconds of boot time. A microcontroller does the opposite: it runs exactly one program, starts in milliseconds, and talks to the physical world directly through its pins. That trade is why microcontrollers sit inside washing machines, drones, car dashboards and nearly every robot you will ever build.

Computer vs microcontroller

A desktop computer is a CPU surrounded by separate chips: RAM, storage, graphics, network. A microcontroller (MCU) puts the processor, working memory, program storage and peripherals (timers, converters, serial ports) on a single chip. Nothing is added or swapped; you work with what the chip has.

Laptop CPUATmega328P (Arduino Uno)
Clock3,000+ MHz16 MHz
RAM8 to 32 GB2 KB
Program storage256 GB+ disk32 KB flash
Operating systemYesNone
Power15 to 60 Wabout 0.1 W

The Uno is millions of times smaller in memory, and that is a feature. Because there is no operating system, your code is the only thing running. When you write digitalWrite(13, HIGH), a pin changes voltage within a few microseconds, every time, with no other process to interrupt it. Engineers call this predictable timing, and robots depend on it.

The Uno board

The Arduino Uno is a convenience wrapper around the ATmega328P: a USB connector, a voltage regulator (so you can feed it 7 to 12 V), a 16 MHz crystal that sets the clock, a reset button and rows of header sockets. Drag the model to look around.

Drag to orbit

Blue pins: analog inputs. Orange pins: digital I/O.

An Uno-style board. Digital pins are amber; the headers give you direct access to the chip's pins.

GPIO: the pins

The chip's connection to the world is GPIO, General Purpose Input/Output. Each pin is a tiny switch you configure in software as either:

  • an output: the chip drives the pin to 5 V (HIGH) or 0 V (LOW), or
  • an input: the chip measures the voltage someone else applies.

The Uno exposes 14 digital pins (numbered 0 to 13) and 6 analog inputs (A0 to A5). A pin can only supply a small current. The datasheet says 40 mA absolute maximum, and 20 mA is the safe design value. At 5 V that is only 5 V x 0.020 A = 0.1 W, enough for an LED but nowhere near enough for a motor. You will see how to handle that in lesson 4.

Three kinds of memory

The ATmega328P has three separate memories, and each has a different job:

MemorySizeKeeps data without power?Holds
Flash32 KBYesYour compiled program (about 0.5 KB goes to the bootloader)
SRAM2 KBNoVariables while the program runs
EEPROM1 KBYesSettings you want to survive a power cycle

Flash and EEPROM can be rewritten only a limited number of times (roughly 10,000 and 100,000 writes). SRAM is fast and unlimited, but it forgets everything when power drops, which is why a robot does not remember its calibration unless you store it in EEPROM.

From sketch to running chip

A program in Arduino is called a sketch. The path from your keyboard to a blinking LED has four steps:

  1. You write the sketch in C++ (.ino file).
  2. The compiler (avr-gcc) translates it into machine code for the AVR instruction set.
  3. The result is sent over USB to the board.
  4. A small program already on the chip, the bootloader, receives the bytes and writes them into flash, then starts your code.

The bootloader is why you do not need a special programmer: it lives in the first 0.5 KB of flash and listens for a few moments after every reset. The upload speed on an Uno is 115,200 bits per second, so 6 KB of program takes about 6,000 x 8 / 115,200 = 0.42 s in theory, and a couple of seconds in practice.

// The smallest complete sketch: it does nothing, but it compiles and uploads.
void setup() {
  // runs once after power-up or reset
}

void loop() {
  // runs again and again, forever
}

Check yourself

Which memory on the ATmega328P holds your variables while the program runs, and loses them when power is removed?

Check yourself

What is the job of the bootloader?