Microcontrollers, the small computers inside every robot
What a microcontroller is, how its architecture and peripherals work, when you need an RTOS, and how to choose one for a robotics project.
Your microwave, your car, your washing machine and almost every robot contain small computers you never see: microcontrollers. This post explains what they are, what is inside them, when they need a real-time operating system and how to pick one for your project.
What is a microcontroller?
A microcontroller (MCU) is a complete small computer on a single chip: processor, memory and input/output pins together. It is built to do one job reliably, such as reading sensors and driving motors, using very little power.
| Microcontroller | Microprocessor (as in a PC or Raspberry Pi) | |
|---|---|---|
| On one chip | CPU, memory and peripherals | CPU only; memory and peripherals outside |
| Runs | One program, starts instantly | A full operating system (Linux, Windows) |
| Speed | 16 MHz to a few hundred MHz | Over 1 GHz |
| Power | Milliwatts | Watts |
| Timing | Precise, predictable | Can be interrupted by the OS |
| Examples | ATmega328P (Arduino Uno), STM32, ESP32 | Raspberry Pi, laptop CPUs |
Where you find them
- Kitchen appliances: the microwave timer, buttons and heating control.
- Cars: dozens of MCUs for the engine, brakes, airbags and windows.
- Home electronics: remote controls, TVs, audio systems.
- Security systems: motion detection, alarms, door locks.
- Wearables: smartwatches and fitness trackers.
- Robots: motor control, sensor reading, battery management.
What is inside
$$ \text{Sensors} \;\rightarrow\; \underbrace{\text{Inputs (GPIO, ADC)} \;\rightarrow\; \text{CPU + memory} \;\rightarrow\; \text{Outputs (GPIO, PWM)}}_{\text{all on one chip}} \;\rightarrow\; \text{Motors} $$
| Part | What it does |
|---|---|
| CPU | Runs the instructions: the ALU calculates, registers hold values being worked on, the control unit fetches and decodes instructions |
| Flash memory | Stores your program, kept when power is off |
| RAM | Holds variables while the program runs |
| GPIO pins | Digital inputs and outputs: buttons, LEDs, relays |
| ADC | Converts an analog voltage into a number |
| Timers and PWM | Measure time precisely and produce PWM signals for motors |
| UART, I²C, SPI, CAN | Talk to sensors, displays, other boards and a PC |
| Interrupts | Let an event (a button press, a timer) pause the main program and be handled immediately |
How fine can an ADC measure? With \(n\) bits and a reference voltage \(V_\text{ref}\), the smallest step is
$$ \Delta V = \frac{V_\text{ref}}{2^n} $$
A 10-bit ADC at 5 V resolves \(5 / 1024 \approx 4.9\) mV; a 12-bit ADC at 3.3 V resolves about 0.8 mV.
PWM sets the average voltage to a motor through the duty cycle \(D\), the fraction of time the pin is on:
$$ V_\text{avg} = D \cdot V_\text{supply} $$
At 12 V and 25 % duty cycle the motor sees 3 V on average and turns slowly.
Example: an obstacle-avoiding robot
$$ \text{Ultrasonic sensor} \;\rightarrow\; \text{MCU reads distance} \;\rightarrow\; \text{decides: turn or go} \;\rightarrow\; \text{PWM to motor driver} \;\rightarrow\; \text{wheels} $$
The microcontroller measures the echo time of the ultrasonic pulse with a timer, converts it to distance, and changes the PWM signals so the robot slows down and turns before it hits something.
Do you need an RTOS?
A simple program runs in one big loop (super-loop): read sensors, decide, drive motors, repeat. That is fine for small robots.
When several things must happen on time at once, for example a 1 kHz motor control loop, a sensor that needs reading every 10 ms and a radio link, a real-time operating system (RTOS) such as FreeRTOS or Zephyr helps. It splits the program into tasks with priorities and guarantees that the most urgent one runs first.
| Super-loop | RTOS |
|---|---|
| Simple, small, easy to debug | Several tasks with priorities |
| Fine for one main job | Needed for many time-critical jobs |
| Timing depends on the whole loop | Timing guaranteed per task |
How to choose one for a robot
| Family | Good for |
|---|---|
| ATmega (Arduino Uno, Nano, Mega) | Learning, simple robots, huge community |
| ESP32 | Wi-Fi and Bluetooth built in, IoT projects, still cheap |
| STM32 | Fast, many timers and CAN bus, serious motor control |
| Raspberry Pi Pico (RP2040) | Cheap, fast, flexible I/O, programmable in C or MicroPython |
| Teensy | Very fast, good for demanding real-time tasks |
Questions to ask: How many pins and which interfaces (I²C, CAN, encoders) do I need? Do I need wireless? How fast must the control loop run? 5 V or 3.3 V sensors? Is there good library support?
In many robots the work is split: a microcontroller handles motors and sensors in real time, while a single-board computer (Raspberry Pi, Jetson) runs vision and ROS 2. micro-ROS lets the microcontroller join the ROS 2 network directly.
Why they matter
Microcontrollers make devices smaller, cheaper, more efficient and smarter. Almost every automated system, from medical devices to factory machines, depends on them, and every robot you build will have at least one.