Arduino GPS Clock

Published  March 7, 2016   17
S Saddam
Author
Arduino GPS Clock

There are many GPS satellites around the Earth which are used to provide the exact location of any place. Along with the location coordinates (Latitude and Longitude), it also provide other data like time, date, altitude, direction tracking angle etc. We have already learned to read this GPS data from Satellite using Arduino. So we are going to make a GPS clock using the ‘Time and Date’ data from the GPS satellite. GPS Updated Clock is very accurate and provides the real time data with precision of milliseconds.

 

Components:

  • Arduino Uno
  • GPS Module
  • 16x2 LCD
  • Connecting wires
  • Power supply

 

Working Explanation: 

GPS module sends the data in NMEA format, see the output of GPS data in below screenshot. NMEA format consist several sentences, in which we need one sentence to extract the Date and Time. This sentence starts from $GPRMC and contains the coordinates, time and other useful information. This $GPRMC is referred to Recommended minimum specific GPS/Transit data, and the length of this string is about 70 characters. We have previously extracted $GPGGA string in Vehicle Tracking System to find the Latitude and Longitude Coordinates. Here is the GPS output:

 

GPS-module-data-in-arduino-serial-monitor

 

And $GPRMC string mainly contains velocity, time, date and position

 

$GPRMC,123519.000,A,7791.0381,N,06727.4434,E,022.4,084.4,230394,003.1,W*6A
$GPRMC,HHMMSS.SSS,A,latitude,N,longitude,E,speed,angle,date,MV,W,CMD

 

Identifier

Description

RMC

Recommended Minimum sentence C

HHMMSS.SSS

Time in hour minute seconds and milliseconds format.

A

Status // A=active and V= void

Latitude

Latitude 49 deg. 16.45 min. North

N

Direction N=North, S=South

Longitude

Longitude(Coordinate)

E

Direction E= East, W=West

Speed

speed in knots

Angle

Tracking angle in degrees

Date

DATE in UTC

MV

Magnetic Variation

W

Direction of variation E/W

CMD (*6A)

Checksum Data

We can extract Time and Date from $GPRMC string by counting the commas in the string. With the help of Arduino and programming, we find $GPRMC string and stores it in an array, then Time (24 hours format) can be found after one comma and Date can be found after nine commas. Time and date are further saved in strings.

A GPS satellite provides Time and date in Coordinated Universal Time (UTC), so we need to convert it accordingly. To convert in according to Indian time, we have added 5:30 in UTC time, as Indian time is 5 and half hours ahead of UTC/GMT.

 

Circuit Diagram:

Circuit connections of Arduino GPS Clock are simple. Arduino is used to control the whole process, it receives the GPS data from satellite through GPS module, extracts the Date and Time from the $GPRMC string and shows it on LCD.

Data pins D4, D5, D6, D7 of 16x2 LCD are connected to pin no. 5, 4 , 3, 2 of Arduino  and command pin RS and EN of LCD are connected to pin 7 and 6 of Arduino respectively. GPS receiver Module Tx pin is connected to Rx pin 10 of Arduino. Ground PIN of Arduino and GPS are connected with each other. Here we have used SKG13BL GPS module, operating at 9800 bps baud rate. Arduino is also configured at 9800 bps baud rate by using function “Serial.begin(9800)”.

GPS Clock using Arduino Circuit Diagram

 

Programming Explanation:

In programming part first we include libraries and define pins for LCD & software serial communication. Also define some variable with arrays for storing data. By using Software Serial Library here, we have allowed serial communication on pin 10 and 11, and made them Rx and Tx respectively. By default Pin 0 and 1 of Arduino are used for serial communication but by using SoftwareSerial library, we can allow serial communication on other digital pins of the Arduino

#include<LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

#include <SoftwareSerial.h>
SoftwareSerial Serial1(10, 11); // RX, TX
... ....
.... ....

After it we have initialized serial communication and LCD in setup function and showed a welcome message on LCD.

