Microcontrollers vs computers
A microcontroller has kilobytes of RAM, no operating system and a power budget in milliwatts. Learn how flash, RAM and power decide what model can fit.
In 60 seconds
Microcontrollers vs computers
A microcontroller has kilobytes of RAM, no operating system and a power budget in milliwatts. Learn how flash, RAM and power decide what model can fit.
| Laptop or phone | Typical microcontroller | |
|---|---|---|
| Working memory (RAM) | Gigabytes | Kilobytes to a few hundred kilobytes (some have a little more) |
| Storage for program and model | Hundreds of gigabytes | Flash memory: tens of kilobytes to a few megabytes |
| Operating system | Windows, Android, Linux, macOS | Often none: one program runs directly on the chip, or a very small real-time OS |
| Power | Watts | Milliwatts, and much less when sleeping |
| Maths | Fast floating point, often a GPU | Some chips have a floating-point unit, some do not; integer maths is usually cheaper |
Two kinds of memory
- Flash keeps things when the power is off, like a notebook. Your program and the model's weights are stored here. Flash is larger than RAM, but it fills up.
- RAM is working memory, like a desk while you study. It holds the input (for example the last second of audio), the numbers the model computes between layers (activations), and your program's variables. In TensorFlow Lite for Microcontrollers, this working area for the model is called the tensor arena.
A memory budget in four steps
- 1
Weights size
Number of weights × bytes per weight. float32 uses 4 bytes; int8 uses 1 byte. - 2
Free flash
Total flash minus the program without the model. The weights must fit here. - 3
Working memory
Input buffer + tensor arena + other variables. This must fit in RAM. - 4
Leave some room
Keep a margin. Libraries, stack use and later features need space too.
# How big is a model? Weights x bytes per weight.
for params in [10_000, 100_000, 1_000_000, 1_000_000_000]:
f32_kb = params * 4 / 1024
i8_kb = params * 1 / 1024
print(f"{params:>13,} weights: float32 {f32_kb:>12,.0f} KB int8 {i8_kb:>12,.0f} KB")No operating system: what changes
- There is usually no file system to "open model.tflite". The model is compiled into the program as an array of bytes.
- Memory is often planned in advance. Many TinyML runtimes avoid asking for memory while running, so the program cannot run out of memory halfway.
- You flash (upload) the whole program to the board, then read results through a serial connection, a light or a small display.
- The chip spends most of its time asleep to save power, waking up on a timer or a sensor signal.
Power in milliwatts
Watch and read more
Lab
Three working Python functions that tell you whether a model fits on an example board.
The problem
The starter describes an example board with 256 KB of RAM and 1024 KB of flash. The firmware already uses 300 KB of flash and 60 KB of RAM. Fill in the three functions:
model_kb(n_params, bytes_per_weight): the size of the weights in KB (1 KB = 1024 bytes).audio_buffer_kb(seconds, sample_rate, bytes_per_sample): RAM needed to hold a piece of audio.fits(n_params, bytes_per_weight, arena_kb):Trueonly if the weights fit in the free flash and the arena fits in the free RAM.
Run it and look at the table. Which models fit as float32, and which only as int8? Then add the audio buffer to the medium model's arena: does it still fit?
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Your model's weights fit in flash easily, but the board crashes when the model starts. What would you check first?Reveal
RAM. The weights may fit in flash, but the tensor arena, the input buffer and the rest of the program must all fit in RAM at the same time. Check the arena size your runtime reports as really needed, the stack size, and any large buffers such as audio. Reduce the input size, use a smaller model, or free other memory.
Q2Why is it risky to plan with 100% of the flash and RAM?Reveal
Other things also need space: libraries, the stack, debug code and future features. Some memory use only shows up at certain moments, such as a deep function call. A board that is full has no room to fix bugs later. Keep a clear margin and measure on the real board.
Questions people ask
Why not just use a Raspberry Pi for everything?
A Raspberry Pi is a full computer running Linux. It is easier to program and much more powerful, but it needs more power and takes time to boot. For a battery sensor that must run for months, a microcontroller is usually the better choice. For a camera project with mains power, a Pi can be easier.
What is the tensor arena?
It is a block of RAM that you set aside for the model's working memory: input, output and the in-between values. You choose its size in your program. Too small and the model cannot start; too big and your other code has no room. Tools can report how much was really used.
Does the model need to fit in RAM or in flash?
Usually the weights stay in flash and are read from there, and only the working memory must fit in RAM. This depends on the runtime and the chip, so check your tool's docs.
Why is integer maths preferred?
Many microcontrollers do integer maths faster and with less energy than floating-point maths, and some have no floating-point unit at all. Some chips also have special instructions that speed up int8 operations. That is one reason int8 models are common in TinyML.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.