Codey Online › Biblioteka komponentów Arduino i ESP32 › GPS NEO-6M
GPS NEO-6M
Czujniki

GPS NEO-6M

NEO-6M to moduł odbiornika GPS służący do wyznaczania położenia, prędkości i czasu na podstawie sygnałów satelitarnych. Zwykle komunikuje się z mikrokontrolerem przez UART szeregowy za pomocą TX i RX, a do zasilania używa VCC i GND; ma też pin PPS do precyzyjnego sygnału czasu.

Szerokość: 42.3 mm

Pinout — GPS NEO-6M

Pin Sygnał Opis
TX TX UART transmit output from the GPS module — connect to the host MCU RX.
GND ground Ground reference for power and UART signals.
VCC power Power input for the GPS module, typically 3.3–5V depending on board regulator.
RX RX UART receive input to the GPS module — connect to the host MCU TX.
PPS digital Pulse-per-second timing output from GPS lock for precise synchronization.

Przykładowy schemat połączeń — GPS NEO-6M

Z Do Przewód
board:5V x1:VCC red
board:GND x1:GND black
board:D4 x1:TX green
board:D3 x1:RX blue

Przykładowy kod Arduino — GPS NEO-6M

Ten przykład został napisany i sprawdzony kompilacją dla arduino-uno. Codey Online dostosuje go do każdej innej obsługiwanej płytki.

#include <SoftwareSerial.h>

// GPS NEO-6M canonical example for Arduino Uno
// Wiring:
// GPS VCC -> 5V
// GPS GND -> GND
// GPS TX  -> D4 (Arduino receives on this pin)
// GPS RX  -> D3 (Arduino transmits on this pin)

SoftwareSerial gpsSerial(4, 3); // RX, TX

char lineBuffer[120];
uint8_t lineIndex = 0;

void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600);

  Serial.println(F("GPS NEO-6M example started."));
  Serial.println(F("Waiting for NMEA sentences..."));
}

void printField(const char *label, const char *value) {
  Serial.print(label);
  Serial.print(value);
}

void handleRMC(char *sentence) {
  // Example: $GPRMC,hhmmss.sss,A,llll.ll,a,yyyyy.yy,a,x.x,x.x,ddmmyy,,,A*hh
  char *fields[12] = {0};
  uint8_t count = 0;
  char *token = strtok(sentence, ",");
  while (token && count < 12) {
    fields[count++] = token;
    token = strtok(NULL, ",");
  }

  if (count < 10) return;
  if (fields[2] == NULL || fields[2][0] != 'A') {
    Serial.println(F("No GPS fix yet."));
    return;
  }

  Serial.println(F("--- Valid Fix (RMC) ---"));
  if (fields[1]) printField("Time (UTC): ", fields[1]), Serial.println();
  if (fields[3] && fields[4]) {
    Serial.print(F("Latitude: "));
    Serial.print(fields[3]);
    Serial.println(fields[4]);
  }
  if (fields[5] && fields[6]) {
    Serial.print(F("Longitude: "));
    Serial.print(fields[5]);
    Serial.println(fields[6]);
  }
  if (fields[7]) {
    Serial.print(F("Speed (knots): "));
    Serial.println(fields[7]);
  }
  if (fields[8]) {
    Serial.print(F("Course (deg): "));
    Serial.println(fields[8]);
  }
  if (fields[9]) {
    Serial.print(F("Date (ddmmyy): "));
    Serial.println(fields[9]);
  }
}

void handleGGA(char *sentence) {
  // Example: $GPGGA,time,lat,N,lon,E,fixquality,numsat,hdop,alt,M,...
  char *fields[15] = {0};
  uint8_t count = 0;
  char *token = strtok(sentence, ",");
  while (token && count < 15) {
    fields[count++] = token;
    token = strtok(NULL, ",");
  }

  if (count < 8) return;
  int fixQuality = fields[6] ? atoi(fields[6]) : 0;
  int satellites = fields[7] ? atoi(fields[7]) : 0;

  Serial.println(F("--- GGA Status ---"));
  Serial.print(F("Fix quality: "));
  Serial.println(fixQuality);
  Serial.print(F("Satellites: "));
  Serial.println(satellites);
  if (fields[8]) {
    Serial.print(F("HDOP: "));
    Serial.println(fields[8]);
  }
  if (fields[9] && fields[10]) {
    Serial.print(F("Altitude: "));
    Serial.print(fields[9]);
    Serial.print(' ');
    Serial.println(fields[10]);
  }
}

void loop() {
  while (gpsSerial.available()) {
    char c = (char)gpsSerial.read();

    if (c == '\n') {
      lineBuffer[lineIndex] = '\0';
      lineIndex = 0;

      // Trim trailing carriage return if present
      char *sentence = lineBuffer;
      size_t len = strlen(sentence);
      if (len > 0 && sentence[len - 1] == '\r') {
        sentence[len - 1] = '\0';
      }

      if (strncmp(sentence, "$GPRMC", 6) == 0 || strncmp(sentence, "$GNRMC", 6) == 0) {
        char temp[120];
        strncpy(temp, sentence, sizeof(temp));
        temp[sizeof(temp) - 1] = '\0';
        handleRMC(temp);
      } else if (strncmp(sentence, "$GPGGA", 6) == 0 || strncmp(sentence, "$GNGGA", 6) == 0) {
        char temp[120];
        strncpy(temp, sentence, sizeof(temp));
        temp[sizeof(temp) - 1] = '\0';
        handleGGA(temp);
      }
    } else {
      if (lineIndex < sizeof(lineBuffer) - 1) {
        lineBuffer[lineIndex++] = c;
      } else {
        lineIndex = 0; // overflow protection
      }
    }
  }
}

Podobne komponenty

18650 Battery Shield with USB-C Charger and 5V Boost Converter

18650 Battery Shield with USB-C Charger and 5V Boost Converter

2.4 inch OLED Display I2C 3.3V

2.4 inch OLED Display I2C 3.3V

3.5 inch TFT LCD Shield

3.5 inch TFT LCD Shield

50kg Load Cell Weight Sensor

50kg Load Cell Weight Sensor

Zbuduj swój projekt Arduino lub ESP32 z pomocą AI

Opisz, co chcesz zbudować. Codey Online napisze kod, narysuje schemat połączeń, skompiluje projekt i wgra go na płytkę bezpośrednio z przeglądarki.

Zacznij za darmo