📖 Scenario: You are working on a small embedded system project using FreeRTOS. You want to understand how tasks work as the main building blocks to run different parts of your program independently.
🎯 Goal: Build a simple FreeRTOS program that creates two tasks. Each task will print a message to show how tasks run separately and repeatedly.
📋 What You'll Learn
Create two tasks with specific names
Use the FreeRTOS API to create tasks
Each task should print a unique message
Use a delay to allow task switching
Print output to show tasks running independently
💡 Why This Matters
🌍 Real World
Embedded systems often need to run multiple functions at once, like reading sensors and controlling motors. Tasks let you organize these functions clearly.
💼 Career
Understanding tasks is essential for embedded software developers working with real-time operating systems like FreeRTOS.
Progress0 / 4 steps
1
Create two task functions
Write two functions called Task1 and Task2 that take a void *pvParameters argument and run an infinite loop. Inside each loop, add a comment // Task running to indicate where code will go.
FreeRTOS
Hint
Each task function must have an infinite loop to keep running.
2
Create task handles and stack size
Declare two variables called xTaskHandle1 and xTaskHandle2 of type TaskHandle_t. Also, create a constant STACK_SIZE with value configMINIMAL_STACK_SIZE.
FreeRTOS
Hint
Task handles are used to manage tasks after creation. Stack size defines memory for each task.
3
Create the two tasks in main
In the main function, use xTaskCreate to create Task1 and Task2. Use STACK_SIZE for stack size, NULL for parameters, priority 1, and store handles in xTaskHandle1 and xTaskHandle2. Then start the scheduler with vTaskStartScheduler().
FreeRTOS
Hint
Use xTaskCreate to make tasks and vTaskStartScheduler to run them.
4
Add print statements and delay inside tasks
Inside the infinite loop of Task1, add printf("Task1 is running\n") and vTaskDelay(pdMS_TO_TICKS(1000)). Inside Task2, add printf("Task2 is running\n") and vTaskDelay(pdMS_TO_TICKS(1500)). This will show each task running independently with delays.
FreeRTOS
Hint
Use printf to show task messages and vTaskDelay to pause each task.
Practice
(1/5)
1. Why are tasks considered the building blocks in FreeRTOS?
easy
A. Because tasks store data permanently in memory.
B. Because tasks allow the program to run multiple jobs independently and smoothly.
C. Because tasks handle hardware interrupts directly.
D. Because tasks compile the program faster.
Solution
Step 1: Understand the role of tasks in FreeRTOS
Tasks divide a program into small, independent jobs that can run at the same time.
Step 2: Recognize the benefit of tasks
This division helps the device perform many things smoothly without waiting for one job to finish.
Final Answer:
Because tasks allow the program to run multiple jobs independently and smoothly. -> Option B
Quick Check:
Tasks = Independent jobs running smoothly [OK]
Hint: Tasks run jobs independently for smooth multitasking [OK]
Common Mistakes:
Thinking tasks store data permanently
Confusing tasks with interrupt handlers
Believing tasks speed up compilation
2. Which of the following is the correct way to create a task in FreeRTOS?
easy
A. xTaskCreate(TaskFunction, "TaskName", StackSize, NULL, Priority, &TaskHandle);
B. CreateTask(TaskFunction, Priority, StackSize);
C. task_create(TaskFunction, StackSize, Priority);
D. StartTask(TaskFunction, "TaskName", Priority);
Solution
Step 1: Recall the FreeRTOS task creation function
The correct function to create a task is xTaskCreate with parameters: function, name, stack size, parameters, priority, and handle.
Step 2: Compare options with correct syntax
Only xTaskCreate(TaskFunction, "TaskName", StackSize, NULL, Priority, &TaskHandle); matches the exact syntax and parameter order of xTaskCreate.
Final Answer:
xTaskCreate(TaskFunction, "TaskName", StackSize, NULL, Priority, &TaskHandle); -> Option A
Quick Check:
Correct function and parameters = xTaskCreate(TaskFunction, "TaskName", StackSize, NULL, Priority, &TaskHandle); [OK]
Hint: Remember xTaskCreate with 6 parameters for tasks [OK]
Common Mistakes:
Using wrong function names like CreateTask
Mixing parameter order
Omitting task handle parameter
3. What will be the output of the following FreeRTOS task code snippet?
A. Task handle parameter is NULL, so task cannot be managed later.
B. The task function should not have an infinite loop.
C. Stack size is too large and causes memory overflow.
D. vTaskStartScheduler() is called before creating the task.
Solution
Step 1: Check task creation parameters
The last parameter is the task handle pointer; passing NULL means the task handle is not stored.
Step 2: Understand implications of NULL task handle
Without a task handle, the program cannot control or delete the task later, which is usually needed.
Final Answer:
Task handle parameter is NULL, so task cannot be managed later. -> Option A
Quick Check:
Task handle NULL means no task control [OK]
Hint: Always provide a task handle pointer to manage tasks [OK]
Common Mistakes:
Thinking infinite loops are wrong in tasks
Believing stack size is always too large
Assuming scheduler starts before task creation
5. You want to design a FreeRTOS application that reads sensor data, processes it, and updates a display. Why should you use separate tasks for each job?
hard
A. To avoid using queues or synchronization mechanisms.
B. To reduce the total memory used by combining jobs in one task.
C. To ensure each job runs independently and can be scheduled without blocking others.
D. To make the program run faster by running all jobs in a single loop.
Solution
Step 1: Understand task separation benefits
Separating jobs into tasks allows each to run independently and be scheduled by FreeRTOS efficiently.
Step 2: Recognize multitasking advantages
This prevents one job from blocking others, improving responsiveness and reliability.
Final Answer:
To ensure each job runs independently and can be scheduled without blocking others. -> Option C
Quick Check:
Separate tasks = independent, non-blocking jobs [OK]
Hint: Use separate tasks to avoid blocking and improve multitasking [OK]
Common Mistakes:
Thinking combining jobs saves memory always
Believing single loop is faster for multitasking
Ignoring need for synchronization with multiple tasks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.