📖 Scenario: You are working on a simple embedded system using FreeRTOS. You want to create a task that prints a message every 1 second to simulate a periodic sensor reading.
🎯 Goal: Build a FreeRTOS task that uses vTaskDelay() to run periodically every 1 second and prints a message each time it runs.
📋 What You'll Learn
Create a task function named vPeriodicTask.
Use vTaskDelay() inside the task to wait for 1 second between prints.
Create the task in main() with a stack size of 100 and priority 1.
Print the message "Task running" each time the task runs.
💡 Why This Matters
🌍 Real World
Periodic tasks are common in embedded systems to read sensors, update displays, or communicate regularly.
💼 Career
Understanding how to use FreeRTOS task delays is essential for embedded software engineers working on real-time applications.
Progress0 / 4 steps
1
Create the periodic task function
Write a task function called vPeriodicTask that takes a void *pvParameters argument. Inside the function, write an infinite loop using for(;;). Inside the loop, print the message "Task running". Do not add any delay yet.
FreeRTOS
Hint
Remember to use for(;;) for an infinite loop and printf to print the message.
2
Add delay using vTaskDelay()
Inside the infinite loop of vPeriodicTask, after the printf statement, add a call to vTaskDelay() to delay the task for 1000 milliseconds (1 second). Use pdMS_TO_TICKS(1000) to convert milliseconds to ticks.
FreeRTOS
Hint
Use vTaskDelay(pdMS_TO_TICKS(1000)) to delay for 1 second inside the loop.
3
Create the task in main()
In main(), create the task vPeriodicTask using xTaskCreate(). Use a stack size of 100, priority 1, and pass NULL for parameters and task handle. Then start the scheduler with vTaskStartScheduler().
FreeRTOS
Hint
Use xTaskCreate() with the correct parameters and then call vTaskStartScheduler().
4
Run and observe the periodic output
Run the program and observe the output. The program should print Task running every 1 second repeatedly.
FreeRTOS
Hint
You should see the message Task running printed every second repeatedly.
Practice
(1/5)
1. What is the primary purpose of vTaskDelay() in FreeRTOS?
easy
A. To pause a task for a specific number of ticks
B. To create a new task
C. To delete a task
D. To send data between tasks
Solution
Step 1: Understand the function role
vTaskDelay() is designed to pause or block a task for a set number of tick counts, allowing other tasks to run.
Step 2: Compare with other options
Creating or deleting tasks and sending data are done by other FreeRTOS functions, not vTaskDelay().
Final Answer:
To pause a task for a specific number of ticks -> Option A
Quick Check:
vTaskDelay() pauses task = A [OK]
Hint: vTaskDelay() always pauses task execution for ticks [OK]
Common Mistakes:
Confusing vTaskDelay() with task creation functions
Thinking vTaskDelay() sends data
Assuming vTaskDelay() deletes tasks
2. Which of the following is the correct way to delay a task for 100 milliseconds using vTaskDelay()?
easy
A. vTaskDelay(100);
B. vTaskDelayMs(100);
C. vTaskDelay(pdMS_TO_TICKS(100));
D. vTaskDelayTicks(100);
Solution
Step 1: Understand tick conversion
FreeRTOS uses ticks, not milliseconds directly. To convert milliseconds to ticks, use pdMS_TO_TICKS().
Step 2: Check syntax correctness
Only vTaskDelay(pdMS_TO_TICKS(100)); correctly converts 100 ms to ticks and delays the task.
Final Answer:
vTaskDelay(pdMS_TO_TICKS(100)); -> Option C
Quick Check:
Use pdMS_TO_TICKS() inside vTaskDelay() [OK]
Hint: Always convert ms to ticks with pdMS_TO_TICKS() [OK]
Common Mistakes:
Passing milliseconds directly without conversion
Using non-existent functions like vTaskDelayMs()
Confusing ticks and milliseconds
3. Given the following code snippet, what will be the approximate delay between each print statement?
D. vTaskDelay should use pdMS_TO_TICKS(200) to convert milliseconds
Solution
Step 1: Check vTaskDelay argument
The argument 200 is treated as ticks, not milliseconds. To delay 200 ms, convert using pdMS_TO_TICKS(200).
Step 2: Verify other code parts
Calling doWork() before delay is valid. The task function returns void, which is correct.
Final Answer:
vTaskDelay should use pdMS_TO_TICKS(200) to convert milliseconds -> Option D
Quick Check:
Convert ms to ticks with pdMS_TO_TICKS() [OK]
Hint: Always convert ms to ticks before passing to vTaskDelay() [OK]
Common Mistakes:
Passing milliseconds directly to vTaskDelay()
Thinking vTaskDelay can't be in loops
Expecting task functions to return values
5. You want a task to run exactly every 1 second, but your system tick rate is 1000 Hz (1 tick = 1 ms). Which is the best way to implement this periodic delay using vTaskDelay()?
hard
A. Use vTaskDelayUntil() instead of vTaskDelay() for precise periodic timing
B. Call vTaskDelay(pdMS_TO_TICKS(1000)); inside the task loop
C. Call vTaskDelay(pdMS_TO_TICKS(1000)); and then call vTaskDelay(0);
D. Call vTaskDelay(1000); inside the task loop
Solution
Step 1: Understand vTaskDelay limitations
vTaskDelay() delays relative to current time, so small variations accumulate causing drift in periodic tasks.
Step 2: Identify precise periodic method
vTaskDelayUntil() uses an absolute wake time, preventing drift and ensuring exact periodic execution.
Final Answer:
Use vTaskDelayUntil() instead of vTaskDelay() for precise periodic timing -> Option A
Quick Check:
vTaskDelayUntil() prevents drift in periodic tasks [OK]
Hint: Use vTaskDelayUntil() for exact periodic task timing [OK]
Common Mistakes:
Using vTaskDelay() causes timing drift
Assuming tick count matches ms without conversion
Adding extra delays unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.