Learn FREERTOS with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Hello World Task (FreeRTOS)
#include <FreeRTOS.h>
#include <task.h>
#include <stdio.h>
void vTask(void* pvParameters) {
printf("Hello, FreeRTOS!\n");
vTaskDelete(NULL);
}
int main() {
xTaskCreate(vTask, "Task1", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}
Basic FreeRTOS task printing Hello World.
2
Blink LED Task (FreeRTOS)
#include <FreeRTOS.h>
#include <task.h>
#include <stdint.h>
#include "gpio.h"
void vBlinkTask(void* pvParameters) {
while(1) {
GPIO_TogglePin(LED_PIN);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
int main() {
xTaskCreate(vBlinkTask, "Blink", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}
Toggles an LED using a FreeRTOS task.
3
Queue Example (FreeRTOS)
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <stdio.h>
QueueHandle_t xQueue;
void vSenderTask(void* pvParameters) {
int value = 42;
xQueueSend(xQueue, &value, portMAX_DELAY);
vTaskDelete(NULL);
}
void vReceiverTask(void* pvParameters) {
int received;
xQueueReceive(xQueue, &received, portMAX_DELAY);
printf("Received: %d\n", received);
vTaskDelete(NULL);
}
int main() {
xQueue = xQueueCreate(1, sizeof(int));
xTaskCreate(vSenderTask, "Sender", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(vReceiverTask, "Receiver", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}
Shows sending and receiving messages via FreeRTOS queue.
4
Mutex Example (FreeRTOS)
#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>
#include <stdio.h>
SemaphoreHandle_t xMutex;
void vTask1(void* pvParameters) {
xSemaphoreTake(xMutex, portMAX_DELAY);
printf("Task1 in critical section\n");
xSemaphoreGive(xMutex);
vTaskDelete(NULL);
}
void vTask2(void* pvParameters) {
xSemaphoreTake(xMutex, portMAX_DELAY);
printf("Task2 in critical section\n");
xSemaphoreGive(xMutex);
vTaskDelete(NULL);
}
int main() {
xMutex = xSemaphoreCreateMutex();
xTaskCreate(vTask1, "T1", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(vTask2, "T2", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}
Demonstrates mutual exclusion using FreeRTOS mutex.
5
Timer Callback (FreeRTOS)
#include <FreeRTOS.h>
#include <timers.h>
#include <stdio.h>
void vTimerCallback(TimerHandle_t xTimer) {
printf("Timer callback triggered\n");
}
int main() {
TimerHandle_t xTimer = xTimerCreate("Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, vTimerCallback);
xTimerStart(xTimer, 0);
vTaskStartScheduler();
return 0;
}
Sets up a FreeRTOS software timer with a callback function.
6
Semaphore Synchronization (FreeRTOS)
#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>
#include <stdio.h>
SemaphoreHandle_t xBinarySem;
void vTaskProducer(void* pvParameters) {
printf("Producer giving semaphore\n");
xSemaphoreGive(xBinarySem);
vTaskDelete(NULL);
}
void vTaskConsumer(void* pvParameters) {
xSemaphoreTake(xBinarySem, portMAX_DELAY);
printf("Consumer received semaphore\n");
vTaskDelete(NULL);
}
int main() {
xBinarySem = xSemaphoreCreateBinary();
xTaskCreate(vTaskProducer, "Producer", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(vTaskConsumer, "Consumer", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}
Uses a binary semaphore to synchronize two tasks.
7
Task Delay Example (FreeRTOS)
#include <FreeRTOS.h>
#include <task.h>
#include <stdio.h>
void vTaskDelayExample(void* pvParameters) {
while(1) {
printf("Task running\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
int main() {
xTaskCreate(vTaskDelayExample, "DelayTask", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
vTaskStartScheduler();
return 0;
}
Demonstrates delaying a task for a fixed period.
8
Queue Set Example (FreeRTOS)
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <stdio.h>
QueueHandle_t xQueue1, xQueue2;
QueueSetHandle_t xQueueSet;
int main() {
xQueue1 = xQueueCreate(1, sizeof(int));
xQueue2 = xQueueCreate(1, sizeof(int));
xQueueSet = xQueueCreateSet(2);
xQueueAddToSet(xQueue1, xQueueSet);
xQueueAddToSet(xQueue2, xQueueSet);
vTaskStartScheduler();
return 0;
}
Demonstrates using a FreeRTOS queue set to manage multiple queues.
9
Priority Task Example (FreeRTOS)
#include <FreeRTOS.h>
#include <task.h>
#include <stdio.h>
void vHighPriorityTask(void* pvParameters) {
printf("High priority task running\n");
vTaskDelete(NULL);
}
void vLowPriorityTask(void* pvParameters) {
printf("Low priority task running\n");
vTaskDelete(NULL);
}
int main() {
xTaskCreate(vLowPriorityTask, "Low", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(vHighPriorityTask, "High", configMINIMAL_STACK_SIZE, NULL, 2, NULL);
vTaskStartScheduler();
return 0;
}
Creates two tasks with different priorities to demonstrate scheduling.
10
Event Group Example (FreeRTOS)
#include <FreeRTOS.h>
#include <task.h>
#include <event_groups.h>
#include <stdio.h>
EventGroupHandle_t xEventGroup;
int main() {
xEventGroup = xEventGroupCreate();
xEventGroupSetBits(xEventGroup, 0x01);
printf("Event bit set\n");
vTaskStartScheduler();
return 0;
}
Shows using FreeRTOS event groups for task synchronization.