Bird
Raised Fist0
FreeRTOSprogramming~30 mins

Multiple tasks running concurrently in FreeRTOS - Mini Project: Build & Apply

Choose your learning style10 modes available
Multiple tasks running concurrently
📖 Scenario: You are working on a small embedded system that needs to perform two tasks at the same time: blinking an LED and sending a message over a serial port. To do this, you will create two FreeRTOS tasks that run concurrently.
🎯 Goal: Create two FreeRTOS tasks: one that blinks an LED every 500 milliseconds and another that sends a message "Hello from Task 2" every 1000 milliseconds. Both tasks should run concurrently.
📋 What You'll Learn
Create a task called Task1 that toggles an LED every 500 ms
Create a task called Task2 that sends the message "Hello from Task 2" every 1000 ms
Use FreeRTOS API functions to create and start the tasks
Use vTaskDelay for timing delays
Start the FreeRTOS scheduler to run the tasks concurrently
💡 Why This Matters
🌍 Real World
Embedded systems often need to perform multiple operations at the same time, like blinking LEDs, reading sensors, and communicating. FreeRTOS tasks let you do this easily.
💼 Career
Understanding how to create and manage concurrent tasks is essential for embedded software developers working with real-time operating systems.
Progress0 / 4 steps
1
DATA SETUP: Define the LED pin and include FreeRTOS headers
Write the code to include FreeRTOS.h and task.h headers. Define a macro LED_PIN with the value 13 representing the LED pin number.
FreeRTOS
Hint

Use #include "FreeRTOS.h" and #include "task.h" to include FreeRTOS headers. Define LED_PIN as 13 using #define.

2
CONFIGURATION: Create task function prototypes and stack sizes
Declare two task functions: void Task1(void *pvParameters) and void Task2(void *pvParameters). Also, define two constants: TASK1_DELAY_MS as 500 and TASK2_DELAY_MS as 1000 for the delay times in milliseconds.
FreeRTOS
Hint

Declare the two task functions with the correct signature. Define the delay constants using #define.

3
CORE LOGIC: Implement the two tasks and create them in main
Implement Task1 to toggle the LED pin and delay for TASK1_DELAY_MS milliseconds using vTaskDelay. Implement Task2 to print "Hello from Task 2" and delay for TASK2_DELAY_MS milliseconds. In main, create both tasks with priority 1 and start the scheduler.
FreeRTOS
Hint

Use infinite loops in each task. Use vTaskDelay(pdMS_TO_TICKS(...)) for delays. Create tasks with xTaskCreate and start the scheduler with vTaskStartScheduler().

4
OUTPUT: Run the program and observe the concurrent task output
Run the program and observe the output. The LED state should toggle every 500 ms and the message "Hello from Task 2" should print every 1000 ms, showing both tasks running concurrently.
FreeRTOS
Hint

Look at the console output. You should see the LED toggling messages every 500 ms and the "Hello from Task 2" message every 1000 ms, interleaved.

Practice

(1/5)
1. What is the main purpose of running multiple tasks concurrently in FreeRTOS?
easy
A. To allow the program to perform several operations at the same time
B. To make the program run slower
C. To use more memory without benefit
D. To avoid using any delays in the code

Solution

  1. Step 1: Understand task concurrency

    Multiple tasks let the program handle different jobs at once, improving efficiency.
  2. Step 2: Identify the purpose in FreeRTOS

    FreeRTOS switches between tasks automatically, enabling multitasking.
  3. Final Answer:

    To allow the program to perform several operations at the same time -> Option A
  4. Quick Check:

    Multiple tasks = multitasking = To allow the program to perform several operations at the same time [OK]
Hint: Multiple tasks mean doing many things at once [OK]
Common Mistakes:
  • Thinking tasks slow down the program
  • Believing tasks waste memory
  • Ignoring the role of delays
2. Which of the following is the correct way to create a task in FreeRTOS?
easy
A. TaskCreate(TaskFunction, "TaskName", stackSize, NULL, priority, NULL);
B. createTask(TaskFunction, "TaskName", stackSize, NULL, priority, NULL);
C. xTaskCreate(TaskFunction, "TaskName", stackSize, NULL, priority, NULL);
D. xCreateTask(TaskFunction, "TaskName", stackSize, NULL, priority, NULL);

