Arduino读取GPS模块

Arduino读取GPS模块

最近要做一个公交车报站器,所以打算用GPS模块来实现定位的功能,于是找朋友借了个GPS模块来试试。

GPS模块

常见的GPS模块的参数都差不多,除了有些个别输出格式不同。
● 接口:RS232 TTL
● 电源:3V至5V
● 默认波特率:9600 bps
● 支持标准的NMEA

接线

GPS模块 Arduino uno
RXD 3
TXD 4
vcc 5V
GND GND

NMEA协议

这篇文章讲的很详细 GPS NMEA-0183标准详解(常用的精度以及经纬度坐标),如果只是需要定位和时间,那我们只用读GPGGA语句和GPGLL语句就可以了,不多赘述。

程序

#include<SoftwareSerial.h>

SoftwareSerial gps(4,3);
byte gpsdata = 0;
void setup()
  {
    Serial.begin(9600); //set the baud rate of serial port to 9600;
    gps.begin(9600);	//set the GPS baud rate to 9600;
  }

void loop()
  {
    if (ss.available()>0)
      gpsdata = gps.read();	//read gps data
      Serial.write(gpsdata);  //print gpsdata
  }

打开串口,查看数据,GPS模要把天线放到室外,不然看到经纬度信息。
GPS串口输出
显然,有些数据我用不上,我只需要用到经纬度信息,这里可以用TinyGPS++库来解析GPS模块数据,编写程序

/*
* richowe 
*/
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

TinyGPSPlus gps;
SoftwareSerial ss(4, 3);

float latitude;
float longitude;

void setup()
  {
    Serial.begin(9600); //set the baud rate of serial port to 9600;
    ss.begin(9600); //set the GPS baud rate to 9600;
  }

void loop()
  {
    while (ss.available() > 0)
      {
        gps.encode(ss.read()); //The encode() method encodes the string in the encoding format specified by encoding.
        
        if (gps.location.isUpdated())
          {
            latitude = gps.location.lat(); //gps.location.lat() can export latitude
            longitude = gps.location.lng();//gps.location.lng() can export latitude
            Serial.print("Latitude=");
            Serial.print(latitude, 6);  //Stable after the fifth position
            Serial.print(" Longitude=");
            Serial.println(longitude, 6);
            delay(500);
          }
      }
  }

打开串口,输出经纬度信息。
输出经纬度信息

总结

GPS模块在室内不能实现定位,但是可以获取的信息很多,如时间,速度。接着在屏幕显示。

发布了31 篇原创文章 · 获赞 22 · 访问量 9501

猜你喜欢

转载自blog.csdn.net/richowe/article/details/103430090