GPS value for altitude always returns only 0

The GPS value for altitude always returns only 0

where is the Problem with TinyGPS++

dataString = String(gps.location.lng(), 7) + "," + String(gps.location.lat(), 7) + "," + String(gps.altitude.meters(), 2);

latitude and longitude are ok, but altitude reports only 0

Thanks for help

Please post a complete sketch that illustrates the problem

Incidentally, you might like to reconsider your decision to use Strings rather than C style strings in your program as they can cause problems in the small memory of most Arduinos, but that is not the cause of the current problem

There does not seems to be anything wrong with the posted code.
Perhaps, by looking at the tinyGPS thingy code I wrote you might take away a few useful things.

void fGPS_Parse(  void *pvParameters )
{
  for (;;)
  {
    xEventGroupWaitBits (eg, evtGPS_Parse, pdTRUE, pdTRUE, portMAX_DELAY) ;
    if ( xSemaphoreTake( sema_GPS_Gate, xTicksToWait0 ) == pdTRUE )
    {
      //query GPS: has a new complete chunk of data been received?
      if ( GPSSerial.available() > 1 )
      {
        if ( GPS.encode(GPSSerial.read()) )
        {
          if (  GPS.location.isValid())
          {
            if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
            {
              xPosit.Lat = GPS.location.lat();// store data into structure
              xPosit.Lon = GPS.location.lng();
              xSemaphoreGive( sema_Posit );
            }
          } // if (  GPS.location.isValid())
          if (GPS.speed.isValid())
          {
            if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
            {
              xPosit.MPH = GPS.speed.mph();
              xPosit.KPH = GPS.speed.kmph();
              xSemaphoreGive( sema_Posit );
            }
          } //  if (GPS.speed.isValid())
          if (GPS.time.isValid())
          {
            if ( xSemaphoreTake( sema_Time, xSemaphoreTicksToWait ) == pdTRUE )
            {
              xTime.iSeconds = GPS.time.second();
              xTime.iMinutes = GPS.time.minute();
              xTime.iHours = GPS.time.hour();
              xSemaphoreGive( sema_Time );
            }
          } // if (GPS.time.isValid())
          if (GPS.date.isValid())
          {
            if ( xSemaphoreTake( sema_Date, xSemaphoreTicksToWait ) == pdTRUE )
            {
              xDate.iMonth = GPS.date. month();
              xDate.iDay = GPS.date.day();
              xDate.iYear = GPS.date.year();
              xSemaphoreGive( sema_Date );
            }
          } // if (GPS.date.isValid())
          if (  GPS.altitude.isValid() )
          {
            if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
            {
              xPosit.Alti = GPS.altitude.meters();
              xSemaphoreGive( sema_Posit );
            }
          } //  if (  GPS.altitude.isValid() )
          if ( GPS.course.isUpdated() )
          {
            if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
            {
              xPosit.Hdg = GPS.course.deg();
              xSemaphoreGive( sema_Posit );
            }
          } // if ( GPS.course.isUpdated() )
          if ( xSemaphoreTake( sema_Posit, xSemaphoreTicksToWait ) == pdTRUE )
          {
            xQueueOverwrite( xQ_Posit, (void *) &xPosit );
            xSemaphoreGive( sema_Posit );
          }
        } // if ( GPS.encode(GPSSerial.read()))
      } // if ( GPSSerial.available() > 0 )
      xSemaphoreGive( sema_GPS_Gate );
    }
  } // for (;;)
  vTaskDelete( NULL );
} // void fGPS_Parse(  void *pvParameters )

Notice my use of 'if ( GPS.altitude.isValid() )'?

The code, to read the GPS, is triggered once a mS, and gathers the data to be displayed once a second.

Also, either write or find a sketch that prints the raw data from the GPS to serial so you can confirm that it's actually getting numbers for altitude.

That is my complet sketch

#include "TinyGPS++.h"
#include "SoftwareSerial.h"
#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;
String dataString = "";
String Datum = "";
String Uhrzeit = "";
int GPSSpeed = 0;

SoftwareSerial serial_connection(8,9); //RX=pin 10, TX=pin 11
TinyGPSPlus gps;

void setup() {

  // put your setup code here, to run once:
  Serial.begin(9600);
  serial_connection.begin(9600);
  Serial.println("GPS Start");
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
  
}

void loop() {

  // make a string for assembling the data to log:
  // put your main code here, to run repeatedly:

while(serial_connection.available())
  {
    gps.encode(serial_connection.read());
  }

if(gps.location.isUpdated())
{
  dataString =  String(gps.location.lng(), 7) + "," + String(gps.location.lat(), 7) + "," + String(gps.altitude.meters(), 2);
  SetDate(gps.date.year(), gps.date.month(), gps.date.day());
  SetTime(gps.time.hour(),gps.time.minute(),gps.time.second());

  WriteToSD(Datum + "  " + Uhrzeit + " ;" + dataString);

  GPSSpeed = gps.speed.kmph();

  if (GPSSpeed > 100) delay(3000);

  if (GPSSpeed > 50 and GPSSpeed < 100) delay(6000);

  if (GPSSpeed > 9 and GPSSpeed < 50) delay(10000);

  if (GPSSpeed < 10) delay(59600);
 
//  delay(59600);

}
}


void SetTime(int P_hour, int P_min, int P_sec)
{
  char s[20];
  sprintf(s,"%02u:%02u:%02u",P_hour, P_min, P_sec); 
  Uhrzeit = s;
}


void SetDate(int P_year, int P_month, int P_day)
{
  char s[20];
  sprintf(s,"%02u.%02u.%02u",P_day, P_month,P_year ); 
  Datum = s;
}


void WriteToSD (String WriteString){
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println(WriteString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(WriteString);
  }
  else {
    Serial.println("error opening datalog.txt");
  }

}

Its quite clear, this line in your sketch;

if(gps.location.isUpdated())

Is checking if the location is updated, i.e the Lat and Long, but the altitude may not yet have been updated.

I always use;

if (gps.location.isUpdated() && gps.altitude.isUpdated())

Idahowalker:
Notice my use of 'if ( GPS.altitude.isValid() )'?

srnet:
if (gps.location.isUpdated() && gps.altitude.isUpdated())

I notice and I try both... but the result is always the same

Initializing SD card...card initialized.
15.11.2019 16:34:59 ;7.xxx2548,51.xxx6470,0
15.11.2019 16:35:30 ;7.xxx2615,51.xxx6280,0
15.11.2019 16:36:00 ;7.xxx1790,51.xxx6090,1
15.11.2019 16:36:31 ;7.xxx0946,51.xxx6010,1
15.11.2019 16:37:01 ;7.xxx1371,51.xxx6130,1

Ok I got it !!!

Thank you.

At last I try both GPS.altitude.isValid() AND if (gps.location.isUpdated() && gps.altitude.isUpdated())

That do not work.

But if I use if (gps.location.isUpdated() && gps.altitude.isUpdated()) with the old variant gps.altitude.meters() it works. !!!

Thanks a lot for your help !!!

DO3GE:
Thanks a lot for your help !!!

The error is often in the code you do not post.