Advertisement
microrobotics

NEO6MV2 GPS Module Interfaced with Arduino Uno

Mar 11th, 2025 (edited)
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * NEO-6M GPS Module Test Code
  3.  * Author: Lucienne Swart
  4.  * Date: 2025/03/11
  5.  *
  6.  * Description:
  7.  * This program reads data from a NEO-6M GPS module using an Arduino Uno and
  8.  * displays latitude, longitude, speed, number of satellites, and HDOP.
  9.  *
  10.  * Wiring:
  11.  * - GPS TX -> Arduino Pin 4 (RX)
  12.  * - GPS RX -> Arduino Pin 3 (TX)
  13.  * - GPS VCC -> 5V (or 3.3V depending on module)
  14.  * - GPS GND -> GND
  15.  *
  16.  * Dependencies:
  17.  * - TinyGPS++ Library (by Mikal Hart)
  18.  * - SoftwareSerial Library (for UART communication with GPS)
  19.  *
  20.  * Notes:
  21.  * - The GPS module requires some time (up to 10–15 minutes for a cold start)
  22.  *   to acquire a satellite fix, especially when used for the first time.
  23.  * - Ensure you are testing outdoors in an open space for the best signal reception.
  24.  */
  25.  
  26. #include <SoftwareSerial.h>
  27. #include <TinyGPS++.h>
  28.  
  29. // Define pins for SoftwareSerial (adjust if needed)
  30. static const int RXPin = 4; // Connect to TX of NEO-6M
  31. static const int TXPin = 3; // Connect to RX of NEO-6M
  32. static const uint32_t GPSBaud = 9600; // Default baud rate for the NEO-6M
  33.  
  34. // Create SoftwareSerial object for GPS communication
  35. SoftwareSerial gpsSerial(RXPin, TXPin);
  36.  
  37. // Create a TinyGPS++ object
  38. TinyGPSPlus gps;
  39.  
  40. void setup() {
  41.   // Begin Serial communication for debugging output
  42.   Serial.begin(115200);
  43.   // Begin SoftwareSerial for the GPS module
  44.   gpsSerial.begin(GPSBaud);
  45.  
  46.   Serial.println("NEO-6M GPS Comprehensive Stats");
  47.   Serial.println("Waiting for GPS data...");
  48. }
  49.  
  50. void loop() {
  51.   // Process all available GPS data
  52.   while (gpsSerial.available() > 0) {
  53.     char c = gpsSerial.read();
  54.     gps.encode(c);
  55.   }
  56.  
  57.   // If new GPS location data is available, display the stats
  58.   if (gps.location.isUpdated()) {
  59.     Serial.println("================================");
  60.  
  61.     // Date Information
  62.     Serial.print("Date: ");
  63.     if (gps.date.isValid()) {
  64.       Serial.print(gps.date.month());
  65.       Serial.print("/");
  66.       Serial.print(gps.date.day());
  67.       Serial.print("/");
  68.       Serial.println(gps.date.year());
  69.     } else {
  70.       Serial.println("INVALID");
  71.     }
  72.  
  73.     // Time Information
  74.     Serial.print("Time: ");
  75.     if (gps.time.isValid()) {
  76.       // Formatting to ensure two digits per field
  77.       if (gps.time.hour() < 10) Serial.print("0");
  78.       Serial.print(gps.time.hour());
  79.       Serial.print(":");
  80.       if (gps.time.minute() < 10) Serial.print("0");
  81.       Serial.print(gps.time.minute());
  82.       Serial.print(":");
  83.       if (gps.time.second() < 10) Serial.print("0");
  84.       Serial.println(gps.time.second());
  85.     } else {
  86.       Serial.println("INVALID");
  87.     }
  88.  
  89.     // Location: Latitude and Longitude
  90.     Serial.print("Latitude: ");
  91.     if (gps.location.isValid()) {
  92.       Serial.print(gps.location.lat(), 6);
  93.     } else {
  94.       Serial.print("INVALID");
  95.     }
  96.     Serial.print(" | Longitude: ");
  97.     if (gps.location.isValid()) {
  98.       Serial.println(gps.location.lng(), 6);
  99.     } else {
  100.       Serial.println("INVALID");
  101.     }
  102.  
  103.     // Speed
  104.     Serial.print("Speed: ");
  105.     if (gps.speed.isValid()) {
  106.       Serial.print(gps.speed.kmph());
  107.       Serial.println(" km/h");
  108.     } else {
  109.       Serial.println("INVALID");
  110.     }
  111.  
  112.     // Course
  113.     Serial.print("Course: ");
  114.     if (gps.course.isValid()) {
  115.       Serial.print(gps.course.deg());
  116.       Serial.println("°");
  117.     } else {
  118.       Serial.println("INVALID");
  119.     }
  120.  
  121.     // HDOP (Horizontal Dilution of Precision)
  122.     Serial.print("HDOP: ");
  123.     // The HDOP value is provided in hundredths; convert it to a float value.
  124.     if (gps.hdop.isValid()) {
  125.       Serial.println(gps.hdop.value() / 100.0);
  126.     } else {
  127.       Serial.println("INVALID");
  128.     }
  129.  
  130.     // Number of Satellites in view
  131.     Serial.print("Satellites: ");
  132.     Serial.println(gps.satellites.value());
  133.    
  134.     Serial.println("================================\n");
  135.   }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement