Arduino UNO R4 - GPS

This tutorial instructs you how to use Arduino Uno R4 with GPS module. In detail, we will learn how to find GPS coordinates (longitude, latitude, altitude), GPS speed (in kilometers per hour), and the current date and time from a NEO-6M GPS module. We will also learn how to calculate the distance from our current GPS location to specific coordinates, like those of London.

Arduino UNO R4 GPS module

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×NEO-6M GPS module
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of NEO-6M GPS module

Pinout

The NEO-6M GPS module has 4 pins:

  • VCC pin: Connect this pin to VCC (5V).
  • GND pin: Connect this pin to GND (0V).
  • TX pin: This is used for serial communication. Connect it to the RX pin of the Serial (or SoftwareSerial) on Arduino UNO R4.
  • RX pin: This is used for serial communication. Connect it to the TX pin of the Serial (or SoftwareSerial) on Arduino UNO R4.
NEO-6M GPS module Pinout

Wiring Diagram

The wiring diagram between Arduino UNO R4 GPS module

This image is created using Fritzing. Click to enlarge image

Please note that the wiring diagram shown above may function, but it is not recommended. The Arduino UNO R4's TX pin sends out a 5V signal, while the GPS module's RX pin can only handle 3.3V. For safety, you should use a voltage divider between the Arduino UNO R4's TX pin and the RX pin of the GPS module. This setup is illustrated in the diagram below.

The wiring diagram between Arduino UNO R4 GPS

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

Reading GPS coordinates, speed (km/h), and date time

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-gps */ #include <TinyGPS++.h> #include <SoftwareSerial.h> #define RX_PIN 4 // The Arduino UNO R4 pin connected to the TX of the GPS module #define TX_PIN 3 // The Arduino UNO R4 pin connected to the RX of the GPS module TinyGPSPlus gps; // the TinyGPS++ object SoftwareSerial gpsSerial(RX_PIN, TX_PIN); // the serial interface to the GPS module void setup() { Serial.begin(9600); gpsSerial.begin(9600); // Default baud of NEO-6M GPS module is 9600 Serial.println(F("Arduino - GPS module")); } void loop() { if (gpsSerial.available() > 0) { if (gps.encode(gpsSerial.read())) { if (gps.location.isValid()) { Serial.print(F("- latitude: ")); Serial.println(gps.location.lat()); Serial.print(F("- longitude: ")); Serial.println(gps.location.lng()); Serial.print(F("- altitude: ")); if (gps.altitude.isValid()) Serial.println(gps.altitude.meters()); else Serial.println(F("INVALID")); } else { Serial.println(F("- location: INVALID")); } Serial.print(F("- speed: ")); if (gps.speed.isValid()) { Serial.print(gps.speed.kmph()); Serial.println(F(" km/h")); } else { Serial.println(F("INVALID")); } Serial.print(F("- GPS date&time: ")); if (gps.date.isValid() && gps.time.isValid()) { Serial.print(gps.date.year()); Serial.print(F("-")); Serial.print(gps.date.month()); Serial.print(F("-")); Serial.print(gps.date.day()); Serial.print(F(" ")); Serial.print(gps.time.hour()); Serial.print(F(":")); Serial.print(gps.time.minute()); Serial.print(F(":")); Serial.println(gps.time.second()); } else { Serial.println(F("INVALID")); } Serial.println(); } } if (millis() > 5000 && gps.charsProcessed() < 10) Serial.println(F("No GPS data received: check wiring")); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the Arduino Uno R4 the GPS module according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Go to the Libraries icon on the left side of the Arduino IDE.
  • Type TinyGPSPlus in the search box, then look for the TinyGPSPlus library by Mikal Hart
  • Click the Install button to add the TinyGPSPlus library.
Arduino UNO R4 TinyGPS++ library
  • Copy the code and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4.
  • Check the result on Serial Monitor.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Calculating the distance from current location to a predefined location

This code finds out how far you are from London (latitude: 51.508131, longitude: -0.128002).

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-gps */ #include <TinyGPS++.h> #include <SoftwareSerial.h> #define RX_PIN 4 // The Arduino UNO R4 pin connected to the TX of the GPS module #define TX_PIN 3 // The Arduino UNO R4 pin connected to the RX of the GPS module TinyGPSPlus gps; // the TinyGPS++ object SoftwareSerial gpsSerial(RX_PIN, TX_PIN); // the serial interface to the GPS module const double LONDON_LAT = 51.508131; const double LONDON_LON = -0.128002; void setup() { Serial.begin(9600); gpsSerial.begin(9600); // Default baud of NEO-6M GPS module is 9600 Serial.println(F("Arduino - GPS module")); } void loop() { if (gpsSerial.available() > 0) { if (gps.encode(gpsSerial.read())) { if (gps.location.isValid()) { double latitude = gps.location.lat(); double longitude = gps.location.lng(); unsigned long distanceKm = TinyGPSPlus::distanceBetween(latitude, longitude, LONDON_LAT, LONDON_LON) / 1000; Serial.print(F("- latitude: ")); Serial.println(latitude); Serial.print(F("- longitude: ")); Serial.println(longitude); Serial.print(F("- distance to London: ")); Serial.println(distanceKm); } else { Serial.println(F("- location: INVALID")); } Serial.println(); } } if (millis() > 5000 && gps.charsProcessed() < 10) Serial.println(F("No GPS data received: check wiring")); }

Detailed Instructions

  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino UNO R4.
  • Check the result on the Serial Monitor.
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!