This article is the sixth in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.

Hardware

  • Arduino Uno
  • Location Detection Sensor (GPS)
  • Jumper Cables

CodifyThings - Internet of Things (IoT) Sensors

Software

Circuit

Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on the breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the GPS.

Step 4: To read coordinates from GPS, you will need to connect a jumper cable from RX (Receive) port of GPS to D3 (Digital) port of Arduino.

Step 5: Similar to step 4, you will also need to connect a jumper cable from TX (Transmit) port of GPS to D2 (Digital) port of Arduino.

Note: Depending on your GPS module you might need to add transistors in the circuit as well, please check specifications of your GPS module.

Your circuit is now complete and it should look similar to figures below.

CodifyThings - Internet of Things (IoT) Sensors

Arduino Code

Next, you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
    • <TinyGPS.h> – https://github.com/mikalhart/TinyGPS/releases/tag/v13

  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()

[code lang=”js”]
/***************************************************************************
* External Libraries
**************************************************************************/

#include <SPI.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>

/*****************************************************************************
* Sensor Setup – Variables & Functions
****************************************************************************/

TinyGPS gps;
SoftwareSerial ss(2, 3); // GPS TX = Arduino D2, GPS RX = Arduino D3

static void smartdelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() – start < ms);
}

void getGPSCoordinates()
{
float latitude;
float longitude;
unsigned long age = 0;

gps.f_get_position(&latitude, &longitude, &age);

// Transmit sensor data
if(latitude != TinyGPS::GPS_INVALID_F_ANGLE &&
longitude != TinyGPS::GPS_INVALID_F_ANGLE)
{
Serial.print("[INFO] Latitude: " + String(latitude));
Serial.println(", Longitude: " + String(longitude));
}
else
{
Serial.println("[INFO] Searching for Satellite");
}

smartdelay(10000);
}

/***************************************************************************
* Standard Arduino Functions – setup(), loop()
**************************************************************************/

void setup()
{
// Initialize serial port
Serial.begin(115200);

// Initialize serial port for GPS data
ss.begin(9600);

}

void loop()
{
// Get GPS Coordinates
getGPSCoordinates();
}
[/code]

Final Product

Make sure your Arduino is powered on and code from above has been deployed. As soon as the code has been deployed, open serial monitor.

CodifyThings - Internet of Things (IoT) Sensors