Arduino GPS and Ensuring Successful GPS Communication when Using Software Serial

An Arduino project I am working on uses the TinyGPS library and an EM-406A GPS receiver to keep track of the date and time. This data is used by the rest of my program. When I was coding up this project, I ran in numerous bugs and believe the cause was derived from serial communication with the GPS module. I am using software serial to talk to a number of serial devices switching to the GPS module when needed (see this post to learn about SoftwareSerial and multiple devices). In the first version of my program, I believe this switching was corrupting the bytes returning from the GPS module so valid GPS data wasn’t beeing received. To fix the problem I used a second while loop and a flag to force communication with the GPS to continue until a valid byte is received. Below is the relevent lines of code from my program.

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

#define RXPIN 2
#define TXPIN 3

TinyGPS gps;
SoftwareSerial GPSSerial(RXPIN,TXPIN);

void setup(void) {

void loop(void) {


// the SoftwareSerial channel for my GPS module is called GPSSerial
GPSSerial.begin(4800); // the EM-406 works at this baud rate

GPSFlag = 0; // a flag to control GPS communication

while(GPSflag == 0) { // continue to talk to GPS until a valid 
// byte is received

while(GPSSerial.available()) { // while the GPS module is sending 
// data

byte c =; // read in the bytes being sent by the GPS

// if incoming byte is valid GPS data, process it
if(gps.encode(c)){ // gps is the name of my TinyGPS object

gps.get_datetime(&date, &time); // get the data and time from 
// the GPS byte

GPSflag = 1; // set the flag to one to indiccate that the GPS data 
// was successfully received




GPSSerial.end(); // disable the GPS module's SoftwareSerial channel



Leave a Reply

Your email address will not be published.