mikalhart / TinyGPS

A compact Arduino NMEA (GPS) parsing library
http://arduiniana.org
387 stars 257 forks source link

Using Hardware serial instead of Software serial #19

Closed SiddiqMohammed closed 5 years ago

SiddiqMohammed commented 5 years ago

Hi, I was working on the arduino mega and was wondering how to use the hardware serial instead of the default software serial. Below is the code that I'd like to change..

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

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;

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

void setup(){
  Serial.begin(9600);
  ss.begin(GPSBaud);
}

void loop(){
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0){
    gps.encode(ss.read());
    if (gps.location.isUpdated()){
      // Latitude in degrees (double)
      Serial.print("Latitude= "); 
      Serial.print(gps.location.lat(), 6);      
      // Longitude in degrees (double)
      Serial.print(" Longitude= "); 
      Serial.println(gps.location.lng(), 6); 
}

Thanks in advance

SiddiqMohammed commented 5 years ago

Since no one has answered it and I figured it out myself, here is the solution for those who may stumble upon this in the future.

All one has to do is remove the "serial" parts from the code. From what I understand, the arduino by default sends data over its hardware serial ports and hence doesn't need it to be specified.

`#include <TinyGPS++.h> TinyGPSPlus gps;

void loop(){ // This sketch displays information every time a new sentence is correctly encoded. while (serial.available() > 0){ gps.encode(serial.read()); if (gps.location.isUpdated()){ // Latitude in degrees (double) Serial.print("Latitude= "); Serial.print(gps.location.lat(), 6);
// Longitude in degrees (double) Serial.print(" Longitude= "); Serial.println(gps.location.lng(), 6); }`

ChuckWilcox commented 4 years ago

Thanks to SiddiqMohammond note, it enabled me to adapt for Arduino Mega, as all other code seems focused on using software serial. The amended code works for Arduino Mega & TinyGPS++ This code avoids software serial, it uses Serial1 , pins 18 & 19, one of the several UARTs available on the Mega. Hope it helps someone.

//Connect GPS Tx to Mega Rx Pin19 & GPS Rx to Mega Tx Pin 18

include <TinyGPS++.h>

TinyGPSPlus gps; void setup() { // put your setup code here, to run once: Serial.begin(57600); // connect serial Serial.println("GPS Signal received:"); Serial1.begin(9600); // connect gps sensor }

void loop(){ // This sketch displays information every time a new sentence is correctly encoded. while(Serial1.available() > 0){ gps.encode(Serial1.read()); if (gps.location.isUpdated()){ // Latitude in degrees (double) Serial.print("Latitude= "); Serial.print(gps.location.lat(), 6); // Longitude in degrees (double) Serial.print(" Longitude= "); Serial.println(gps.location.lng(), 6); } } }

neuraledgeai commented 3 years ago

It is very easy trigger hardware serial port communication in Arduino. I have added a link below below in which it provides an example to communicate with Ublox neo 6m gps module via hardware port in Arduino Uno.

https://spiderli.wordpress.com/2021/05/31/how-to-use-hardware-serial-instead-of-software-serial-communication-in-arduino/ Here is the link... You can use this same code and method in Arduino mega also..

ARTMEDIUM commented 3 years ago

Example from AnoopkumarU is not work. So thanks SiddiqMohammed for real help. He advise is very good.

neuraledgeai commented 3 years ago

This is a perfect example of using hardware serial communication. Here the uno is communicating to a GPS module via hardware serial and it works fine.May I know know what went wrong?

ARTMEDIUM commented 3 years ago

When I try to get any information from a GPS module, including date or time, I get 0.

MotoMotoRo commented 2 years ago

Hello.... Well done guys ... Great on the Mega 2560.