Other microcontroller families
Learning one microcontroller is useful; understanding what all of them have in common is far more useful. This topic moves beyond the family you started with and compares architectures, resources and tools, so that you can choose the right chip for each problem and migrate without starting from scratch.
The basic architecture, the control registers, memory and I/O ports are covered in the microcontroller family, and the internal peripherals in microcontroller peripherals. Here we compare across families.
01Architecture decisions
| Decision | What it implies |
|---|---|
| Harvard or von Neumann | Harvard has separate buses for program and data: it can fetch an instruction and a data item in the same cycle. Von Neumann uses a single bus, simpler and more flexible. Almost all microcontrollers use variants of Harvard. |
| RISC or CISC | RISC: few instructions, almost all of them single-cycle, and a compiler that combines them. It is what AVR, PIC and ARM use. CISC survives mostly in the PC world. |
| 8, 16 or 32 bits | The width of the data path. With 8 bits, adding two 32-bit variables takes several instructions; with 32 bits, one. It is not just speed: it also changes how much memory can be addressed. |
| Registers or accumulator | AVRs and ARMs have banks of general-purpose registers; classic PICs work almost everything against a single accumulator, which forces more data moves. |
| Pipeline | Overlaps the fetch of one instruction with the execution of the previous one. It raises performance and complicates exact cycle counting, which sometimes matters in fine-grained control. |
For years, choosing 8 bits was the economical decision. Today an ARM Cortex-M0+ costs the same or less than an equivalent 8-bit part, with more memory and better peripherals. The reason to keep using 8 bits is usually something else: simplicity, very low sleep current, availability, and the fact that the team already knows it.
02Comparative overview
| Family | Core | Clock | Flash / RAM | Distinguishing feature |
|---|---|---|---|---|
| AVR (ATmega328P) | 8-bit RISC | 16 MHz | 32 kB / 2 kB | The one on classic Arduino boards. Huge documentation and very predictable behavior. |
| PIC (16F877A) | 8-bit RISC | 20 MHz | 14 kB / 368 B | Decades of industry behind it. Very robust, with good analog peripherals. |
| ARM Cortex-M (STM32F103) | 32-bit | 72 MHz | 128 kB / 20 kB | Today's industrial standard. Many manufacturers, one common core and shared tools. |
| ESP32 | 32-bit, dual core | 240 MHz | 4 MB / 520 kB | Has WiFi and Bluetooth built in. It is the natural choice for any connected project. |
| RP2040 | 32-bit, dual Cortex-M0+ | 133 MHz | external / 264 kB | Its PIO blocks generate protocols in hardware without occupying the CPU. |
The concepts are the same in all of them: input and output ports with their direction and data registers, timers, interrupts with their vector and their service routine, A/D converters, PWM, serial communication. What changes are the register names and the tools. That is why someone who has understood one family well learns the next in days, not months.
03How to choose
| Criterion | What to ask |
|---|---|
| Peripherals | Does it have the number of A/D channels, PWM outputs, serial ports and timers the project needs? This is what rules out candidates first. |
| Memory | Flash for the program and RAM for the data, with some margin. Running out of RAM midway through development forces a redesign. |
| Power consumption | If it runs on a battery, what matters are the low-power modes and the sleep current, not the running current. |
| Connectivity | Do you need WiFi, Bluetooth, Ethernet, CAN? Integrating it is cheaper than adding it externally. |
| Package | A chip available only in a fine-pitch surface-mount package may be impossible to solder in the workshop. |
| Availability and price | The best chip in the world is no use if you cannot get it in the country or if it has a year-long lead time. |
| Tools and community | Compiler, debugger, libraries, examples, and people who have already solved the same problem. |
Requirements: 4 analog sensors, logging to a memory card, a real-time clock, a display, sending data over WiFi once an hour, and battery power with a small solar panel.
- A classic 8-bit part falls short: 2 kB of RAM is not comfortable for handling the card, the display and the buffers, and an external WiFi module would have to be added.
- An ESP32 solves WiFi, memory and processing power all at once, and its deep sleep brings consumption down to tens of microamps between transmissions.
- Decision: ESP32, with the caveat that its A/D converter is mediocre. If temperature accuracy matters, an external converter is added over a serial bus.
That last point is the kind of detail that only shows up by reading the datasheet: the decision is not made by brand but by the numbers.
04Migrating from one family to another
- The program logic: state machines, calculations, filtering, decisions.
- The protocols and data formats.
- Everything written in standard C that does not touch registers.
- The configuration of clocks, ports and peripherals.
- The interrupt service routines and their vectors.
- Anything that depends on exact cycle counting.
- Startup code, the vector table and the memory map.
/* Abstraction layer: the application never touches registers. */ /* hal.h — the same interface for any family */ void hal_init(void); void hal_led(uint8_t encendido); uint16_t hal_adc(uint8_t canal); void hal_espera_ms(uint16_t ms); /* hal_avr.c — implementation for AVR */ void hal_led(uint8_t e) { if (e) PORTB |= (1<<PB5); else PORTB &= ~(1<<PB5); } /* hal_stm32.c — implementation for STM32, same signature */ void hal_led(uint8_t e) { HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, e ? GPIO_PIN_SET : GPIO_PIN_RESET); } /* app.c — not a single line changes when migrating */ while (1) { hal_led(1); hal_espera_ms(500); hal_led(0); hal_espera_ms(500); }
Adding it afterward, to a program where registers are sprinkled everywhere, costs almost as much as rewriting it. Separating what is logic from what is hardware from day one costs nothing and is worth a great deal, even if you never migrate: it makes it possible to test the logic on the computer, without the chip.
05Development tools
| Tool | What it does |
|---|---|
| Cross compiler | Runs on the PC and generates code for the microcontroller. In practice, GCC for AVR, ARM and RISC-V. |
| Linker and linker script | Decides where everything goes: code in flash, variables in RAM, stack. It is what changes the most between families. |
| Programmer | Writes the binary into the chip's flash: ISP on AVR, ICSP on PIC, SWD on ARM, serial port on ESP32. |
| Debugger | Halts execution, sets breakpoints and shows variables and registers on the real chip. With SWD, two pins are enough. |
| Logic analyzer | Shows what happens on the pins. For debugging serial communication it is more useful than the debugger. |
| Simulator | Lets you test without hardware. It is good for the logic; it never replaces testing on the real circuit. |
And it works, until it stops working. Learning to use a real debugger —stopping at a line, looking at variables, stepping through the code— completely changes how fast you find problems. An SWD adapter is inexpensive and is the best investment in the workshop after the oscilloscope.
06In the lab
Write a program that reads a potentiometer and controls the brightness of an LED by PWM, with an abstraction layer. Implement it on two different families by changing only the layer's file. Measure how many lines had to be touched: it is the concrete demonstration of why layers are separated.
Choose three microcontrollers and build your own table with: A/D channels and their resolution, number of timers, memory, sleep current, operating voltage, packages and real price in the local market. Justify which one you would choose for a specific project.
With an SWD adapter or similar, set a breakpoint inside an interrupt service routine, observe the value of the variables and step through the code. Compare the experience with debugging the same problem by turning an LED on and off.
With a multimeter in series, measure the current of a board in normal operation, at idle and in deep sleep. Calculate the battery life with a given battery in each case. The difference between the three states is usually three orders of magnitude.
07Common mistakes
| Mistake | Consequence |
|---|---|
| Choosing the chip out of habit | You end up adding externally what another chip had built in, at higher cost and with less reliability. |
| Not checking real availability | The project gets stuck waiting for a component that cannot be found. |
| Mixing 5 V and 3.3 V | ARMs and the ESP32 run at 3.3 V and do not tolerate 5 V on their pins: they get damaged. |
| Registers scattered throughout the program | Migrating or testing becomes impossible without rewriting. |
| Assuming the internal A/D is good | It varies enormously between families. If the measurement matters, you have to read the real specification. |
| Running out of RAM | The program behaves erratically because of stack overflows, and the symptom does not point to the cause. |
| Debugging only with an LED | Hours are lost on problems that a breakpoint solves in minutes. |
08Self-assessment
What advantage does the Harvard architecture give in a microcontroller?
Separate buses for program and data: it can fetch the next instruction and access a data item in the same cycle, which raises performance without raising the clock.
Why is a 32-bit core not just “faster”?
Because it also changes how much memory it can address and how many instructions are needed to operate on large variables: adding two 32-bit integers takes one instruction instead of several.
What feature distinguishes the ESP32?
It has built-in WiFi and Bluetooth, in addition to a dual core and plenty of memory. That is what makes it the natural choice for a connected project.
What is the first criterion that rules out candidates when choosing a chip?
The peripherals: if it does not have the number of A/D channels, PWM outputs or serial ports that the project needs, no amount of speed or memory makes up for it.
Which part of the program ports without changes between families?
The logic: state machines, calculations, filtering and protocols, as long as they are written in standard C and do not touch registers directly.
What is a hardware abstraction layer for?
So that the application does not touch registers: only the implementation of the layer is changed when migrating. It also makes it possible to test the logic on the computer, without the chip.
What is a cross compiler?
One that runs on the PC and generates code for a different architecture, that of the microcontroller. In practice, GCC in its versions for AVR, ARM or RISC-V.
What precaution is needed when connecting an ARM or an ESP32 to 5 V logic?
They run at 3.3 V and their pins do not tolerate 5 V: a level shifter or a divider is needed. Connecting them directly damages them.
What is the difference between a programmer and a debugger?
The programmer only writes the program into the flash; the debugger also halts execution, sets breakpoints and shows variables and registers while the chip is running.
Why measure the sleep current and not only the running current?
Because in battery-powered equipment the device spends most of its time asleep: battery life is set by the sleep current, which can be a thousand times lower than the running current.