Understand microcontroller components, programming, and their role in controlling electronic devices and embedded systems.
The microcontroller (MCU) is a small complete computer in a single integrated circuit: it contains the processor, program memory, data memory, and peripherals (input and output ports, timers, converters, communication interfaces). The remote control, washing machine, microwave oven, and a car's anti-lock braking system each contain one, which always runs the same program to control a specific device [1][2][3][4].
- Processor (core) of 8, 16, or 32 bits, with frequencies from a few megahertz to several hundred.
- Program memory non-volatile, usually flash type, which retains code without power, and data memory volatile (SRAM); often a small EEPROM memory for parameters is also present.
- Input and output ports (GPIO): digital lines configurable to read switches and drive LEDs, relays, or other loads, within the current limits specified by the manufacturer.
- Timers and counters (*timers*): measure intervals, generate periodic signals and pulse-width modulated (PWM) signals.
- Analog-to-digital converter (ADC): converts a voltage into a number, to read sensors.
- Serial interfaces: UART, SPI, I2C, USB, and others to communicate with other circuits [1][2][3].
At power-up the processor starts from a fixed address and executes the stored program. Programs are written in C or assembly language and loaded into flash memory. The device must respond to external events: it does so with polling (repeated checking of an input) or with interrupts, signals that suspend the main program and execute a short service routine, then return to the point left off. To consume little power, many MCUs offer sleep modes in which the core stops and reactivates on an event.
The microprocessor has more computing power but requires external memory and peripherals; the microcontroller has lower performance but much lower cost, size, and power consumption, because everything is integrated. It is suitable for real-time control of a dedicated function.
An MCU at $16\ \mathrm{MHz}$ with an ADC of $10$ bits and reference $5\ \mathrm{V}$ reads a sensor that provides $10\ \mathrm{mV}$ per degree Celsius.
1. ADC resolution: $5/1024 \approx 4.9\ \mathrm{mV}$ per level, that is about $0.49\ ^\circ\mathrm{C}$ per level.
2. With a voltage of $0.25\ \mathrm{V}$ the temperature is $25\ ^\circ\mathrm{C}$ and the read value is about $0.25/5\cdot1024 \approx 51$.
3. To perform the measurement every $100\ \mathrm{ms}$ a timer with prescaler $64$ is used: each count lasts $64/(16\cdot10^{6}) = 4\ \mathrm{\mu s}$, so $0.1/(4\cdot10^{-6}) = 25000$ counts are needed, a value that fits in a $16$-bit counter.
Such a result is no more accurate than allowed by the sensor and the voltage reference: the ADC resolution does not coincide with its accuracy, because offset, noise, and reference tolerance add errors of several levels.
Operating consumption is approximately proportional to the clock frequency and the square of the supply voltage; in standby it drops to a few microamperes or less. A watchdog timer restarts the device if the program hangs, an important requirement in systems operating without human intervention. The program can be updated, in models with flash memory, even after installation, through a bootloader or a programming interface.
Microcontrollers are found in household appliances, automobiles, toys, alarm systems, measuring instruments, medical devices, and networked sensors (Internet of Things), often battery-powered for years.
[1] Wikipedia (it): Microcontrollore
[2] Wikipedia (en): Microcontroller
[3] Wikipedia (fr): Microcontrôleur
[4] Wikipedia (de): Mikrocontroller
Generating summary…