The RTOS vs bare metal microcontroller decision affects every non-trivial embedded project. For Indian engineers building everything from home automation controllers to industrial PLCs, choosing correctly between a real-time operating system and bare-metal coding determines code complexity, reliability, and maintainability for years to come.
Table of Contents
- What is Bare Metal Programming
- What is an RTOS
- When to Use Bare Metal
- When to Use RTOS
- Code Comparison
- Popular RTOS Options in India
- Frequently Asked Questions
What is Bare Metal Programming
Bare metal means writing firmware that runs directly on the hardware without an operating system kernel. Your code is the only software running — no preemption, no task scheduler, no system calls. You control everything: interrupts, timing, peripheral access. A typical bare metal structure is the main loop with peripheral polling or interrupt service routines.
// Typical bare metal structure (Arduino is bare metal)
void main() {
init_hardware();
while(1) {
// Poll sensors
if (sensor_ready()) {
data = read_sensor();
process_data(data);
}
// Check communication
if (uart_data_available()) {
handle_command(uart_read());
}
// State machine
run_state_machine();
}
}
What is an RTOS
An RTOS (Real-Time Operating System) provides task scheduling, synchronisation primitives (semaphores, mutexes, queues), and deterministic interrupt response times. Tasks are independent execution contexts with their own stack. The RTOS scheduler switches between tasks based on priority and blocking conditions.
FreeRTOS (used in ESP32 and STM32), Zephyr (nRF52840), and ThreadX (Azure RTOS) are the most common in Indian embedded development. Key RTOS services:
- Preemptive task scheduling (highest priority ready task always runs)
- Queues for inter-task data exchange
- Mutexes to protect shared resources
- Software timers for periodic tasks
- Event flags for task synchronisation
When to Use Bare Metal
Use bare metal when:
- Simple, single-function devices: thermostats, LED controllers, motor speed controllers
- Ultra-low resource MCUs: ATtiny85 (8KB flash, 512B SRAM) — RTOS overhead is prohibitive
- Deterministic hard real-time: when you need sub-microsecond jitter with absolute predictability
- Learning embedded programming: bare metal teaches hardware fundamentals without RTOS abstraction
- Cost-critical production: eliminating RTOS removes ~5–10 KB flash and 2–4 KB RAM overhead
When to Use RTOS
Use RTOS when:
- Multiple concurrent activities: reading sensors while handling WiFi while updating display
- Non-trivial communication: MQTT client, web server, BLE concurrently with sensor reading
- Teams sharing codebase: RTOS tasks provide clear module boundaries
- Commercial IoT product requiring OTA updates, watchdog, and power management
- Complex state machines that become spaghetti in a bare metal super-loop
Code Comparison
Same functionality — sensor reading + WiFi upload — bare metal vs RTOS:
// BARE METAL: state machine gets messy with timing
void loop() {
static unsigned long lastSample = 0;
static unsigned long lastUpload = 0;
if (millis() - lastSample >= 1000) {
lastSample = millis();
// Read sensor (may block if I2C is slow)
temperature = sensor.readTemperature();
}
if (millis() - lastUpload >= 60000) {
lastUpload = millis();
// WiFi upload (blocks for 500-2000ms during upload)
uploadToCloud(temperature); // During this, we miss sensor samples!
}
}
// RTOS: Clean concurrent tasks
void sensorTask(void *p) {
for (;;) {
temperature = sensor.readTemperature(); // Blocking OK here
vTaskDelay(pdMS_TO_TICKS(1000)); // Other tasks run during delay
}
}
void uploadTask(void *p) {
for (;;) {
uploadToCloud(temperature); // Blocking OK - sensor runs concurrently
vTaskDelay(pdMS_TO_TICKS(60000));
}
}
Popular RTOS Options in India
- FreeRTOS: Free, runs on ESP32, STM32, Arduino Due. Best Indian community support
- Zephyr RTOS: Linux Foundation project, Nordic nRF52, STM32. Safety certifiable
- Arduino asyncio (MicroPython): Cooperative multitasking, beginner-friendly on Pico
- ChibiOS: Compact RTOS for STM32, good for embedded systems courses at Indian engineering colleges
- Azure RTOS ThreadX: Commercial-grade, Microsoft-supported, available free for many MCUs
Frequently Asked Questions
Does using FreeRTOS make code automatically real-time?
Not necessarily. RTOS provides the framework for real-time behaviour, but correct task priorities, avoiding priority inversion, and limiting blocking operations are required for genuinely real-time responses. Poor RTOS usage can be less deterministic than well-written bare metal code.
Is Arduino code bare metal?
On AVR boards (Uno, Mega, Nano), yes — Arduino’s loop() runs directly without an OS. On ESP32 boards with Arduino framework, Arduino code runs as a FreeRTOS task, even if you never explicitly create tasks.
Can I mix bare metal and RTOS in the same project?
Yes. Start bare metal, identify which modules need concurrent execution, and gradually introduce FreeRTOS tasks for those. Many Indian products start as Arduino sketches and grow to FreeRTOS as complexity increases.
Add comment