This project demonstrates safe shared-data access in a FreeRTOS-based system running on an Arduino Uno.
Multiple tasks concurrently read, display, and act on an ADC value, with a mutex ensuring data consistency and preventing race conditions.
A potentiometer simulates a sensor input, and LEDs provide a clear visual indication of system state.
Project OverviewIn multitasking embedded systems, sharing data without protection can lead to corrupted or inconsistent values.
This project uses a FreeRTOS mutex to protect a shared ADC variable accessed by three independent tasks.
Core FreeRTOS support is enabled with:
#include <Arduino_FreeRTOS.h>
#include <semphr.h>Hardware SetupThe potentiometer generates analog values from 0 to 1023, simulating a real sensor signal.
System ArchitectureShared Resource
volatile int adcValue = 0;
SemaphoreHandle_t xADCMutex;- adcValue is accessed by all tasks
- The mutex guarantees exclusive access during read/write operations
🧠 Key Insight:
Even a single integer must be protected when accessed by multiple preemptive tasks
Task Responsibilities
ADC Reading Task
Reads the analog input every 50ms and updates the shared value
if (xSemaphoreTake(xADCMutex, portMAX_DELAY)) {
adcValue = analogRead(POTENTIOMETER_PIN);
xSemaphoreGive(xADCMutex);
}ADC Printing Task
Safely reads the ADC value and prints it to the Serial Monitor.
if (xSemaphoreTake(xADCMutex, portMAX_DELAY)) {
Serial.println(adcValue);
xSemaphoreGive(xADCMutex);
}LED Control Task
Copies the shared ADC value under mutex protection and controls LEDs based on thresholds.
if (localValue < 341) { /* Red */ }
else if (localValue < 682) { /* Yellow */ }
else { /* Green */ }LED Logic Mapping
This mapping provides instant visual feedback of sensor levels.
Mutex Design
- Type: FreeRTOS Mutex (Binary, priority inheritance enabled)
- Protected Resource: adcValue
- Purpose: Prevent concurrent access and data races
xADCMutex = xSemaphoreCreateMutex();🧠 Why a Mutex (not a semaphore)?
Mutexes support priority inheritance, preventing priority inversion in real-time systems.
Timing and Scheduling
Expected Serial Output
ADC Monitoring System Started
ADC Value: 120
ADC Value: 135
ADC Value: 145Key Learning Points
- How mutexes prevent race conditions
- Safe sharing of sensor data between tasks
- Coordinating multiple tasks with different priorities
- Real-time LED control driven by analog input
Learning Outcome
This project provides a clear, practical example of why mutexes are essential in real-time embedded systems and how to apply them correctly when tasks share data.
It closely mirrors real-world sensor-processing pipelines used in professional RTOS-based designs.
That's all!If you have any questions or suggestions, don’t hesitate to leave a comment below.


_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)

Comments