Here is the sketch. It compliles, loads and works fine on the Genuine Mega 2560. The LCD display does not give an accurate heading because it's based on the NMEA sentence extraction from the GPS. However, the GPS module contains an HMC5883L chip that I wish to get accurate heading data from. But I can't find the correct code to access the chip. The module has 2 separate wires for the HMC chip. The wires are currently connected to serial2 ports on the Arduino.
// Connect Deegoo-FPV NEO-N8M gps TX Yellow to serial3 RX pin 15 and gps RX White to TX pin 14
// Connect Deegoo-FPV NEO-M8N compass Brown to serial2 RX pin 17 and compass grey to TX pin 16
// Deegoo-FPV has NEO-N8M & intergated HCM5883L Compass
//Connect I2C LCD 20 x 4 Yellow to SCL Pin 21 Green to SDA Pin 20
// Set Serial3 to 38400
// Set Serial Begin to 9600
// Set Monitor to 9600
#include <TinyGPS++.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
TinyGPSPlus gps; // create gps object
// LCD geometry
const int LCD_COLS = 20;
const int LCD_ROWS = 4;
void setup()
{
lcd.begin(LCD_COLS, LCD_ROWS);
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(6, 0);
lcd.print("Satellite");
lcd.setCursor(5, 1);
lcd.print("Acquisition");
lcd.setCursor(5, 2);
lcd.print("In Progress");
lcd.setCursor(3, 3);
lcd.print("Please Wait.....");
delay(10000);
lcd.clear();
Serial.begin(9600); // connect serial for debugging
Serial.println("Pending GPS Receive Signal:");
Serial3.begin(38400); // connect gps sensor
}
void loop(){
while(Serial3.available()){ // check for gps data
if(gps.encode(Serial3.read()))// encode gps data
{
if (gps.satellites.value() > 0) {
//Latitude
lcd.setCursor(0, 0);
lcd.print("Latitude: ");
lcd.print(gps.location.lat(),6);
//Longitude
lcd.setCursor(0, 1);
lcd.print("Longitude:");
lcd.print(gps.location.lng(),6);
//Heading
lcd.setCursor(0, 2);
lcd.print("Heading: ");
lcd.print(gps.course.deg(),0);
//Speed
lcd.setCursor(0, 3);
lcd.print("Speed: ");
lcd.print(gps.speed.mph(),1);
// Number of satellites connected
lcd.setCursor(11, 3);
lcd.print(" Sats: ");
lcd.print(gps.satellites.value());
delay(2000);
}
}
}
}
Well that explains a lot. Now I see that I cannot use the HMC5883L on that device as I am already using the SCL/SDA ports on the Mega. Do you have a better suggestion as to what device I can add for compass data?
No problem, the I2C port is a bus that supports more than one device. Each connected device must have a distinct I2C address.
However, logic level shifters are required when connecting 3.3V sensors like the HMC5883L to 5V Arduinos like the Mega 2560.
Any modern magnetometer will work as a compass. Sparkfun, Adafruit and Pololu (among others) sell hobby modules with the logic level shifters built in.
So there is the code I'm using. The GPS works fine. The magnetometer works fine. But when I combine them the LCD goes blank. It compiles with no errors. At the moment the magnetometer outputs to the serial monitor instead of the LCD. It looks to me like Serial3 never becomes available so the sketch exits the loop and only the mag gives the correct output to the monitor. If I remove all the mag code, the output to the lcd looks perfect.
The posted code does not compile without errors. There are too many brackets "{".
It looks to me like Serial3 never becomes available
That is easy enough to check and fix. GPS units output NMEA sentences regardless of whether they have a satellite fix. Put in Serial.print statements to make sure.
With TinyGPS++, the following is not the correct way to determine whether you have a valid location. Study the example code and documentation more carefully.