How to Use GPS Module with Arduino: Simple Guide and Example
To use a
GPS module with an Arduino, connect the module's TX and RX pins to Arduino's RX and TX pins (usually via SoftwareSerial). Then, use a GPS library like TinyGPS++ to read and parse GPS data from the serial input.Syntax
Here is the basic syntax to set up communication between Arduino and a GPS module using the SoftwareSerial library and TinyGPS++ library:
SoftwareSerial gpsSerial(rxPin, txPin);- Creates a serial connection on specified pins.TinyGPSPlus gps;- Creates a GPS parser object.gpsSerial.begin(9600);- Starts serial communication with GPS module at 9600 baud rate.gps.encode(gpsSerial.read());- Reads and parses GPS data byte by byte.gps.location.lat()andgps.location.lng()- Get latitude and longitude.
arduino
#include <SoftwareSerial.h> #include <TinyGPS++.h> // Define pins for GPS module const int rxPin = 4; const int txPin = 3; // Create SoftwareSerial object SoftwareSerial gpsSerial(rxPin, txPin); // Create TinyGPS++ object TinyGPSPlus gps; void setup() { Serial.begin(9600); // Serial monitor gpsSerial.begin(9600); // GPS module } void loop() { while (gpsSerial.available() > 0) { gps.encode(gpsSerial.read()); } if (gps.location.isUpdated()) { Serial.print("Latitude: "); Serial.println(gps.location.lat(), 6); Serial.print("Longitude: "); Serial.println(gps.location.lng(), 6); } }
Example
This example shows how to connect a GPS module to Arduino pins 4 (RX) and 3 (TX), read GPS data, and print latitude and longitude to the Serial Monitor.
Make sure to install the TinyGPS++ library via Arduino Library Manager before running.
arduino
#include <SoftwareSerial.h> #include <TinyGPS++.h> const int rxPin = 4; const int txPin = 3; SoftwareSerial gpsSerial(rxPin, txPin); TinyGPSPlus gps; void setup() { Serial.begin(9600); gpsSerial.begin(9600); Serial.println("GPS Module Example"); } void loop() { while (gpsSerial.available() > 0) { gps.encode(gpsSerial.read()); } if (gps.location.isUpdated()) { Serial.print("Latitude: "); Serial.println(gps.location.lat(), 6); Serial.print("Longitude: "); Serial.println(gps.location.lng(), 6); } }
Output
GPS Module Example
Latitude: 37.774929
Longitude: -122.419416
Common Pitfalls
- Wrong wiring: Connect GPS TX to Arduino RX and GPS RX to Arduino TX, not the same pins.
- Baud rate mismatch: Most GPS modules use 9600 baud; check your module's specs.
- Not using SoftwareSerial: Arduino Uno has one hardware serial; use SoftwareSerial for GPS to avoid conflicts.
- Not parsing data correctly: Use a GPS library like TinyGPS++ to decode NMEA sentences.
- Power issues: GPS modules need stable 3.3V or 5V power; check your module's voltage requirements.
arduino
/* Wrong wiring example (DO NOT use):
GPS TX -> Arduino TX
GPS RX -> Arduino RX
Correct wiring:
GPS TX -> Arduino RX (e.g., pin 4)
GPS RX -> Arduino TX (e.g., pin 3)
*/Quick Reference
| Step | Description |
|---|---|
| Connect GPS TX to Arduino RX pin (e.g., 4) | Receive GPS data on Arduino |
| Connect GPS RX to Arduino TX pin (e.g., 3) | Send data if needed (usually not required) |
| Use SoftwareSerial to read GPS data | Avoid conflicts with Arduino hardware serial |
| Install and include TinyGPS++ library | Parse GPS NMEA sentences easily |
| Read and decode GPS data in loop | Get latitude and longitude values |
| Print data to Serial Monitor | View GPS coordinates |
Key Takeaways
Connect GPS TX to Arduino RX and use SoftwareSerial for communication.
Use the TinyGPS++ library to parse GPS data easily.
Match the GPS module baud rate (usually 9600) with your code.
Check wiring and power supply carefully to avoid issues.
Print GPS coordinates to Serial Monitor to verify data.