StratoLab

Lesson 4: GPS Sensor

Working with GPS sensor data using the Arduino language

Pre-requisites:

Objectives:

What you will be using:

Note: I had some issues using the GPS module from inside my garage. If you do not get data on your serial monitor. Here are some things you can try to get good results…

</br>

Tips

What you will be learning:

Video Walk-through

In addition to the reading below, you can watch this video for guidance!

Tips

Pinout chart

Pin on the GPS module | Pin on the Arduino ————— | ————— TXD | Digital pin 11 RXD | Digital pin 10 GND | Any Ground pin VDC | 5 volts </br>

Lesson Four

How to get the TinyGPSPlus library:

Steps

#include "TinyGPSPlus.h"
#include <SoftwareSerial.h>

// Create the serial_connection
//
// Connect the GT-U7 RX and TX to the Arduino
// using the following connections...
// RX=pin --> Arduino analog 10
// TX pin --> Arduino analog 11
//
// In the code below on line 14 is the constructor.
// use the RX pin number as the first argument
// use the TX pin number as the second argument
SoftwareSerial serial_connection(10, 11);

TinyGPSPlus gps;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  serial_connection.begin(9600);
  Serial.println("GPS Start");
}

void loop() {
  // put your main code here, to run repeatedly:
  while(serial_connection.available())
  {
    gps.encode(serial_connection.read());
  }
  if(gps.location.isUpdated())
  {
    Serial.println("Satellite Count:");
    Serial.println(gps.satellites.value());
    Serial.println("Latitude:");
    Serial.println(gps.location.lat(), 6);
    Serial.println("Longitude:");
    Serial.println(gps.location.lng(), 6);
    Serial.println("Speed MPH:");
    Serial.println(gps.speed.mph());
    Serial.println("Altitude Feet:");
    Serial.println(gps.altitude.feet());
    Serial.println("");
  }
}

Review

Want more?

If you have finished with the base lesson, check out the items below.

Things to think about, validate, and/or try:

Update the code to do any/all of the following:

Challenge

If you have finished with the extension lesson questions, check out the challenge below.

Use a red and green LED to show if the GPS sensor is transmitting within our outside a circle at your current location. The green LED should light solid inside the circle and solid red outside.

Things to think about:

Try to write the code on your own. If you get stuck or need some inspiration expand this section. ```java #include "TinyGPSPlus.h" #include // Create the serial_connection // // Connect the GT-U7 RX and TX to the Arduino // using the following connections... // RX=pin --> Arduino analog 10 // TX pin --> Arduino analog 11 // // In the code below on line 14 is the constructor. // use the RX pin number as the first argument // use the TX pin number as the second argument SoftwareSerial serial_connection(10, 11); TinyGPSPlus gps; // Set home location static const float homeLat=40.541800, homeLong=-88.949430; // GPS scale factor and radius (Meters) static const int gpsFactor=111000, gpsRadius=2; void setup() { pinMode(12,OUTPUT); // Red pinMode(13,OUTPUT); // Green Serial.begin(9600); serial_connection.begin(9600); Serial.println("GPS Start"); } void loop() { // Variable to hold GPS sensor lat/long float currentLat, currentLong; while(serial_connection.available()) { gps.encode(serial_connection.read()); } if(gps.location.isUpdated()) { Serial.println("Latitude:"); Serial.println(gps.location.lat(), 6); currentLat = gps.location.lat(); Serial.println("Longitude:"); Serial.println(gps.location.lng(), 6); currentLong = gps.location.lng(); // the point is inside the circle if d2<r2 // on the circle if d2=r2 // and outside the circle if d2>r2 // Thus, you want to compare the number (x2−x1)2+(y2−y1)2 with r2 float distanceFromHome = abs(sqrt(sq(currentLat-homeLat)+sq(currentLong-homeLong))*gpsFactor); Serial.println(distanceFromHome, 6); if (distanceFromHome <= gpsRadius) // Green { digitalWrite(13, HIGH); digitalWrite(12, LOW); } else if (distanceFromHome > gpsRadius) // Red { digitalWrite(13, LOW); digitalWrite(12, HIGH); } Serial.println("---"); } } ``` </details> ## Troubleshooting - If your GPS is not reading out coordinates on the COM port - you may have to move outside - move close to a window - wait for a satellite(s) to be acquired - You need to have the TinyGPSPlus library installed in order for the code to work, refer to the video further up this page for more help - The GPUs unit should have a solid red light when looking for satellites and start blinking when it connects with a satellite(s) - Swap the two data wires (TXD and RXD) </br></br> ### Need help? Watch the walk-through [video](assets/videos/Lesson4.mp4?raw=true) for guidance!