Ai-Thinker
Published

【GP-02-Kit】GPS Clock

It introduces the project design process of creating a GPS clock using the Ai-Thinker GP-02-Kit development board with Arduino IDE.

IntermediateFull instructions provided275
【GP-02-Kit】GPS Clock

Things used in this project

Hardware components

GP-02-Kit
×1

Story

Read more

Schematics

Schematic Diagram of GP-02-Kit

Code

Code Explanation of GPS Clock

C/C++
#include <TinyGPSPlus.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>

// I2C Connection Instructions
// NodeMCU Development Board — 0.96" OLED Pin Mapping
// GND → GND
// 3V3 → VCC
// SCL → D1 (GPIO 5)
// SDA → D2 (GPIO 4)

#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 64 // OLED display height
#define OLED_SDA 02 // SDA pin, GPIO2 (D4)
#define OLED_SCL 14 // SCL pin, GPIO14 (D5)
#define OLED_RESET 13 // Reset pin
#define SCREEN_ADDRESS 0x3C // OLED display I2C address

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// GPS module pin definitions
#define RXPin 4 // GPIO12 (NodeMCU D6)
#define TXPin 5 // GPIO14 (NodeMCU D5)

SoftwareSerial ss(RXPin, TXPin);
TinyGPSPlus gps;

int Year, Month, Date, Hour, Minute, Second, Yea, Mon, Dat, Hou;
double Lat, Lng;
String sMonth, sDate, sHour, sMinute, sSecond;

void setup() {
  Wire.begin(OLED_SDA, OLED_SCL);
  Serial.begin(9600);
  WiFi.mode(WIFI_OFF); // Disable WiFi to save power
  WiFi.forceSleepBegin();
  ss.begin(9600); // Initialize virtual serial port for GPS
  
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.display();
}

void loop() {
  boolean newData = false;
  for (unsigned long start = millis(); millis() - start < 300;) {
    while (ss.available()) {
      if (gps.encode(ss.read())) {
        newData = true;
      }
    }
  }

  Yea = gps.date.year();
  Mon = gps.date.month();
  Dat = gps.date.day();
  Hou = gps.time.hour();
  Minute = gps.time.minute();
  Second = gps.time.second();
  Lng = gps.location.lng();
  Lat = gps.location.lat();

  // Convert UTC time to Beijing time and correct errors
  Hour = Hou + 8;
  if (Hour >= 24) {
    Hour -= 24;
  }

  if (Hou + 8 >= 24) {
    Date = Dat + 1;
    if ((Mon == 1 || Mon == 3 || Mon == 5 || Mon == 7 || Mon == 8 || Mon == 10 || Mon == 12) && (Date > 31)) {
      Date -= 30;
      Month = Mon + 1;
    } else if ((Mon == 4 || Mon == 6 || Mon == 9 || Mon == 11) && (Date > 30)) {
      Date -= 29;
      Month = Mon + 1;
    } else if ((Yea % 4 == 0) && (Date > 29)) {
      Date -= 28;
      Month = Mon + 1;
    } else if ((Yea % 4 != 0) && (Date > 28)) {
      Date -= 27;
      Month = Mon + 1;
    } else {
      Month = Mon;
      Year = Yea;
    }
    if (Month > 12) {
      Month -= 12;
      Year = Yea + 1;
    }
  } else {
    Date = Dat;
    Month = Mon;
    Year = Yea;
  }

  // Display the results
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(38, 0);
  display.setTextSize(1);
  display.print(Year);

  display.setCursor(63, 0);
  display.print("-");
  display.setCursor(71, 0);
  sMonth = formatNumber(Month, 2);
  display.print(sMonth);

  display.setCursor(83, 0);
  display.print("-");
  display.setCursor(91, 0);
  sDate = formatNumber(Date, 2);
  display.print(sDate);

  display.setTextSize(2);
  display.setCursor(26, 13);
  sHour = formatNumber(Hour, 2);
  display.print(sHour);

  display.setCursor(46, 13);
  display.print(":");
  display.setCursor(56, 13);
  sMinute = formatNumber(Minute, 2);
  display.print(sMinute);

  display.setCursor(76, 13);
  display.print(":");
  display.setCursor(86, 13);
  sSecond = formatNumber(Second, 2);
  display.print(sSecond);

  display.setTextSize(1);
  display.setCursor(35, 33);
  display.print(gps.location.lng(), 8);

  display.setCursor(35, 43);
  display.print(gps.location.lat(), 8);

  display.setCursor(105, 53);
  display.print("m");

  display.setCursor(50, 53);
  display.print("km/h");

  display.setCursor(80, 53);
  display.print(gps.speed.mps());

  display.setCursor(25, 53);
  display.print(gps.speed.kmph());

  display.display();
  delay(500);
  display.clearDisplay();
}

// Helper function to format numbers
String formatNumber(int number, int digits) {
  String formatted = "";
  if (number < pow(10, digits - 1)) {
    formatted = String(number, DEC);
    while (formatted.length() < digits) {
      formatted = "0" + formatted;
    }
  } else {
    formatted = String(number, DEC);
  }
  return formatted;
}

Credits

Ai-Thinker
61 projects • 17 followers
Ai-Thinker is a leading supplier of IoT wireless products and solutions, including antennas, modules, and RF lab services.

Comments