Solution

  1. Step 1: Recall FreeRTOS task creation function

    The correct function to create a task is xTaskCreate.
  2. Step 2: Match the exact syntax

    xTaskCreate(TaskFunction, "TaskName", stackSize, NULL, priority, NULL); matches the correct function name and parameters.
  3. Final Answer:

    xTaskCreate(TaskFunction, "TaskName", stackSize, NULL, priority, NULL); -> Option C
  4. Quick Check:

    Task creation function = xTaskCreate = xTaskCreate(TaskFunction, "TaskName", stackSize, NULL, priority, NULL); [OK]
Hint: Remember the function starts with 'xTaskCreate' [OK]
Common Mistakes:
  • Using incorrect function names like createTask
  • Mixing up parameter order
  • Missing the 'x' prefix in function name
3. Consider this FreeRTOS code snippet:
void Task1(void *pvParameters) {
  for (;;) {
    printf("Task1 running\n");
    vTaskDelay(pdMS_TO_TICKS(1000));
  }
}

void Task2(void *pvParameters) {
  for (;;) {
    printf("Task2 running\n");
    vTaskDelay(pdMS_TO_TICKS(500));
  }
}
What will be the output pattern when both tasks run concurrently?
medium
A. Task1 prints every 1 second; Task2 prints every 0.5 seconds
B. Task1 and Task2 print at the same time every 1 second
C. Task1 prints every 0.5 seconds; Task2 prints every 1 second
D. Only Task1 prints because Task2 is blocked

Solution

  1. Step 1: Analyze vTaskDelay timing for Task1

    Task1 delays 1000 ms (1 second) between prints.
  2. Step 2: Analyze vTaskDelay timing for Task2

    Task2 delays 500 ms (0.5 seconds) between prints.
  3. Step 3: Understand concurrent execution

    Both tasks run independently, so Task2 prints twice as often as Task1.
  4. Final Answer:

    Task1 prints every 1 second; Task2 prints every 0.5 seconds -> Option A
  5. Quick Check:

    Delays control print rate = Task1 prints every 1 second; Task2 prints every 0.5 seconds [OK]
Hint: Check each task's delay time to predict print frequency [OK]
Common Mistakes:
  • Assuming tasks print simultaneously
  • Mixing up delay times
  • Thinking one task blocks the other
4. Given this code snippet, what is the error?
void TaskA(void *pvParameters) {
  while(1) {
    // Do something
    vTaskDelay(1000);
  }
}

xTaskCreate(TaskA, "TaskA", 1000, NULL, 1, NULL);
medium
A. Task function must return int, not void
B. vTaskDelay requires delay in ticks, not milliseconds
C. Stack size 1000 is too small
D. Task name must be longer than 5 characters

Solution

  1. Step 1: Check vTaskDelay parameter

    vTaskDelay expects delay in ticks, not milliseconds.
  2. Step 2: Identify correct delay usage

    Use pdMS_TO_TICKS(1000) to convert 1000 ms to ticks.
  3. Final Answer:

    vTaskDelay requires delay in ticks, not milliseconds -> Option B
  4. Quick Check:

    Delay unit must be ticks = vTaskDelay requires delay in ticks, not milliseconds [OK]
Hint: Use pdMS_TO_TICKS() to convert ms to ticks for vTaskDelay [OK]
Common Mistakes:
  • Passing milliseconds directly to vTaskDelay
  • Wrong return type for task function
  • Incorrect stack size assumptions
5. You want two tasks to print messages alternately every 500 ms without overlapping prints. Which approach ensures this behavior in FreeRTOS?
hard
A. Use global variables without any synchronization
B. Use vTaskDelay with different delay times for each task
C. Run both tasks at the same priority without synchronization
D. Use a binary semaphore to synchronize tasks before printing

Solution

  1. Step 1: Understand the need for synchronization

    To avoid overlapping prints, tasks must coordinate access to the printing resource.
  2. Step 2: Identify synchronization method

    A binary semaphore allows one task to print while the other waits, alternating access.
  3. Step 3: Evaluate other options

    Different delays or same priority without sync can cause overlapping prints; global variables alone don't prevent conflicts.
  4. Final Answer:

    Use a binary semaphore to synchronize tasks before printing -> Option D
  5. Quick Check:

    Synchronization via semaphore = Use a binary semaphore to synchronize tasks before printing [OK]
Hint: Use semaphores to control shared resource access [OK]
Common Mistakes:
  • Relying only on delays for synchronization
  • Ignoring task priority effects
  • Not using any synchronization mechanism
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard