MKR GPS Shield Basics

Learn how to access GPS data from the module on board the MKR GPS Shield.

Introduction

The ability to pinpoint your exact location can be very useful for different types of projects. With the MKR GPS Shield, we can reach high accuracy with minimal power consumption.

In this tutorial, we will use a very basic example from the Arduino_MKRGPS library, which records different geolocation data directly from the GPS shield, and prints them in the Serial Monitor.

Goals

The goals of this project are:

  • Set up the MKR GPS Shield.
  • Record longitude, latitude, speed and altitude.
  • Print the data in the Serial Monitor.

Hardware & Software Needed

Global Positioning System (GPS)

The GPS is an incredible technology that is used to pinpoint an exact geographical location. Even more interesting, it uses a series of satellites orbiting the Earth to do so. Basically, a satellite in orbit continuously sends signals towards the Earth, which are picked up by GPS receivers, that exist in e.g. smartphones. As the satellite has a positioning system, it knows where it is, relative to the Earth. But this signal can only pinpoint where you are in a certain part of the world, e.g. in the Atlantic Ocean or Asia. For more accuracy, data from other satellites are also used, where for every satellite the accuracy increases.

It is quite spectacular, that something 20.000 kilometers above the Earth can pinpoint your exact location. What is even more interesting, is that we can create our own projects using this technology. The MKR GPS shield can get extremely accurate readings on where we are in the world. This can be used to first locate where we are in the world, but we can also use it to record for example speed.

There is, of course, much more behind the GPS technology. If we want to read more, why not start at NASA's own article on how GPS technology works.

Circuit

The circuit in this tutorial is pretty simple, and the simplest assembly is to connect an I2C cable from a MKR board directly to the MKR GPS Shield. This cable comes with every GPS shield, and has an ESLOV connector at each

Simple circuit of board and GPS shield.
Simple circuit of board and GPS shield.

If you are using a third party cable to connect the MKR board and MKR GPS Shield via I2C, make sure the cable is wired identically to the cable that comes with the shield.

Programming the Board

We will now get to the programming part of this tutorial.

1. First, let's make sure we have the drivers installed for the board we are using. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) and install it.

2. Now, we need to install the library needed. If we are using the Web Editor, there is no need to install anything. If we are using an offline editor, simply go to Tools > Manage libraries.., and search for Arduino_MKRGPS and install it.

3. Here are some of the core functions of this sketch:

  • GPS.begin
    - initializes the GPS library.
  • GPS.available()
    - checks for available GPS data from the module.
  • GPS.latitude()
    - records latitude.
  • GPS.longitude()
    - records longitude.
  • GPS.altitude()
    - records altitude.
  • GPS.speed()
    - records speed in km/h.

The sketch can be found in the snippet below. Upload the sketch to the board.

1#include <Arduino_MKRGPS.h>
2
3void setup() {
4 // initialize serial communications and wait for port to open:
5 Serial.begin(9600);
6 while (!Serial) {
7 ; // wait for serial port to connect. Needed for native USB port only
8 }
9
10 // If you are using the MKR GPS as shield, change the next line to pass
11 // the GPS_MODE_SHIELD parameter to the GPS.begin(...)
12 if (!GPS.begin()) {
13 Serial.println("Failed to initialize GPS!");
14 while (1);
15 }
16}
17
18void loop() {
19 // check if there is new GPS data available
20 if (GPS.available()) {
21 // read GPS values
22 float latitude = GPS.latitude();
23 float longitude = GPS.longitude();
24 float altitude = GPS.altitude();
25 float speed = GPS.speed();
26 int satellites = GPS.satellites();
27
28 // print GPS values
29 Serial.print("Location: ");
30 Serial.print(latitude, 7);
31 Serial.print(", ");
32 Serial.println(longitude, 7);
33
34 Serial.print("Altitude: ");
35 Serial.print(altitude);
36 Serial.println("m");
37
38 Serial.print("Ground speed: ");
39 Serial.print(speed);
40 Serial.println(" km/h");
41
42 Serial.print("Number of satellites: ");
43 Serial.println(satellites);
44
45 Serial.println();
46 }
47}

Testing It Out

After we have uploaded the code to the board, we need to open the Serial Monitor to start the program. It is a good idea to have our setup close to a window. Once the data is available, it will start printing the values in the Serial Monitor.

GPS data printed in the Serial Monitor.
GPS data printed in the Serial Monitor.

Note: This process may take some time. If no data is available after 5 minutes or so, try moving your device closer to a window.

Once we start getting the values, we can double check that they are alright. For example,

longitude
and
latitude
should be very accurate, and be able to pinpoint almost your exact location. You can enter the coordinates in Google Maps for example, to see if the values match.

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • We have not installed the MKRGPS library.
  • We have selected the wrong port and board to upload to.
  • We have not installed the right Board Package for the board we are using.
  • We need to move the MKR GPS Shield closer to a window.

Conclusion

In this tutorial, we learned how to retrieve accurate location data from the MKR GPS Shield. We managed to retrieve a lot of useful data and print it in the Serial Monitor. With it, we can ideate some pretty cool ideas, such as speedometer, tracking device or maybe a geolocation game. GPS is a great technology that can retrieve data from anywhere in the world as long it has clear sight, which makes it very useful for remote projects.

Feel free to explore the Arduino_MKRGPS library further, and try out some of the many cool functions.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.