MPU9250 with GPS Shield Logger

Bare with me, I’m new to this so any guidance is greatly appreciated.

I have a Redboard, GPS Shield Logger and MPU 9250. I can get the GPS Shield Logger working and the 9250 working on their own - each code base runs fine. When I combine the two codebases the 9250 checks out ok but the GPS unit isn’t getting a sat signal even though the led indicates such. It could be the code, but I’m less confident in the having the 9250 wired correctly into the GPS. I have the 9250 connected to the GPS Shield at 3V3, GND, A4 and A5.

Any thoughts?

It is likely an issue in the code. Our Technical Support staff can only provide limited support for custom code. However, if you’d like to post your code we can try to give suggestions as well as feedback from the forum community.

Brandon,

Here’s the code:

The GPS lights indicate a satellite connection and the 9250 indicates its connected and reading sensors but I end up with the looping message seen below.

Setting up SD card.

gpslog0.csv exists

File name: gpslog1.csSetting up SD card.

gpslog0.csv exists

gpslog1.csv exists

File name: gpslog2.csv

MPU9250 I AM 0x71 I should be 0x71

MPU9250 is online…

x-axis self test: acceleration trim within : 1.5% of factory value

y-axis self test: acceleration trim within : -0.9% of factory value

z-axis self test: acceleration trim within : 1.9% of factory value

x-axis self test: gyration trim within : -4.9% of factory value

y-axis self test: gyration trim within : -1.1% of factory value

z-axis self test: gyration trim within : -0.1% of factory value

MPU9250 initialized for active data mode…

AK8963 I AM 0x48 I should be 0x48

No GPS data. Sats: 1495990272

No GPS data. Sats: 1495990272

No GPS data. Sats: 1495990272

No GPS data. Sats: 1495990272

#include <SPI.h>
#include <SD.h>
#include <TinyGPS++.h>
#include <quaternionFilters.h>
#include <MPU9250.h>

#define ARDUINO_USD_CS 10 // uSD card CS pin (pin 10 on SparkFun GPS Logger Shield)

/////////////////////////
// Log File Defintions //
/////////////////////////
// Keep in mind, the SD library has max file name lengths of 8.3 - 8 char prefix,
// and a 3 char suffix.
// Our log files are called "gpslogXX.csv, so "gpslog99.csv" is our max file.
#define LOG_FILE_PREFIX "gpslog" // Name of the log file.
#define MAX_LOG_FILES 100 // Number of log files that can be made
#define LOG_FILE_SUFFIX "csv" // Suffix of the log file
char logFileName[13]; // Char string to store the log file name
// Data to be logged:
#define LOG_COLUMN_COUNT 8
char * log_col_names[LOG_COLUMN_COUNT] = {
  "longitude", "latitude", "altitude", "speed", "course", "date", "time", "satellites"
}; // log_col_names is printed at the top of the file.

//////////////////////
// Log Rate Control //
//////////////////////
#define LOG_RATE 5000 // Log every 5 seconds
unsigned long lastLog = 0; // Global var to keep of last time we logged

/////////////////////////
// TinyGPS Definitions //
/////////////////////////
TinyGPSPlus tinyGPS; // tinyGPSPlus object to be used throughout
#define GPS_BAUD 9600 // GPS module's default baud rate

/////////////////////////////////
// GPS Serial Port Definitions //
/////////////////////////////////
// If you're using an Arduino Uno, RedBoard, or any board that uses the
// 0/1 UART for programming/Serial monitor-ing, use SoftwareSerial:
#include <SoftwareSerial.h>
#define ARDUINO_GPS_RX 9 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 8 // GPS RX, Arduino TX pin
SoftwareSerial ssGPS(ARDUINO_GPS_TX, ARDUINO_GPS_RX); // Create a SoftwareSerial

// Set gpsPort to either ssGPS if using SoftwareSerial or Serial1 if using an
// Arduino with a dedicated hardware serial port
#define gpsPort ssGPS  // Alternatively, use Serial1 on the Leonardo

// Define the serial monitor port. On the Uno, and Leonardo this is 'Serial'
//  on other boards this may be 'SerialUSB'
#define SerialMonitor Serial

#define I2Cclock 400000
#define I2Cport Wire
#define MPU9250_ADDRESS MPU9250_ADDRESS_AD0
MPU9250 myIMU(MPU9250_ADDRESS, I2Cport, I2Cclock);

