This project shows how to get location in latitude and longitude coordinates using an electronic circuit built with the
Arduino Mega 2560
and the
ITEAD GPS Shield v1.1
. If you want to see how to do GPS location sensing with the Arduino Uno and an LCD display check out my other blog.
The GPS shield has a
GPS antenna
connected. This is very important as you can't get a location fix if you don't have an antenna.
Using this circuit I got a location fix indoors on the ground floor of a 2 story house in 1 to 2 minutes. Outdoors it got a fix in about 30s or less. Accuracy was very good, to within 10-20ft initially, and improving the longer you leave it (it gets a new reading every second.
The wiring of this diagram is straightforward. First you should set the voltage switch on your GPS shield to 5v before you power it on and connect it up. There is also a 3.3v setting, but for our circuit we are using the Arduino Mega 2560 so the voltage setting I used is 5v. Next you need to set the jumpers for Rx (receive) and Tx (transmit) on the GPS shield. I set my GPS Rx to pin 6 and GPS Tx to pin 5. I wanted to receive the output of this circuit on my computer so I needed the default hardware serial interface for that. To communicate with the GPS shield I used the mega hardware serial interface 1. So I wired GPS Rx pin 6 to Mega Serial1 Tx1 pin 18, and GPS Tx pin 5 to Mega Serial 1 Rx1 pin 19. Using this circuit I was able to get a location fix even inside a house on the ground floor of a 2 story house. If used outside with a clear view of the sky it will get a GPS fix even faster.
You will need TinyGPS to run the sketch I used. Specifically you will need the TinyGPS.cpp and TinyGPS.h files to run this. I just dropped them in the same folder as the Arduino sketch and then when I loaded the sketch the Arduino IDE automatically found these two files and compiled and uploaded them with my sketch to the Mega for testing.
Below is a screenshot of the Arduino IDE serial console showing output from this sketch running. The GPS latitude / longitude coordinates each have a precision of 6 decimal places.
Below is the Arduino sketch I used for testing:
#include "TinyGPS.h" TinyGPS gps; #define GPS_TX_DIGITAL_OUT_PIN 5 #define GPS_RX_DIGITAL_OUT_PIN 6 long startMillis; long secondsToFirstLocation = 0; #define DEBUG float latitude = 0.0; float longitude = 0.0; void setup() { #ifdef DEBUG Serial.begin(19200); #endif // Serial1 is GPS Serial1.begin(9600); // prevent controller pins 5 and 6 from interfering with the comms from GPS pinMode(GPS_TX_DIGITAL_OUT_PIN, INPUT); pinMode(GPS_RX_DIGITAL_OUT_PIN, INPUT); startMillis = millis(); Serial.println("Starting"); } void loop() { readLocation(); } //-------------------------------------------------------------------------------------------- void readLocation(){ bool newData = false; unsigned long chars = 0; unsigned short sentences, failed; // For one second we parse GPS data and report some key values for (unsigned long start = millis(); millis() - start < 1000;) { while (Serial1.available()) { int c = Serial1.read(); // Serial.print((char)c); // if you uncomment this you will see the raw data from the GPS ++chars; if (gps.encode(c)) // Did a new valid sentence come in? newData = true; } } if (newData) { // we have a location fix so output the lat / long and time to acquire if(secondsToFirstLocation == 0){ secondsToFirstLocation = (millis() - startMillis) / 1000; Serial.print("Acquired in:"); Serial.print(secondsToFirstLocation); Serial.println("s"); } unsigned long age; gps.f_get_position(&latitude, &longitude, &age); latitude == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : latitude; longitude == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : longitude; Serial.print("Location: "); Serial.print(latitude, 6); Serial.print(" , "); Serial.print(longitude, 6); Serial.println(""); } if (chars == 0){ // if you haven't got any chars then likely a wiring issue Serial.println("Check wiring"); } else if(secondsToFirstLocation == 0){ // still working } }If you are interested in how location sensing can be integrated into a broader solution that includes notification of location and geo-fence alerts on a users Android phone see Geo-Fencing
Hope you found this useful. Let me know if you create any cool enhancements to it. Have fun!
If you need any of the parts for this projects you can find them below: