I am using Arduino ATmega 2560. When i am using "AT+CGNSINF" command to get GPS value, sometimes it showing incorrect speed. The correct speed should be (50-60)km/h but sometimes it showing (200-300)km/h. How can I fix the problem or avoid/ignore the unexpected speed. here is my code

void get_GPS()
    sendATCommand("AT+CGNSINF","OK", 1000);

    // condition for valid response
    while (strstr(AT_Buffer, "+CGNSINF: 1,1") != NULL)
        strtok(AT_Buffer, ",");
        strtok(NULL, ",");
        strtok(NULL, ",");
        strtok(NULL, ",");
        strtok(NULL, ",");
        strtok(NULL, ",");
        Serial.println(strtok(NULL, ","));

        //sendATCommand("AT+CGNSINF", "OK", 1000);
uint8_t sendATCommand(char* ATcommand, const char *expectedResponse, unsigned long timeout)
    uint8_t x = 0, answer = 0;

    memset(AT_Buffer, '\0', AT_BufferLength);    // Initialice the string
    while (Serial1.available() > 0) Serial1.read();    // Clean the input buffer
    if (ATcommand[0] != '\0')
        Serial1.println(ATcommand);    // Send the AT command

    unsigned long previous = millis();

    // this loop waits for the answer
    do {

        if (Serial1.available() != 0) {    // if there are data in the UART input buffer, reads it and checks for the asnwer
            AT_Buffer[x] = Serial1.read();
            if (strstr(AT_Buffer, expectedResponse) != NULL)    // check if the desired answer (OK) is in the response of the module
                answer = 1;

    } while ((millis() - previous < timeout));    // Waits for the asnwer with time out

    return answer;
  • Need to check your code, May you need to correct something there...
    – flik
    Apr 11, 2018 at 6:54
  • here is my code pastebin.com/Rzc1qXTF Apr 11, 2018 at 7:00
  • 1
    Edit your question, put the code into it. Select it and press Ctrl+K to format it as code.
    – Nick Gammon
    Apr 11, 2018 at 7:37
  • ERR_CONNECTION_TIMED_OUT link is not opening..
    – flik
    Apr 11, 2018 at 7:48

1 Answer 1


Speed calculation in most GPS systems is very crude. It consists of simply "Where was I then? Where am I now?". Many will also add "How long has passed between then and now?", but many also don't - they assume a fixed time between points (1 second, for example). It doesn't know it's supposed to move, all it knows is the coordinates it has at the moment.

With the latter, if you don't get a reading for a couple of seconds (buildings in the way, for example) the position can suddenly jump a long way. You can see the effect in your phone's GPS in built-up areas - your position may jump long distances and then pause for a bit.

To visualise it in 1-dimension, you can think of a simple number sequence 0-9 and the difference between each value:

Value:        0 1 2 3 4 5 6 7 8 9 
Difference:     1 1 1 1 1 1 1 1 1

The difference equates to the "speed" of the sequence.

Now if the numbers don't get updated smoothly, but all at once at the end, you get something like:

Value:        0 1 2 2 2 2 2 7 8 9
Difference:     1 1 0 0 0 0 5 1 1

The end results is the same, but you get a "dead" period with no "movement" and then a big jump with an unusually high speed.

It's up to you to filter out such anomalies. It's simple enough to say "5 is too much - ignore it". You could also average the past 10 seconds worth of data (for example) and use that as your average speed over that time period, which would smooth out such peaks and troughs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.