void setup()
{
  Wire.begin();
  SerialMonitor.begin(9600);
  gpsPort.begin(GPS_BAUD);

  SerialMonitor.println("Setting up SD card.");
  // see if the card is present and can be initialized:
  if (!SD.begin(ARDUINO_USD_CS))
  {
    SerialMonitor.println("Error initializing SD card.");
  }
  updateFileName(); // Each time we start, create a new file, increment the number
  printHeader(); // Print a header at the top of the new file

    // Read the WHO_AM_I register, this is a good test of communication
  byte c = myIMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
  SerialMonitor.print(F("MPU9250 I AM 0x"));
  SerialMonitor.print(c, HEX);
  SerialMonitor.print(F(" I should be 0x"));
  SerialMonitor.println(0x71, HEX);

  if (c == 0x71) // WHO_AM_I should always be 0x71
  {
    SerialMonitor.println(F("MPU9250 is online..."));

    // Start by performing self test and reporting values
    myIMU.MPU9250SelfTest(myIMU.selfTest);
    SerialMonitor.print(F("x-axis self test: acceleration trim within : "));
    SerialMonitor.print(myIMU.selfTest[0],1); Serial.println("% of factory value");
    SerialMonitor.print(F("y-axis self test: acceleration trim within : "));
    SerialMonitor.print(myIMU.selfTest[1],1); Serial.println("% of factory value");
    SerialMonitor.print(F("z-axis self test: acceleration trim within : "));
    SerialMonitor.print(myIMU.selfTest[2],1); Serial.println("% of factory value");
    SerialMonitor.print(F("x-axis self test: gyration trim within : "));
    SerialMonitor.print(myIMU.selfTest[3],1); Serial.println("% of factory value");
    SerialMonitor.print(F("y-axis self test: gyration trim within : "));
    SerialMonitor.print(myIMU.selfTest[4],1); Serial.println("% of factory value");
    SerialMonitor.print(F("z-axis self test: gyration trim within : "));
    SerialMonitor.print(myIMU.selfTest[5],1); Serial.println("% of factory value");

    // Calibrate gyro and accelerometers, load biases in bias registers
    myIMU.calibrateMPU9250(myIMU.gyroBias, myIMU.accelBias);

    myIMU.initMPU9250();
    // Initialize device for active mode read of acclerometer, gyroscope, and
    // temperature
    SerialMonitor.println("MPU9250 initialized for active data mode....");

    // Read the WHO_AM_I register of the magnetometer, this is a good test of
    // communication
    byte d = myIMU.readByte(AK8963_ADDRESS, WHO_AM_I_AK8963);
    SerialMonitor.print("AK8963 ");
    SerialMonitor.print("I AM 0x");
    SerialMonitor.print(d, HEX);
    SerialMonitor.print(" I should be 0x");
    SerialMonitor.println(0x48, HEX);

    if (d != 0x48)
    {
      // Communication failed, stop here
      SerialMonitor.println(F("Communication failed, abort!"));
      SerialMonitor.flush();
      abort();
    }
  } // if (c == 0x71)
  else
  {
    SerialMonitor.print("Could not connect to MPU9250: 0x");
    SerialMonitor.println(c, HEX);

    // Communication failed, stop here
    SerialMonitor.println(F("Communication failed, abort!"));
    SerialMonitor.flush();
    abort();
  }
}

void loop()
{
  if ((lastLog + LOG_RATE) <= millis())
  { // If it's been LOG_RATE milliseconds since the last log:
    if (tinyGPS.location.isUpdated()) // If the GPS data is vaild
    {
      if (logGPSData()) // Log the GPS data
      {
        SerialMonitor.println("GPS logged."); // Print a debug message
        lastLog = millis(); // Update the lastLog variable
      }
      else // If we failed to log GPS
      { // Print an error, don't update lastLog
        SerialMonitor.println("Failed to log new GPS data.");
      }
    }
    else // If GPS data isn't valid
    {
      // Print a debug message. Maybe we don't have enough satellites yet.
      SerialMonitor.print("No GPS data. Sats: ");
      SerialMonitor.println(tinyGPS.satellites.value());
    }
  }

  // If we're not logging, continue to "feed" the tinyGPS object:
  while (gpsPort.available())
    tinyGPS.encode(gpsPort.read());
}

byte logGPSData()
{
  File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file

  if (logFile)
  { // Print longitude, latitude, altitude (in feet), speed (in mph), course
    // in (degrees), date, time, and number of satellites.
    logFile.print(tinyGPS.location.lng(), 6);
    logFile.print(',');
    logFile.print(tinyGPS.location.lat(), 6);
    logFile.print(',');
    logFile.print(tinyGPS.altitude.feet(), 1);
    logFile.print(',');
    logFile.print(tinyGPS.speed.mph(), 1);
    logFile.print(',');
    logFile.print(tinyGPS.course.deg(), 1);
    logFile.print(',');
    logFile.print(tinyGPS.date.value());
    logFile.print(',');
    logFile.print(tinyGPS.time.value());
    logFile.print(',');
    logFile.print(tinyGPS.satellites.value());
    logFile.println();
    logFile.close();

    return 1; // Return success
  }

  return 0; // If we failed to open the file, return fail
}

// printHeader() - prints our eight column names to the top of our log file
void printHeader()
{
  File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file

  if (logFile) // If the log file opened, print our column names to the file
  {
    int i = 0;
    for (; i < LOG_COLUMN_COUNT; i++)
    {
      logFile.print(log_col_names[i]);
      if (i < LOG_COLUMN_COUNT - 1) // If it's anything but the last column
        logFile.print(','); // print a comma
      else // If it's the last column
        logFile.println(); // print a new line
    }
    logFile.close(); // close the file
  }
}

// updateFileName() - Looks through the log files already present on a card,
// and creates a new file with an incremented file index.
void updateFileName()
{
  int i = 0;
  for (; i < MAX_LOG_FILES; i++)
  {
    memset(logFileName, 0, strlen(logFileName)); // Clear logFileName string
    // Set logFileName to "gpslogXX.csv":
    sprintf(logFileName, "%s%d.%s", LOG_FILE_PREFIX, i, LOG_FILE_SUFFIX);
    if (!SD.exists(logFileName)) // If a file doesn't exist
    {
      break; // Break out of this loop. We found our index
    }
    else // Otherwise:
    {
      SerialMonitor.print(logFileName);
      SerialMonitor.println(" exists"); // Print a debug statement
    }
  }
  SerialMonitor.print("File name: ");
  SerialMonitor.println(logFileName); // Debug print the file name
}