STANNUM
Open the simulator

Tutorials / Sensors

GPS receiver (NEO-6M)

A module that talks on its own: NMEA sentences on a serial port, once a second, decoded with TinyGPS++.

Parts in the example:
  • GPS NEO-6M
The GPS NEO-6M block as it appears on the canvas.

What it is

A GPS module is a computer of its own: it finds satellites and, once a second, sends its position as text lines in the NMEA format over a serial port at 9600 baud — nobody asks, it just talks. The firmware reads the port and decodes the sentences; TinyGPS++ does the decoding.

In the simulator

The block's sliders are what a moving receiver would report: Spd (km/h), Hdg (course) and Sat (satellites in view — fewer than 3 gives no fix, and the sentences go out empty, as the real module does). The position itself — latitude, longitude, altitude — is in Settings, Brasília by default, and so is the UART the bytes arrive on.

Pins

PinWhat it is
TXthe module's transmit line — to the GPIO the sketch uses as Serial2's RX (16 in the example)
RXthe module's receive line — unused here; the simulated module ignores configuration
VCCpower — to a 3V3 symbol
GNDground — to a GND symbol

Wiring

The circuit below is the example GPS tracker from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
GPS NEO-6M pinGoes to
TXRX2 on the board (GPIO 16)
RXleft unconnected
VCCa 3V3 symbol
GNDa GND symbol

Settings

Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.

The Settings tab of the GPS NEO-6M.
SettingAcceptsDefaultNotes
Latitude-90 to 90 °-15.7939 °Decimal degrees; negative = southern hemisphere. The default is Brasilia.
Longitude-180 to 180 °-47.8828 °Decimal degrees; negative = west of Greenwich.
Altitude-100 to 9000 m1172 m
ESP32 UARTSerial2 (the usual one for GPS) · Serial1Serial2 (the usual one for GPS)The sketch picks the GPIOs in begin(); the simulation delivers the bytes on the UART chosen here.

Code

sketch.cpp
#include <Arduino.h>
#include <TinyGPSPlus.h>

// The NEO-6M talks on its own: NMEA 0183 at 9600 baud, one packet per second.
// On the ESP32 Serial2 listens on any GPIOs — here RX=16 (the module's TX).
TinyGPSPlus gps;

// landmark to measure the distance to: Praca dos Tres Poderes, in Brasilia
const double MARCO_LAT = -15.8002, MARCO_LON = -47.8610;

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, 16, 17);
  Serial.println("waiting for the first NMEA packet...");
}

void loop() {
  while (Serial2.available()) gps.encode(Serial2.read());

  static unsigned long antes = 0;
  if (millis() - antes < 1000) return;
  antes = millis();

  // CLASSIC TinyGPS gotcha: isValid() latches on the last good value and
  // never goes back to false. What tells you the fix DROPPED is the AGE of the
  // data — with no satellites the module stops sending position and age() climbs.
  if (!gps.location.isValid() || gps.location.age() > 3000) {
    Serial.printf("no fix (%d satellites)\n", (int)gps.satellites.value());
    return;
  }
  double dist = TinyGPSPlus::distanceBetween(
      gps.location.lat(), gps.location.lng(), MARCO_LAT, MARCO_LON);
  Serial.printf("%.4f, %.4f | %.0f km/h heading %.0f | %d sat | alt %.0f m | %.1f km to landmark\n",
                gps.location.lat(), gps.location.lng(),
                gps.speed.kmph(), gps.course.deg(),
                (int)gps.satellites.value(), gps.altitude.meters(),
                dist / 1000.0);
}

Serial2.begin(9600, SERIAL_8N1, 16, 17) opens the second serial port on the pins wired to the module; gps.encode() eats every byte. The example prints position, speed, heading, satellites, altitude and the distance to a landmark. Read its comment about isValid(): it never goes back to false — the age of the data is what tells you the fix was lost.

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

Every second the serial monitor prints a line with the coordinates and the distance to the landmark. Drag Sat below 3: after three seconds the sketch reports “no fix”. Raise it again and the position returns.

How the simulation models it

Try this

  1. Show the coordinates on the LCD, updated once a second.
  2. Log a line to the SD card on every fix: a track you could plot on a map.

See also