Help in writing freertos code for GPS module

I am fairly new to the using Freertos and rtos in general and would like to build an application that;

  1. Creates two global variables name longitude and latitude
  2. Blinks two LEDs alternately at a rate of 1 seconds each.
  3. Receive GPS location data (longitude and latitude) and store them in the global variables. This will run at a rate of 10seconds
  4. To check if longtitude and latitude values have been updated and display their new value in the serial monitor.

2,3,4 and written as separate tasks. I have also attached my code.

#include <Arduino_FreeRTOS.h>
#include <TinyGPS++.h> 

#define led1  4
#define led2  6
double longitude;
double latitude;

TinyGPSPlus gps;

void GetGPS(void *pvParameters);
void BlinkLed1(void *pvParameters);
void BlinkLed2(void *pvParameters);
void PrintGPS(void *pvParameters);
//void SendData(void *pvParameters);
//void GetWeights(void *pvParameters);

void setup() {
    Serial.begin(115200); // For serial monitor
    Serial1.begin(115200); // For GPS connections
    Serial1.println("AT+GPS=1"); // atmega32u4[TX] -> A9G[RX]

    xTaskCreate(BlinkLed1,"led 1",128,NULL,1,NULL);
    xTaskCreate(BlinkLed2,"led 2",128,NULL,1,NULL);
    xTaskCreate(PrintGPS,"print locations",128,NULL,2,NULL);


void loop() {

void BlinkLed1(void *pvParameters){
    while (1) {

void BlinkLed2(void *pvParameters){
    while (1) {

void GetGPS(void *pvparameters) {
    while (1) {
      if (Serial1.available() > 0)
        if (gps.encode( {
            longitude = gps.location.lng();
            latitude =;

void PrintGPS(void *pvParameters){
    while (1) {
    // Check if there is any data in the variables
    if (longitude != 0.0 && latitude != 0.0) {
      // Display the data on the serial monitor
      Serial.print("Longitude: ");
      Serial.print("Latitude: ");

      // Reset the variables
      longitude = 0.0;
      latitude = 0.0;

    // Wait for 1 second before checking again
    vTaskDelay(10000 / portTICK_PERIOD_MS);


The LEDs bit work however there is nothing displayed in the serial monitor to indicate if I am a getting anything from the GPS module. However the same code for getting the GPS readings work when running bare-metal. Could someone guide me to the right resources on how to get it done well. Thanks in advance

The (minium) task stack of 128 seems too small for the tasks using stack hungry printf functions. Retry with increased (x 4) stack sizes. Also use volatile for shared variables like longitude, latitude.


  • Check return codes of FreeRTOS API calls like xTaskCreate.
    How do you know that it was successful ?
  • Make use of the FreeRTOS built-in development support like defining configASSERT and enable stack checking .

In addition to the suggestions from hs2@, you can think of merging GetGPS and PrintGPS tasks into one. This way you won’t have to worry about concurrency issues when accessing the global variables longitude and latitude.