An object almost nobody sees, even though each of us uses it dozens of times a day: the washing machine, the remote control, the car ignition key, the wall thermostat all contain at least one complete computer, which never presents itself as such. This course opens the series: what a microcontroller is, how it differs from the processor of an ordinary computer, why an embedded system is designed by entirely different criteria than a desktop one, and why we will learn all this using an eight-bit microcontroller more than fifteen years old.
1Purpose and structure of the course6 min
A desktop computer or a phone is immediately recognized as a computer: it has a screen, a keyboard, an operating system. By contrast, the washing machine in the bathroom, the TV remote control, the car ignition key, the wall thermostat, the kitchen scale and the car anti-lock brake each contain at least one complete computer, with a processor, memory and a program - only this computer never presents itself as such. It has no screen of its own, does not run apps, cannot be reprogrammed by the user and, normally, is not noticed unless it breaks.
This object is called a microcontroller, and the systems built around it are called embedded systems - because the computing part is embedded inside a product that, from the buyer's point of view, is not a computer but a washing machine, a drill or an insulin pump.
No prior experience with microcontrollers is needed. Only elementary notions of digital electronics (logic level, register, bit) and basic notions of C programming are assumed.
Learning outcomes
- Explain the difference between microprocessor, microcontroller and system on chip, and its practical consequences on cost and power consumption
- Distinguish determinism from speed, and explain why the absence of a cache is an advantage here
- List the five design criteria of an embedded system (determinism, response time, power, cost, reliability)
- Describe the component blocks of a microcontroller and the role of each
- Justify the choice of data type based on the available memory resources
2What microcontrollers are for10 min
For several decades, almost any product that moves, heats, measures something or interacts with a person has contained a microcontroller, and the behavior of the product - what the customer perceives as its quality - is largely given by the program running in that microcontroller. A poorly designed gearbox can be heard; a poorly designed control algorithm is felt just as strongly, except nobody can open it up with a screwdriver to see it.
We go through a few fields, not to make an inventory, but to notice that technical requirements differ greatly from one field to another - and that, precisely for this reason, there are hundreds of families of microcontrollers instead of just one.
| Field | What the microcontroller does there |
|---|---|
| Robotics and mechatronics | closes the sensor→command→actuator loop, with a constant sampling step required by the PID controller; fuses data from several sensors with different noise and delays |
| Automotive | dozens of electronic control units; ignition timing must be delivered with an error of a few degrees of crankshaft rotation - it is not enough to be fast on average, it must be punctual every time |
| Consumer electronics and appliances | reads the keys, lights the display, remembers the settings; replaced the electromechanical cam-based programmer of 1970s washing machines |
| Environmental control and instrumentation | measures correctly and acts rarely; long-term measurement stability matters, not compute power; gave rise to the concept of a smart sensor, with its own calibration built in |
3What a microcontroller is10 min
This is where the name comes from: a microprocessor processes data brought to it from outside; a microcontroller controls a process, which requires pins through which it measures and commands. A microcontroller stripped of its peripherals would serve no purpose, because it would no longer have anything to touch the world with.
The minimum resources
For a circuit to deserve the name microcontroller, it must contain a minimal set of blocks:
- Central processing unit - ALU, working registers, instruction decoding logic, plus an oscillator that generates the clock, usually without any external component.
- Local memory, of two kinds - a non-volatile memory (Flash, nearly universal today) for the program, and a volatile memory (RAM) for variables, the stack and working areas.
- Interrupt system - without it, the microcontroller would be forced to constantly poll all event sources, consuming all of the processor's time.
- Digital input and output lines, grouped into parallel ports.
- Programmable timers and counters - the only way for a program to measure time precisely, without wasting the processor in wait loops.
The optional resources
On top of this core, each family adds peripherals that make it suited to a particular type of application: analog-to-digital conversion, synchronous and asynchronous serial ports, pulse-width modulated outputs (PWM), an analog comparator, EEPROM memory for calibration parameters, capture/compare for timers, a watchdog timer, and sleep modes for reducing power consumption. This list does not need to be memorized now - it is the map of the peripherals we will study, one by one, in the following chapters.
4Microprocessor, microcontroller and system on chip10 min
The three names - microprocessor, microcontroller, system on chip - are often used interchangeably, but they denote three different ways of dividing a computing system into integrated circuits. The distinction is not academic: it decides how many components the board has, how much the product costs, how much it consumes and how quickly it responds.
A microprocessor contains practically only the central unit - memory and peripherals are separate circuits, connected through buses that run across the board. A microcontroller brings onto the same silicon the processor, program memory, data memory and peripherals - the minimal system reduces, in fortunate cases, to the chip, a decoupling capacitor and a power supply. A system on chip (SoC) takes the idea further, but in a different direction: it integrates one or more high-power cores, a graphics processor, external memory controllers - but it does not, as a rule, carry its main memory internally, instead using external DRAM, and it almost always runs a complete operating system.
| Criterion | Microprocessor | Microcontroller | System on chip |
|---|---|---|---|
| Memory | external, expandable | internal, fixed at manufacturing | internal controller, external DRAM |
| Peripherals | separate circuits | on the same silicon | on the same silicon, but complex |
| Program | loaded from disk, changes often | fixed, written once to Flash | complete operating system |
| Power | large | very small | medium to large |
| Cost per unit | medium to large | very small | large |
| Startup | slow | immediate | slow |
From the moment the supply voltage crosses the threshold, after a short stabilization delay, the core starts executing the instruction at address zero. There is no operating system to load, no boot device to choose. This is why a device built around a microcontroller is ready to work in a few milliseconds, while one built around an SoC needs tens of seconds.
The most important consequence concerns the program: in a desktop computer, programs change constantly; in a microcontroller, the program is written once, when the product is manufactured, and stays the same for its entire life. This has consequences we return to at every turn: there is no dynamic memory allocation in the usual sense, there is no file system, there is no operating system separating programs from each other - our program is the sole master of the machine and, precisely for that reason, is also solely responsible for everything that happens.
5Determinism and response time12 min
A microcontroller is never used alone - it is the core of an embedded system, that is, an assembly in which the computing part serves a well-defined function, fixed at design time, inside a product that does not present itself as a computer.
physical process → sensors and conditioning → microcontroller → actuation
elements → action on the process. The time to traverse this loop, from the appearance of
a change in the process to the effect of the command, is the quantity that decides whether the system
works or not.Once this loop is drawn, it becomes clear why the criteria by which a microcontroller is chosen and programmed do not resemble those by which a desktop computer is chosen.
Determinism
On a desktop computer, the only question is how long an operation takes on average. In a embedded system, the relevant question is entirely different: what is the longest possible duration?
An airbag trigger that usually responds in five milliseconds, but sometimes in fifty, is unusable - the extra forty-five milliseconds are spent while the occupant moves toward the steering wheel. Determinism is easily lost for reasons that seem innocent: a library function whose duration depends on the data value, a wait loop for a peripheral, a long interrupt routine that delays a more urgent one, a cache memory that sometimes contains the requested instruction and sometimes does not.
The ATmega328P core executes most instructions in a single clock cycle. At the 16 MHz frequency of the Arduino Uno board, how many instructions do we have available if the regulation loop of a motor must run every millisecond?
See the solution
One millisecond means 16 000 000 × 0.001 = 16 000 clock cycles, so about
16 000 simple instructions.
This looks like a lot, but what is consumed outside the actual computation must be subtracted: an analog-to-digital conversion on this chip takes around 100 µs (already a tenth of the period), and transmitting a character on the serial interface at 9600 bit/s takes about one millisecond - that is, an entire period. The practical conclusion, which we return to several times in this course: missing the deadline does not come from computation, but from waiting on peripherals. That is why we will learn never to wait for a peripheral in a loop, but to work with interrupts (Lecture 05).
Response time
Closely related to determinism, but distinct from it: the interval between the occurrence of a physical event and the moment the system has produced the requested effect - it is made up of the sensor delay, the detection delay, the computation time and the inertia of the actuator.
A system is called "real time" not because it is fast, but because a correct response delivered too late is considered wrong. In hard real time, missing the deadline means a failure or an accident (ignition control). In soft real time, occasionally missing it degrades quality but does not compromise the function (updating a temperature display). The same application usually contains tasks from both categories, and the art of design lies in giving them the right priorities.
6Power, unit cost and reliability13 min
Power
An embedded system is often powered by a battery that must last months or years. A wireless sensor that reports a temperature every ten minutes spends over 99% of its time in sleep - the energy consumed during those idle moments, multiplied by their duration, far exceeds the energy consumed during measurement.
Unit cost
In a product manufactured in large volume, a difference of one currency unit between two chips means a million currency units at a million units - an amount that could pay a design team for an entire year. That is why, in embedded systems, the smallest chip that does the job is commonly chosen, even if that means more programming effort - the exact opposite of a desktop computer, where more memory than needed is bought, so no time is lost on optimization. Cost is not reduced to the chip's price: a microcontroller with more integrated peripherals may be more expensive individually, but if it removes an external ADC and a regulator from the board, the total cost of the product goes down.
Reliability
A microcontroller in a utility meter mounted on a pole or in a medical implant does not have anyone nearby to press a reset button. The system must be able to detect its own abnormal state and recover from it on its own.
If the program has hung in an infinite loop, the reinitialization no longer happens, and the system reboots. Used incorrectly - reinitialized from a periodic interrupt routine that runs regardless of the state of the main program - it completely loses its purpose, because it keeps being "fed" even after the main program has died.
Reliability is also helped by simpler things: correct behavior when the supply voltage drops (below a threshold, the microcontroller may execute wrong instructions and may corrupt the EEPROM memory), robustness to electromagnetic interference (the decoupling capacitor near the supply pins is not a formality), and checking the integrity of data in non-volatile memory.
7A short history of microcontrollers8 min
The history of the microcontroller begins with the history of the microprocessor. In the late 1960s, the Japanese company Busicom needed specialized circuits for a new range of desktop calculators. The idea that changed things, credited to Marcian "Ted" Hoff (an engineer of Romanian origin at Intel), was to stop designing a circuit for each function, and instead build a single circuit capable of executing a sequence of stored instructions - the function of the product was to be given by the content of the program memory, not by the topology of the silicon.
| Period | What happened |
|---|---|
| 1971-1972 | Intel launches the 4004 (4-bit) and the 8008 (8-bit) - the first commercial microprocessors |
| 1974-1980 | The move to the microcontroller: TI TMS1000 (1974), Intel 8048 (1976) and 8051 (1980, compatible cores are still made today); the PIC line also appears |
| 1980s | 8-bit microcontrollers become established; timers, serial interfaces and the first ADCs are integrated on chip |
| 1990s | 16- and 32-bit architectures; Atmel introduces AVR (designed for efficient C, 32 general registers); Flash memory becomes common in place of EPROM |
| 2000s | USB and complex communication peripherals are integrated; the ARM Cortex-M architecture appears, licensed by dozens of manufacturers |
| ~2010 | 32-bit microcontrollers become dominant in new applications of medium complexity |
| After 2015 | IoT pushes the integration of radio interfaces (Wi-Fi, LPWAN); RISC-V appears (no license fee for the instruction set), along with on-device inference of small machine learning models |
They continue to be made and sold in huge quantities, because in a remote control, an electric toothbrush or a thermostat, a 32-bit core brings no benefit, but does add cost and power draw. Evolution has added new categories at the top, without erasing the ones at the base - the reason this course still uses an 8-bit microcontroller.
8Anatomy of a microcontroller13 min
We now move from what a microcontroller does to how it is built, at the block level - the way the circuit looks from the programmer's point of view, not the silicon designer's.
The core
Contains the ALU (additions, comparisons, bit operations), the working registers, the program counter, the stack pointer and the status register (flags: zero, negative, carry). Microcontroller cores are usually of the RISC type: a small instruction set, uniform length, most executed in a single clock cycle - a choice that means a small control unit (low cost) and an exactly calculable duration (essential for determinism).
Program memory and data memory
Program memory is non-volatile (Flash, nearly universal today) - it holds the code and constants. The limited number of erase cycles (thousands to tens of thousands) is a physical property: each cell stores charge on an isolated floating gate, and erasing gradually degrades the insulator. It is not a bother for program memory (reprogrammed only tens to hundreds of times, during development), but it becomes a real problem for non-volatile data memory, if the program writes to it too often.
Data memory (SRAM) is fast, written/read an unlimited number of times, but volatile - it holds global variables, the working area of functions and the stack. It is the resource most often underestimated by beginners, because it is typically ten to twenty times smaller than program memory.
The stack grows from one end of memory toward the other, while variables occupy the area at the opposite end. When the function call depth grows enough, the stack reaches over the variables and overwrites them - the program keeps running, but with changed values, and on returning from a function it may end up, through a corrupted return address, anywhere in program memory. The typical symptom: a system that runs correctly for hours and then reboots for no apparent reason, usually right when it is shown to the client. Hence the rules: no dynamic allocation, explicitly sized buffers, care with large local variables and with cascading calls.
Many microcontrollers also have a third memory, EEPROM, non-volatile but written word by word, for data that must survive power-off (calibration constants, the last user setting) - much slower to write than SRAM, with a limited number of writes per cell.
Peripherals
The practical consequence: programming a microcontroller mostly means writing and reading registers, bit by bit, without touching neighboring bits:
DDRB |= (1 << PB5); /* bit 5 of port B becomes an output */
PORTB |= (1 << PB5); /* the line is set to logic 1 */
PORTB &= ~(1 << PB5); /* the line is set to logic 0 */
The oscillator, power supply and pins
The oscillator frequency directly determines execution speed, time-measurement resolution and power draw. An integrated RC oscillator needs no external components and starts quickly, but drifts with temperature and voltage (a few percent) - fine for reading a button, unsuitable for asynchronous serial communication. A quartz crystal gives accuracy orders of magnitude better, at the cost of two extra components - the reason the Arduino Uno board has a crystal mounted on it.
The decoupling capacitor near the supply pins has a real physical purpose: at every clock edge, thousands of gates switch at once and demand a current spike that the supply traces cannot deliver instantly - the capacitor acts as a local reservoir. Without it, the voltage has brief drops at every switching event, and the circuit behaves unpredictably, with errors that are hard to diagnose.
Almost always, the same physical pin can be an ordinary digital line, an ADC input, a timer output or a serial interface line, depending on the register configuration. Multiplexing is a consequence of economy (pins are expensive), but it has a direct practical consequence: two functions that need the same pin cannot be used at the same time, and this must be checked in the multiplexing table before drawing the wiring, not after.
9The microcontroller of this course: ATmega328P and Arduino12 min
To learn something concrete, in this book we work with the ATmega328P, an 8-bit microcontroller from the AVR family, mounted on the Arduino Uno board - not because it is the most capable, but because it is the most suitable for learning.
| Resource | Value |
|---|---|
| Architecture | AVR, RISC, Harvard, 8-bit data word |
| Operating frequency | 16 MHz on the Arduino Uno board, from a quartz crystal |
| Program memory | 32 KB Flash, part of which is used by the bootloader |
| Data memory | 2 KB SRAM |
| Non-volatile data memory | 1 KB EEPROM |
| General registers | 32, of 8 bits each, directly accessible by the ALU |
| Input-output lines | 23, grouped into three ports |
| Timers | two 8-bit and one 16-bit, with capture/compare/PWM modes |
| Analog-to-digital conversion | successive approximation, 10-bit resolution, with an input multiplexer |
| Serial interfaces | one asynchronous USART, one synchronous SPI, one two-wire (I²C) |
| Power supply | 5 V on the Arduino Uno board |
Everything meant by variables, buffers and the stack must fit into 2048 bytes - ten to twenty times less than the 32 KB of Flash memory.
A program must keep the last 100 values measured by the ADC, for a moving
average, and the programmer initially declares them as float. How much SRAM does this
buffer take, and what can be done?
See the solution
On this architecture, a float takes 4 bytes, so the buffer needs
100 × 4 = 400 bytes, almost 20% of the 2048 available.
However, the ADC of this chip has only 10 bits of resolution, so the values fit in a
uint16_t (2 bytes). With this change, the buffer drops to 200 bytes - and the
sum of the values, which can reach 100 × 1023 = 102 300, is kept in a
uint32_t. This saves 200 bytes and, in addition, the computation becomes faster, because
the core has no floating-point unit - operations on float are carried out
through library routines, dozens of times slower than integer operations.
Choosing the data type is no longer a matter of convenience, but one of resources - a theme that comes up constantly in this course.
Arduino functions vs. direct registers
The Arduino environment offers simple functions (such as pinMode, digitalWrite),
useful for quickly checking an idea - but they are not magic: each ultimately translates into
writes to the same registers, plus a cost in execution time and memory.
/* variant with Arduino functions */
void setup() { pinMode(13, OUTPUT); }
void loop() {
digitalWrite(13, HIGH); delay(500);
digitalWrite(13, LOW); delay(500);
}
/* the same action, directly on registers */
void setup() { DDRB |= (1 << PB5); } /* pin 13 is PB5 */
void loop() {
PORTB |= (1 << PB5); _delay_ms(500);
PORTB &= ~(1 << PB5); _delay_ms(500);
}
The variant with functions is more readable for a beginner; the variant with registers is shorter, more efficient and, more importantly for us, shows exactly what happens in the circuit. In this course we will use both, but the explanation will always start from the registers, because that is the only place where the mechanism is visible.
10Map of the course4 min
The rest of the course follows an order that is not accidental: each chapter uses what was built in the previous ones, and at the end they all meet in a complete application - a mobile robot (Lecture 11).
- Lecture 02 - the internal AVR architecture: memories, the 32 general registers, the instruction cycle.
- Lecture 03 - programming the microcontroller: the toolchain, the structure of a program with no operating system, how the program gets from the computer into Flash.
- Lecture 04 - the input-output ports: the electrical structure of a pin, the three registers that govern it, the pull-up resistors.
- Lecture 05 - the interrupt system: changes the way of thinking - from "one thing at a time" to "events can occur at any moment".
- Lecture 06 - the timers: how time is measured and generated without occupying the processor.
- Lecture 07 - the analog-to-digital converter: from transducer to numeric value.
- Lectures 08-09 - serial communications, asynchronous (USART) and synchronous (SPI, I²C).
- Lecture 10 - the design and development method for an application, with emphasis on debugging.
- Lecture 11 - a complete case study: the GT mobile robot, where all the peripherals work together.
Each chapter should be read with the data sheet next to you and the board on the table. A microcontroller is not learned by reading about it, just as swimming is not learned by reading about water. The explanations clarify the mechanism; the certainty that you have understood it comes only when the setup does what you predicted it would do.
11Frequent mistakes5 min
- "A more powerful microcontroller solves any determinism problem." False - determinism does not come from speed, but from the ability to calculate a guaranteed upper bound. A fast processor, but with unpredictable cache and branch prediction, can be less deterministic than a slow, simple one. Analyze the worst case, not the average execution time.
- "Something that works on the test board will work the same way in the final product." Not necessarily - SRAM overflow, for example, can stay latent for a long time and appear exactly at the unfavorable call depth, years later, in the field. Explicitly size buffers and the stack, do not assume that "it worked on the bench" is enough.
- "Using the float type is always simpler and costs only a little extra memory."
The cost is not just memory - floating-point operations on a core with no dedicated FPU
are carried out through library routines, dozens of times slower than integer
operations.
Choose the smallest data type that covers the actual range of values (for
example
uint16_tfor a 10-bit ADC).
12Summary and glossary5 min
A microcontroller embeds a processor, memory and peripherals on the same silicon - unlike a microprocessor (external memory and peripherals) or a system on chip (external main memory, complete operating system). An embedded system is designed by five criteria that do not resemble those of a desktop computer: determinism (a guaranteed upper bound on delay, not the average), response time (with the hard/soft real-time distinction), power (dynamic power depends on frequency and on the square of the voltage - stopping the clock is the most effective lever), unit cost (matters at a scale of millions of units), and reliability (the watchdog timer limits the duration of a failure, it does not fix it). At the block level, a microcontroller contains a core (usually RISC, Harvard), program memory (Flash), data memory (SRAM - the resource most often underestimated), optional non-volatile memory (EEPROM), peripherals (real circuits, working independently of the core), an oscillator and a power/reset circuit. The course uses the ATmega328P on the Arduino Uno board, chosen for the simplicity that allows full understanding of the mechanism, transferable afterward to any other microcontroller.
13Self-check questions7 min
- Explain, starting from the partitioning into integrated circuits, why a system with a microprocessor has more chips on the board than one with a microcontroller.
- What does it mean for a system to be deterministic, and why does the absence of a cache become an advantage here?
- Distinguish between hard and soft real time, with one example each from the automotive field.
- Derive from the CMOS dynamic power formula three methods for reducing power consumption.
- Explain the role of the watchdog timer and why reinitializing it from a periodic interrupt cancels its usefulness.
- What is the difference between the von Neumann and Harvard architectures, and what practical advantage does the second bring?
- Describe the mechanism by which SRAM overflow produces apparently random behavior.
14Directions for further study2 min
The next lecture turns the block diagram from this chapter into a concrete address map: how the memories of the AVR microcontroller are organized, what the 32 general registers do, how an instruction is executed and how to read, in the data sheet, the information we need.
The concepts in this lecture - registers, bits, bootloader, the structure of the Arduino board - become a real setup in Laboratory 01, where you install the development environment and command your first LED.