Getting Data from the MKR GPS and an external Sensor via I2C

Hello,

I'm using a MKR WiFi 1010 with a MKR GPS Shield as well as the Würth ISDS (IMU). As the Code I use the example Codes from the library. I connected both sensors to the board via I2C.
For the MKR GPS I modified the connector cable, so I could use the Pins of the MKR WiFi. I connected both sensor via breadboard with the MKR WiFi.
When started the example codes of both sensors individually, the corressponding sensor gave me data. At this point both sensors were connected to the MKR WiFi but only one was called.
Next I put together the codes of both sensors by simply copy-pasting the indiviual codes. However now only the IMU gives me data.
I put some marks in the code, so I could check wich loops were run through and which one were left out.

The MKR GPS seems to connect to the board, but never enter the if (GPS.available()) loop, but I can't figure out why. I also used an I2C scanner to check if the board recognizes both sensors as I2C devices, wich it did. So I figured that the problem is within the code, not the hardware.

#include <WSEN_ISDS.h> //IMU
#include <Arduino_MKRGPS.h> //GPS
#include <Wire.h>

Sensor_ISDS sensor;
 int status;

void setup()
{
 
   Serial.begin(9600);

 // Initialize the I2C interface
  sensor.init(ISDS_ADDRESS_I2C_0);

  // Reset sensor
  status = sensor.SW_RESET();
  if (WE_FAIL == status)
  {
    Serial.println("Error:  SW_RESET(). Stop!");
    while(1);  
  
  }

  // Set FIFO ODR to 26Hz
  status = sensor.select_ODR(2);
  if (WE_FAIL == status)
    {
    Serial.println("Error:  select_ODR(). Stop!");
    while(1);  
  }

  // Set high performance mode
  status = sensor.set_Mode(2);
  if (WE_FAIL == status)
  {
    Serial.println("Error:  set_Mode(). Stop!");
    while(1);  
  } 

//GPS-Code from example
  
  while (!Serial) {
    Serial.println("wait for GPS serial port to connect"); // wait for serial port to connect. Needed for native USB port only
  } 

  // If you are using the MKR GPS as shield, change the next line to pass
  // the GPS_MODE_SHIELD parameter to the GPS.begin(...)
  if (!GPS.begin()) {
    Serial.println("Failed to initialize GPS!");
    while (1);
  }

}
 
 int i=1; 
 