Then we have extracted time and date form the received string.

while(x<str_lenth)
    {
     if(str[x]==',')
     comma++;
      if(comma==1)
      {
        x++;
        UTC_hour+=str[x++];
        ... ....
        .... ....

And then convert time and date to decimal and modify it to Indian time (UTC +5:30)

int UTC_hourDec=UTC_hour.toInt();
int UTC_minutDec=UTC_minut.toInt();
int Second=UTC_second.toInt();
int Date=UTC_date.toInt();
int Month=UTC_month.toInt();
... ....
.... ....

And then finally Time and Date have been showed on LCD using lcd.print function, check the full Code below.

Code

#include<LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

#include <SoftwareSerial.h>
SoftwareSerial Serial1(10, 11); // RX, TX

char str[70];
char *test="$GPRMC";
int temp,i;

void setup() 
{
  lcd.begin(16,2);
  Serial1.begin(9600);
  lcd.setCursor(0,0);
  lcd.print("GPS Updated Clock");
  lcd.setCursor(0,1);
  lcd.print(" Circuit Digest ");
  delay(300);
}

void loop()
{
  serial1Event();
  if (temp) 
  {
    lcd.clear();
    int str_lenth=i;
    int x=0,comma=0;
    String UTC_hour="";
    String UTC_minut="";
    String UTC_second="";
    String UTC_date="";
    String UTC_month="";
    String UTC_year="";
    String str1="";
    while(x<str_lenth)
    {
     if(str[x]==',')
     comma++;
      if(comma==1)
      {
        x++;
        UTC_hour+=str[x++];
        UTC_hour+=str[x++];
        UTC_minut+=str[x++];
        UTC_minut+=str[x++];
        UTC_second+=str[x++];
        UTC_second+=str[x];
        comma=2;
      }

      if(comma==10)
      {
        x++;
          UTC_date+=str[x++];
          UTC_date+=str[x++];
          UTC_month+=str[x++];
          UTC_month+=str[x++];
          UTC_year+=str[x++];
          UTC_year+=str[x];
      }  
      x++;
    }

    int UTC_hourDec=UTC_hour.toInt();
    int UTC_minutDec=UTC_minut.toInt();
    int Second=UTC_second.toInt();
    int Date=UTC_date.toInt();
    int Month=UTC_month.toInt();
    int Year=UTC_year.toInt();

    int Hour=UTC_hourDec+5;
    if(Hour>23)
    {
     Hour-=24;
     Date+=1;
    }
    int Minut=UTC_minutDec+30;
    if(Minut>59)
    Minut-=60;
    
    
   // UTC_ind_zone_time
    lcd.clear();
    lcd.print("Date: ");
    lcd.print(Date);
    lcd.print("/");
    lcd.print(Month);
    lcd.print("/");
    lcd.print("20");
    lcd.print(Year);
     
     lcd.setCursor(0,1);
     lcd.print("Time: ");
     lcd.print(Hour);
    lcd.print(":");
    lcd.print(Minut);
    lcd.print(":");
    lcd.print(Second);
  //  delay(100);
    temp=0;
//    j=0;
    i=0;
    x=0;
    str_lenth=0;
//    k=0;
  }
 // delay(1000);
}

void serial1Event()
{
  while(1)
  {
   while (Serial1.available())            //checking serial data from GPS
   {
    char inChar = (char)Serial1.read();
     str[i]= inChar;                    //store data from GPS into str[]
     i++;
     if (i < 7)                      
     {
      if(str[i-1] != test[i-1])         //checking for $GPRMC sentence
      {
        i=0;
      }
     }
    if(i>65)
    {
     temp=1;
     break;
    }
  }
   if(temp)
    break;
  }
}

Video

Have any question realated to this Article?

Ask Our Community Members

Comments

Submitted by Lee on Sat, 08/20/2016 - 08:19

Permalink

Very nice article! I love you can get Time-Date info from GPS. I'll be trying this out myself.
Thanks

Submitted by pcprusti on Wed, 12/14/2016 - 21:51

Permalink

your code have many logical errors.
let the gps data be "$GPRMC,223500.000,A,2713.1700,N,07728.7736,E,0.01,0.00,161.46,280213,,,A*62"
case-1:
1> Here 10:35 AM (10:35) UTC= 4:05 PM (16:05) IST but your code shows 03:05:00
2>here date is 28 /02/2013 UTC= 01/03/2013 IST but your code shows 29/02/2013 though 2013 is not a leap year.
case-2:
let the gps data be "$GPRMC,223500.000,A,2713.1700,N,07728.7736,E,0.01,0.00,161.46,300413,,,A*62"
1> Here 10:35 AM (10:35) UTC= 4:05 PM (16:05) IST but your code shows 03:05:00
2>here date is 30/04/2013 UTC= 01/05/2013 IST but your code shows 31/04/2013 though April has 30 days

Submitted by Jimmy on Sat, 02/04/2017 - 15:22

Permalink

Bonjour Saddam,

Thank you for your nice idea, congratulations.
It works correctly. I corrected the time UTC +1 hour instead of UTC +5: 30 h. I live in France.
I would like to make two small improvements, you can surely help me. The first is that the digits are displayed on two digits, for example. 10:01:05 instead of 10: 1: 5, for the time and date.
The second is that the display blinks every second, I do not know why. It is not comfortable to read the screen. I use the NEO-6M GPS module because here in France I have not found the one you are using.
I hope you can help me.
Best regards
Jimmy

P.S. hope you understand my very poor English

Submitted by Daniel on Fri, 04/07/2017 - 01:47

Permalink

Thanks for the project.
The code have some strage pieces, but help me to interpreter the gps data!

Submitted by Jimmy on Fri, 04/21/2017 - 19:02

Permalink

Hi Abhishek,
Thanks to you very much, I managed to change the number one point of my question.
Now, I would like to solve the point twoo.
The display remains blinking every second.
Do you have an idea to solve this problem.
When this problem is solved it will be great for me.
Best regards
Jimmy

@Jimmy,
I would guess the 'blinking' you refer to is software related, not hardware...
Try removing the lcd.clear(); from your code. You may need to play with the formatting of your lcd code in order to make sure you don't leave extraneous characters on screen each loop iteration, but this should get rid of the blinking.

Submitted by Patrik on Mon, 10/09/2017 - 17:31

Permalink

Hi, can you help me migrate it to arduino mega ? I have problem with software serial Serial1 , mega have 4 serials and i cant make it work, i want to use your project as core for my clock, thermostat, humidity, solar powered project, not for school, for home. Thanks in advance
exit status 1
conflicting declaration 'SoftwareSerial Serial1'
when i tried to change Serial1 to mySerial, it have problem at serialEvent.

Submitted by Deepa on Thu, 10/26/2017 - 01:21

Permalink

I tried this circuit but in serial monitor tool it doesn't display anything it is simply blank what may be the reason for this problem and please recommend solution for this problem..

Hi Deepa,

Was you code uploaded successfully? Make sure the baudrate on your Seril Monitor and the one you have mentioned in the void setup are the same

Submitted by Asgar on Thu, 02/15/2018 - 22:24

Permalink

i used neo 6m and changed the baud rate. yet my LCD is not showing anything. one more thing i want to ask is that can i replace neo 6m with G-top u2p1b1506 (rhydolabz GPS modle)
please help

Submitted by Ameena on Thu, 07/12/2018 - 14:48

Permalink

Hey, thanks for the project. Can we built an RTC where it works on GPS but has a crystal on standby? Please give me a clue how to start with it.

I did the same but it doesnot print anything.(Except the print statements of voidsetup and not of voidloop)

I did not use LCD, I tried doing that using 'Serial.print()' function and i have used 9600 baud which is the same for GPS Module.