Learn Freertos - 10 Code Examples & CST Typing Practice Test
FreeRTOS is a lightweight, open-source real-time operating system (RTOS) kernel for embedded devices. It provides multitasking, scheduling, and inter-task communication for microcontrollers and small processors.
View all 10 Freertos code examples →
Learn FREERTOS with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Freertos
What is Freertos?
FreeRTOS is a lightweight, open-source real-time operating system (RTOS) kernel for embedded devices. It provides multitasking, scheduling, and inter-task communication for microcontrollers and small processors.
What are the primary use cases for Freertos?
Task scheduling for embedded systems. IoT and sensor node software. Industrial automation control. Real-time motor and sensor management. Edge devices and microcontroller applications
What are the strengths of Freertos?
Very lightweight (few KB footprint). Highly portable across MCUs. Open-source with active community. Predictable real-time behavior. Easy integration with peripheral drivers
What are the limitations of Freertos?
Not a full-featured OS (no file system, GUI). Limited memory protection features. Requires careful design for hard real-time tasks. Debugging complex task interactions can be tricky. No native networking stack (requires integration)
How can I practice Freertos typing speed?
CodeSpeedTest offers 10+ real Freertos code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.