void loop()
{
  while (i<5){  //header only at beginning
      Serial.print("Latitude");
    Serial.print("  "); //tab als Leerzeichen zwischen den einzelnen Spalten
    Serial.print("Longitude");
    Serial.print("  "); 
    Serial.print("Altitude [m]");
    Serial.print("  "); 
    Serial.print("Ground speed [km/h]");
    Serial.print("  "); 
    Serial.print("Number of satellites");
     Serial.print("a_X [mg]");
    Serial.print("a_Y [mg]");
    Serial.print("a_Z [mg]");
    Serial.print("Gyro_X [mdps]");
    Serial.print("Gyro_Y [mdps]");
    Serial.println("Gyro_Z [mdps]");
  i++;

  }
 
  Serial.println("Header fertig");
  
  //GPS-code from example
 // check if there is new GPS data available
     
     Serial.println("GPS Anfang/Abfrage available");
      if (GPS.available()) {
        // read GPS values
         Serial.println("GPS Variablen zuweisen");
        float latitude   = GPS.latitude();
        float longitude  = GPS.longitude();
        float altitude   = GPS.altitude();
        float speed      = GPS.speed();
        int   satellites = GPS.satellites();
    
        // print GPS values
        Serial.print("GPS");
        Serial.print(latitude, 6);
        Serial.print("  "); 
        Serial.print(longitude, 6);
        Serial.print("  "); 
        Serial.print(altitude);
        Serial.print("  "); 
        Serial.print(speed);
        Serial.print("  "); 
        Serial.print(satellites);
        Serial.println("GPS Daten ausgegeben");
    
    }
    Serial.println("GPS fertig");
 
  //accelaration from example
  status = sensor.is_ACC_Ready_To_Read();
  if (WE_FAIL == status)
  {
    Serial.println("Error: is_ACC_Ready_To_Read(). Stop!");
    while(1);
  } 

    else if (1 == status)
  {
    int16_t acc_X; //length=16 bit
    int16_t acc_Y;
    int16_t acc_Z;
    Serial.println("Beschleunigung initialisiert");
    
#if 0
    // Get acceleration along X axis in mg
   status = sensor.get_acceleration_X(&acc_X);
   if (WE_FAIL == status)
  {
    Serial.println("Error:  get_acceleration_X(). Stop!");
    while(1);  
  }
    Serial.println("Acceleration along X axis in [mg]: ");
    Serial.println(acc_X);

    // Get acceleration along Y axis in mg
    status = sensor.get_acceleration_Y(&acc_Y);
    if (WE_FAIL == status)
  {
    Serial.println("Error:  get_accel_Y(). Stop!");
    while(1);  
  }
    Serial.println("Acceleration along Y axis in [mg]: ");
    Serial.println(acc_Y);

    // Get acceleration along Z axis in mg
    status = sensor.get_acceleration_Z(&acc_Z);
    if (WE_FAIL == status)
  {
    Serial.println("Error:  get_acceleration_Z(). Stop!");
    while(1);  
  }
    Serial.println("Acceleration along Z axis in [mg]: ");
    Serial.println(acc_Z);
#else
    
    status = sensor.get_accelerations(&acc_X,&acc_Y,&acc_Z);
    if (WE_FAIL == status)
  {
    Serial.println("Error:  get_accelerations(). Stop!");
    while(1);  
  }
    Serial.print("a_X [mg]");
    Serial.print("a_Y [mg]");
    Serial.println("a_Z [mg]");
    Serial.print(acc_X);
    Serial.print(" ");
    Serial.print(acc_Y);
    Serial.print(" ");
    Serial.print(acc_Z);
    Serial.println(" ");
#endif
  }
  else /*status == '0' -> data not ready */
  {
    Serial.println("No data in output register (Accelaration).");
  }

Serial.println("Beschleunigung fertig");

  //Gyro from example
  status = sensor.is_Gyro_Ready_To_Read();
 if (WE_FAIL == status)
  {
    Serial.println("Error: is_Gyro_Ready_To_Read(). Stop!");
    while(1);
  } 
  else if (1 == status)
  {
    int32_t gyro_X;
    int32_t gyro_Y;
    int32_t gyro_Z;
 Serial.println("Gyro initialisiert");
 
#if 0
    // Get X-axis angular rate in [mdps]
   
    status = sensor.get_angular_rate_X(&gyro_X);
    if (WE_FAIL == status)
    {
    Serial.println("Error:  get_angular_rate_X(). Stop!");
    while(1);  
  }
    Serial.println("Angular rate in X axis in [mdps]: ");
    Serial.println(gyro_X);

    // Get Y-axis angular rate in [mdps]
    status = sensor.get_angular_rate_Y(&gyro_Y);
    if (WE_FAIL == status)
    {
    Serial.println("Error:  get_angular_rate_Y(). Stop!");
    while(1);  
  }
    Serial.println("Angular rate in  Y axis in [mdps]: ");
    Serial.println(gyro_Y);

    // Get Z-axis angular rate in [mdps]
    status = sensor.get_angular_rate_Z(&gyro_Z);
    if (WE_FAIL == status)
    {
    Serial.println("Error:  get_angular_rate_Z(). Stop!");
    while(1);  
  }
    Serial.println("Angular rate in  Z axis in [mdps]: ");
    Serial.println(gyro_Z);
#else
    status = sensor.get_angular_rates(&gyro_X,&gyro_Y,&gyro_Z);
    if (WE_FAIL == status)
    {
    Serial.println("Error:  get_angular_rates(). Stop!");
    while(1);  
  }
    //Serial.println("Angular rate in X,Y,Z axis in [mdps]: ");
    Serial.print(gyro_X);
    Serial.print(" ");
    Serial.print(gyro_Y);
    Serial.print(" ");
    Serial.print(gyro_Z);
    Serial.println(" "); 

#endif
  }
  else
  {
    Serial.println("No data in output register Gyroscope.");
  }

Serial.println("Gyro fertig"); 
    
  //delay(3000);
}

Libarary for the IMU/ISDS-Sensor:
https://github.com/WurthElektronik/SensorLibrariesArduino/tree/master

Does anybody know what to do to fix this issue? Thanks a lot :slight_smile:

ok, so the problem was within the MKRGPS-lib
with every loop-walkthrough the code only takes 1 bit of the GPS-data and then the next one in the next iteration and so on until it has all the data, then it starts from the beginning
this works as long, as the only sensor in the loop is the GPS but as soon as another sensor sends its data the connection to the GPS crashes
to fix it, you first have to access all the data of the GPS before you go on to the next sensor

 // check if there is new GPS data available
  do 
  {
    if (GPS.available()) {
      // read GPS values
      float latitude   = GPS.latitude();
      float longitude  = GPS.longitude();
      float altitude   = GPS.altitude();
      float speed      = GPS.speed();
      int   satellites = GPS.satellites();

      // print GPS values
      Serial.print(latitude, 7);
      Serial.print(" ");
      Serial.print(longitude, 7);
      Serial.print(" ");
      Serial.print(altitude);
      Serial.print(" ");
      Serial.print(speed);
      Serial.print(" ");
      Serial.print(satellites);
      Serial.print(" ");
    }

      }
  while(Wire.available()); 

this code solved the problem for me

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.