This guide will show you how to make a simple GPS with Arduino.
What you will need
GPS Neo-6M
This will be used to determine the location.
Arduino UNO Rev3
This will be what we use to control all the other components. Think of this as the motherboard of the build.
Arduino IDE
This will be used to program the Arduino
About GPS
Before we start this project, you need to know a little bit about GPS. Satellites will send signals on their distance from the module. Once four or more satellites are connected and giving out this data, the receiver can then use the data to figure out the exact location of the user.
The receiver will then present this data in the form of NMEA sentences. This is a standard communication developed by the National Marine Electronics Association. We will be using TinyGPS++ to parse these.
Using the GPS with Arduino
For this part, you don’t need the LCD. This will show you how to log the GPS output to the serial monitor. We will then parse this data using TinyGPS++.
Preparations
First, open Arduino IDE and you will be greeted with a blank sketch.
At the top toolbar, click Ctrl + Shift + I to bring up the library manager and type “TinyGPSPlus” and install the top result.
Sign up for our newsletter!
Code
Now that we are all prepared, lets start writing the code. First, we include the library that helps us communicate with the GPS.
#include <SoftwareSerial.h>
Next, we include the library that parses NMEA sentences.
#include <TinyGPSPlus.h>
Now, declare the communication between the Arduino and the GPS and then the parser.
SoftwareSerial ss(4,3);
TinyGPSPlus gps;
After that, we go inside the void setup()
function and we initiate the communication between the computer and the Arduino and the GPS and Arduino.
Serial.begin(9600);
ss.begin(9600);
Next, we go into void loop()
and specify that whatever is below this line of code should only happen when the Arduino receives a signal.
while (ss.available() > 0)
Then, we encode the data in a format that the GPS can then parse.
gps.encode(ss.read());
Then, we create an if
block so the serial monitor only displays our data when the data the GPS is outputting is valid.
if (gps.location.isUpdated()) {
}
Now, inside the if
block, we can access all the data and print it to the serial monitor.
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6); //6 for 6 decimal places
Serial.print("Longitude= ");
Serial.print(gps.location.lng(), 6); //6 for 6 decimal places
Your full code should look like this:
#include <SoftwareSerial.h>
#include <TinyGPSPlus.h>
SoftwareSerial ss(4,3);
TinyGPSPlus gps;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ss.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
while (ss.available() > 0)
gps.encode(ss.read());
if (gps.location.isUpdated()) {
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6); //6 for 6 decimal places
Serial.print("Longitude= ");
Serial.print(gps.location.lng(), 6); //6 for 6 decimal places
}
}
Wiring
The wiring is shown below:
- GPS RX > Digital 4 on Arduino
- GPS TX > Digital 3 on Arduino
- GPS VCC > Power 3.3V on Arduino
- GPS GND > Power GND on Arduino
Uploading
Now, with the Arduino IDE opened and the code ready, press Ctrl + U on your keyboard. The code will write and start outputting to the serial monitor, which you can access by pressing Ctrl + Shift + M on your keyboard or by going to the top toolbar and clicking Tools > Serial Monitor. The GPS will take a couple minutes to get its location. You may want to stick the antenna outside for this, as it will take a long time to get its location indoors.
Soon, you will be able to view the data coming in.