SKILL BUILDER

Learning new tricks every issue

Written by Mikal Hart

Image

For makers, it has become quite cheap to incorporate high-quality geospatial data into electronics projects. And in the last few years, GPS receiver modules have grown much more diverse, powerful, and easy to integrate with development boards like Arduino, PIC, Teensy, and Raspberry Pi. If you’ve been thinking of building around GPS, you’ve picked a good time to get started.

EASY

Image

COUNTING STARS

One evening I built a little Arduino/GPS test gizmo to spy on the GPS satellite constellation. I was able to count all 32 distinct satellites over a 24-hour period, with as many as 13 visible at once. For more info: makezine.com/gps

Image

FUN FACT:

GPS could not work without Einstein’s theory of relativity, as compensation must be made for the 38 microseconds the orbiting atomic clocks gain each day from time dilation in Earth’s gravitational field.

Time Required: 2 Hours Cost: $75–$150

A quick exercise in understanding and applying GPS data.

MIKAL HART

(arduiniana.org) is a senior software engineer at Intel Corp. in Austin, Texas. He is the inventor of the Reverse Geocache Puzzle and a founder of The Sundial Group. He has written about electronics development and prototyping for MAKE and for several books.

Whatever your project, GPS is simple to integrate. Most receiver modules communicate with a straightforward serial protocol, so if you can find a spare serial port on your controller board, it should take just a handful of wires to make the physical connection. And even if not, most controllers support an emulated “software” serial mode that you can use to connect to arbitrary pins.

For beginners, Adafruit’s Ultimate GPS Breakout module is a good choice. There are a lot of competing products on the market, but the Ultimate is a solid performer at a reasonable price, with big through-holes that are easy to solder or connect to a breadboard.

Image

The Adafruit Ultimate GPS Breakout board. A typical GPS interface, assuming both module and main board run at compatible voltages, is as simple as connecting four wires.

First, connect ground and power. In Arduino terms, this means connecting one of the microcontroller GND pins to the module’s GND, and the +5V pin to the module’s VIN.

To manage data transfer, you also need to connect the module’s TX and RX pins to the Arduino. I’m going to arbitrarily select Arduino pins 2 (TX) and 3 (RX) for this purpose, even though pins 0 and 1 are specifically designed for use as a “hardware serial port” or UART.

Why? Because I don’t want to waste the only UART these low-end AVR processors have. Arduino’s UART is hard-wired to the onboard USB connector, and I like to keep it connected to my computer for debugging.

MATERIALS

» Arduino Uno or compatible microcontroller/single-board computer. Maker Shed item #MKSP99, makershed.com

» GPS module such as Adafruit’s Ultimate GPS Breakout. Maker Shed #MKAD47

Tools

» Computer PC, Mac, or Linux. Laptop preferred.

» Soldering iron and solder may be required to attach header pins to your GPS module

» Solderless breadboard

» Jumper wires

NOTE:

The sketch defines the receive pin (RXPin) as 2, even though we said earlier that the transmit (TX) pin would be connected to pin 2. This is a common source of confusion. RXPin is the receive pin (RX) from the Arduino’s point of view. Naturally, it must be connected to the module’s transmit (TX) pin, and vice versa.

Sketch 1: A Toe in the Datastream

The instant you apply power, a GPS module begins sending chunks of text data on its TX line. It may not yet see a single satellite, much less have a “fix,” but the data faucet comes on right away, and it’s interesting to see what comes out. Our first simple sketch (Figure 1) does nothing but display this unprocessed data.

Image

#include <SoftwareSerial.h>
#define RXPin 2
#define TXPin 3
#define GPSBaud 4800
#define ConsoleBaud 115200

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(ConsoleBaud);
  ss.begin(GPSBaud);

  Serial.println("GPS Example 1");
  Serial.println("Displaying the raw NMEA data transmitted by GPS module.");
  Serial.println("by Mikal Hart");
  Serial.println();
}

void loop()
{
if (ss.available() > 0) // As each character arrives...
  Serial.write(ss.read()); // ... write it to the console.
}

Upload this sketch and open Serial Monitor at 115,200 baud. If everything’s working, you should see a dense, endless stream of comma-separated text strings. Each will look something like Figure 2.

Image

