搜索代码

Extracting Latitude and Longitude from a GPS Signal in Arduino

Extracting Latitude and Longitude from a GPS Signal in Arduino

In this tutorial, we will learn how to extract latitude and longitude data from a GPS signal using the Arduino and the Ublox NEO-6M GPS module. This project is particularly useful for applications that require location tracking, such as robotics or outdoor navigation. By the end of this tutorial, you will have a working program that reads GPS data and prints the coordinates to the serial monitor.

Before we dive into the details, you may want to refer to the associated video for further clarification on the wiring and code implementation (in video at 00:00).

Hardware Explained

The primary component for this project is the Ublox NEO-6M GPS module. This module is designed to provide accurate positioning data using signals from GPS satellites. It can receive signals from multiple satellites simultaneously, which allows for precise location tracking. The module communicates with the Arduino using serial communication, making it easy to integrate into various projects.

In addition to the GPS module, you will need an Arduino board to process the GPS data. The Arduino will read the data sent by the GPS module and extract the latitude and longitude values for further use. The GPS module typically operates at 5V, and it provides a serial output that the Arduino can read through its RX pin.

Datasheet Details

Manufactureru-blox
Part numberNEO-6M
Logic/IO voltage3.3V
Supply voltage3.0 to 5.5V
Output current≤ 50 mA
GPS sensitivity-165 dBm
Operating temperature-40 to 85 °C
Update rate1 Hz (default)
Package25 x 35 x 4 mm
Notes / variantsVarious configurations available

  • Ensure that the GPS module is connected to a stable power source (3.0 to 5.5V).
  • Use a suitable library to handle the GPS data parsing.
  • Keep the antenna clear of obstructions for better satellite reception.
  • Check the baud rate of the GPS module; it typically operates at 9600 baud.
  • Consider using a capacitor for power smoothing if the GPS module is unstable.

Wiring Instructions

Arduino wiring for Neo-6M GPS module
Arduino wiring for Neo-6M GPS module

To wire the NEO-6M GPS module to your Arduino, start by connecting the VCC pin of the GPS module to the 5V pin on the Arduino. Next, connect the GND pin of the GPS module to the ground (GND) pin on the Arduino. The GPS module will communicate with the Arduino using the TX and RX pins. Connect the TX pin of the GPS module to the RX pin (usually pin 0) on the Arduino.

If you are using an Arduino with dedicated I2C pins, ensure to connect the SDA and SCL pins appropriately (typically A4 for SDA and A5 for SCL on standard Arduino boards). After wiring, double-check all connections to ensure that they are secure and properly oriented.

Code Examples & Walkthrough

Here's a short excerpt from the code to initialize the serial communication and set up the input string for GPS data:

void setup() {
    Serial.begin(9600);
    inputString.reserve(200);
}

This code initializes the serial communication at a baud rate of 9600 and reserves space for the incoming GPS data string.

Next, let's look at the part of the code that processes the incoming GPS data and extracts latitude and longitude:

if (BB == signal) {
    String LAT = inputString.substring(7, 17);
    String LON = inputString.substring(20, 31);
    Serial.println(LAT);
    Serial.println(LON);
}

In this snippet, the program checks if the incoming data matches the expected GPS signal. If it does, it extracts the latitude and longitude values from the input string and prints them to the serial monitor.

Finally, the code includes a section to handle new data arriving via the serial connection:

void serialEvent() {
    while (Serial.available()) {
        char inChar = (char) Serial.read();
        inputString += inChar;
        if (inChar == '\\n') {
            stringComplete = true;
        }
    }
}

This function reads incoming characters from the serial port and appends them to the inputString. Once a newline character is detected, it sets the stringComplete flag to true, indicating that a complete string of data has been received.

Demonstration / What to Expect

Upon uploading the code and running the Arduino, you should see the latitude and longitude values printed in the serial monitor. It's important to ensure that the GPS module has a clear view of the sky to receive satellite signals effectively. If the module does not receive a signal, it may not display valid coordinates (in video at 02:30).

Common pitfalls include wiring errors, insufficient power supply, and poor satellite reception. Make sure to troubleshoot these aspects if you encounter any issues.

Video Timestamps

  • 00:00 - Introduction
  • 02:30 - Wiring instructions
  • 05:00 - Code explanation
  • 08:15 - Demonstration of GPS functionality

图像

GPS antenna
GPS antenna
GPS module with antenna
GPS module with antenna
GPS modules
GPS modules
Arduino wiring for Neo-6M GPS module
Arduino wiring for Neo-6M GPS module
3-Extract latitude and longitude from a GPS signal in an Arduino NEO-6M
语言: C++
/*
 * 这是用于Ublox NEO-6M GPS模块的Arduino代码。
 * 此代码提取GPS纬度和经度,以便可以用于其他目的。
 * 
 * 作者:Ahmad Nejrabi,Robojax视频
 * 日期:2017年1月24日,加拿大安大略省Ajax
 * 允许分享此代码,前提是此备注与代码一起保留。
 * 免责声明:此代码“按原样”提供,仅用于教育目的。
 * 
 * / 
 * 
 * // 为RoboJax.com编写
 */
String inputString = ""; // 一个用于保存 incoming 数据的字符串
boolean stringComplete = false; // 字符串是否完整
String signal = "\$GPGLL";
void setup() {
 // 初始化串行:
    Serial.begin(9600);
 // 为inputString保留200字节。
    inputString.reserve(200);
}

void loop() {
 // 在到达换行符时打印字符串:
    if (stringComplete) {
        String BB = inputString.substring(0, 6);
        if (BB == signal) {
            String LAT = inputString.substring(7, 17);
            int LATperiod = LAT.indexOf('.');
            int LATzero = LAT.indexOf('0');
            if (LATzero == 0) {
                LAT = LAT.substring(1);
            }

            String LON = inputString.substring(20, 31);
            int LONperiod = LON.indexOf('.');
            int LONTzero = LON.indexOf('0');
            if (LONTzero == 0) {
                LON = LON.substring(1);
            }

            Serial.println(LAT);
            Serial.println(LON);

        }

 // Serial.println(inputString);
 // 清除字符串:
        inputString = "";
        stringComplete = false;
    }
}

/*
 * 当新数据通过硬件串行 RX 到达时,会发生 SerialEvent。此例程在每次 loop() 运行之间执行,因此在 loop 内部使用 delay 可能会延迟响应。可能会有多个字节的数据可用。
 */
void serialEvent() {
    while (Serial.available()) {
 // 获取新字节:
        char inChar = (char) Serial.read();
 // 将其添加到 inputString:
        inputString += inChar;
 // 如果输入的字符是换行符,则设置一个标志。
 // 因此主循环可以对此做点什么:
        if (inChar == '\\n') {
            stringComplete = true;
        }
    }
}

资源与参考

尚无可用资源。

文件📁

没有可用的文件。