Notes About Working with Various Arduino & Netduino Microcontroller Boards

Sunday, July 7, 2013

Displaying GPS Data Using an Adafruit Ultimate GPS, SainSmart IIC/I2C/TWI Serial 2004 20x4 LCD, & an Arduino Due


This post shows how to read data from an Adafruit Ultimate GPS breakout (ver. 3) via a serial connection and display date, time, and location data to the 20 x 4 character SainSmart IIC/I2C/TWI Serial 2004 LCD. These components are connected to an Arduino Due.

Back in June, I posted about using the Adafruit Ultimate GPS with an Arduino Due.  That example uses a more complex approach to parsing the NMEA data and converting values to numeric types. The code presented below sticks to displaying elements of the GPS data ($GPRMC) as strings for display without any conversion.

I made a brief post about using the Sainsmart I2C display last fall. The LiquidCrystal_I2C library for the 20 x 4 display is available from www.yourduino.com. For documentation, see this page.  I am using ver. 1 of the library, and it works fine with the Arduino Due.  There is also a version 2 of the library, but I haven't tested it yet.   I have found that the I2C address for the LCD is 0x3F, not 0x27 as shown in some of the online examples. You may need to try both addresses.

Remember that the Arduino Due is a 3.3V device that does not have 5V tolerant pins like the Netduino.  Both the Adafruit GPS and the Sainsmart I2C LCD work using 3.3V.


Connections


Ultimate GPS Arduino Due
VIN          3.3V
GND          GND
RX           TX1 (Digital 18)
TX           RX1 (Digital 19)

I2C LCD Arduino Due
GND     GND
VCC     3.3V
SDA     SDA (Digital 20)
SCL     SCL (Digital 21)


Code


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

String buffer = "";
String element[10];
LiquidCrystal_I2C lcd(0x3F,20,4);

void setup() {
  Serial1.begin(9600);
  lcd.init(); 
  lcd.backlight();
  lcd.setCursor(0, 0);
}

void loop() {
  if (Serial1.available() > 0) {
    char c = (char) Serial1.read();
    buffer += c;
    if(buffer.endsWith("\r\n")) { 
      if(buffer.startsWith("$GPRMC")) {
        int numArgs = 0;
        int beginIdx = 0;
        int idx = buffer.indexOf(",");
        int k = 0;
        while (idx != -1)
        {
          element[k++] = buffer.substring(beginIdx, idx);
          beginIdx = idx + 1;
          idx = buffer.indexOf(",", beginIdx);
        }  
        if(element[2] == "A") {  
          lcd.setCursor(0, 0);
          lcd.print(element[1] + " " + element[9]);
          lcd.setCursor(1, 1);
          lcd.print(element[3] + " " + element[4]);
          lcd.setCursor(0, 2);
          lcd.print(element[5] + " " + element[6]);
        }
        else{
         lcd.clear(); 
         lcd.setCursor(0, 0);
         lcd.print("No valid data...");
        }
      }
      buffer = "";
    }
  } 
}

No comments:

Post a Comment