Image GPRMC is probably the most common NMEA sentence. It contains 12 comma-separated fields, followed by an asterisk (*) and a checksum to ensure data integrity.

These distinctive strings are known as NMEA sentences, so called because the format was invented by the National Maritime Electronics Association. NMEA defines a number of these sentences for navigational data ranging from the essential (location and time), to the esoteric (satellite signal-to-noise ratio, magnetic variance, etc.). Manufacturers are inconsistent about which sentence types their receivers use, but GPRMC is essential. Once your module gets a fix, you should see a fair number of these GPRMC sentences.

Sketch 2: Finding Yourself

It’s not trivial to convert the raw module output into information your program can actually use. Fortunately, there are some great libraries already available to do this for you. Limor Fried’s popular Adafruit GPS Library is a convenient choice if you’re using their Ultimate breakout. It’s written to enable features unique to the Ultimate (like internal data logging) and adds some snazzy bells and whistles of its own.

My favorite parsing library, however — and here I am of course completely unbiased — is the one I wrote called TinyGPS++. I designed it to be comprehensive, powerful, concise, and easy to use. Let’s take it for a spin.

CODING WITH TINYGPS++

From the programmer’s point of view, using TinyGPS++ is very simple:

1. Create an object gps.

2. Route each character that arrives from the module to the object using gps.encode().

3. When you need to know your position or altitude or time or date, simply query the gps object.

Our second application continually displays the receiver’s location and altitude, using TinyGPS++ to help with parsing. In a real device, you might log this data to an SD card or display it on an LCD.

Grab the library and sketch FindingYourSelf.ino from makezine.com/gps. Install the library, as usual, in the Arduino libraries folder. Upload the sketch to your Arduino and open Serial Monitor at 115,200 baud. You should see your location and altitude updating in real time. To see exactly where you stand, paste some of the resulting latitude/longitude coordinates into Google Maps. Now hook up your laptop and go for a stroll or a drive. (But remember to keep your eyes on the road!)

Sketch 3: Finding Your Way

Our third and final application is the result of a personal challenge to write a readable TinyGPS++ sketch, in fewer than 100 lines of code, that would guide a user to a destination using simple text instructions like “keep straight” or “veer left.”

Every 5 seconds the code captures the user’s location and course (direction of travel) and calculates the bearing (direction to the destination), using the TinyGPS++ courseTo() method. Comparing the two vectors generates a suggestion to keep going straight or turn, as shown in Figure 3.

Image

How FindingYourself.ino translates your course and bearing into text directions. The sketch can become the starting point for creating almost any type of autonomous or semi-autonomous vehicle.

Download the sketch Finding YourWay.ino from makezine.com/gps and open it in the Arduino IDE. Set a destination 1km or 2km away, upload the sketch to your Arduino, run it on your laptop, and see if it will guide you there. But more importantly, study the code and understand how it works.

Going Further

A popular first project idea is some sort of logging device that records the minute-by- minute position and altitude of, say, a hiker walking the Trans- Pennine Trail. Or what about one of those sneaky magnetic trackers the DEA agents in Breaking Bad stick on the bad guys’ cars?

Both are totally feasible, and would probably be fun to build, but I encourage you to think more expansively, beyond stuff you can already buy on Amazon. It’s a big world out there. Roam as far and wide as you can. Image

THE FOURTH DIMENSION

The creative potential of GPS is vast. One of the most satisfying things I ever made was a GPS-enabled puzzle box that opens only at one preprogrammed location. If your victim wants to get the treasure locked inside, she has to figure out where that secret location is and physically bring the box there. (See The Reverse Geocache Puzzle, MAKE Volume 25.)

Though we associate GPS with location in space, don’t forget those satellites are transmitting time- and date-stamps, too. The average GPS clock is accurate to one ten-millionth of a second, and the theoretical limit is even higher. Even if you only need your project to keep track of time, a GPS module may still be the cheapest and easiest solution.

To turn FindingYourself.ino into a super-accurate clock, just change the last few lines like this:

if (gps.time.isUpdated()) {
   char buf[80];
   sprintf(buf, "The time
is %02d:%02d:%02d", gps.time.
hour(), gps.time.minute(),
gps.time.second());
  Serial.println(buf);
}

Find the codes and full steps at makezine.com/gps Share it: #makegps

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset