Neo M8N GPS Module and Gy-271 Compass Module

I am trying to make a gps navigated robot.

When I connect GPS module and compass module individually, they work fine... GPS through arduino mega serial1 and compass through i2c...
But when I connect them together, only the compass module gives outputs...

Any idea?

Show us your code in between [code] ...tags...[/code],

  ... so it looks like this.

If your code is bigger than 9000 bytes, you can't
embed it here.

Instead, attach it to your post. Use the "Attachments and other options" in the full post editor. That option is not available in Quick Reply editor at the bottom of this page. Type a little in the post box, then press "Preview" and you should see it.

Cheers,
/dev

#include "Ublox.h"
#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>
#define SERIAL_BAUD 115200
#define GPS_BAUD 9600
#define N_FLOATS 4
#include "Math.h";


// Create a compass
HMC5883L_Simple Compass;
    

Ublox M8_Gps;
// Altitude - Latitude - Longitude - N Satellites
float gpsArray[N_FLOATS] = {0, 0, 0, 0};

void setup() {
   Serial.begin(SERIAL_BAUD);
   Serial1.begin(GPS_BAUD);
   Wire.begin();

  //Colombo - Magnetic declination: -2° 10'
  Compass.SetDeclination(-2, 10, 'W');  
  Compass.SetSamplingMode(COMPASS_SINGLE);
  Compass.SetScale(COMPASS_SCALE_130);
  Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
  //digitalWrite(SDA, LOW);
  //digitalWrite(SDL, LOW);
}
String g[5]={"alt","lat","lon","sat"};
void loop() {
   if(!Serial1.available())
    return;

  while(Serial1.available()){
        char c = Serial1.read();
         if (M8_Gps.encode(c)) {
          gpsArray[0] = M8_Gps.altitude;
          gpsArray[1] = M8_Gps.latitude;
          gpsArray[2] = M8_Gps.longitude; 
          gpsArray[3] = M8_Gps.sats_in_use;
        }
  }
  //  float lat1=gpsArray[1];
  //  float lng1=gpsArray[2];
  //  float delta_lat=lat0-lat1;
  //  float delta_lng=lng0-lng1;
  //  float a= ((sin(delta_lat/2))**2)+ cos(lat0)*cos(lat1)*((sin(delta_lng/2))**2);
  //  
    float heading = Compass.GetHeadingDegrees();  //**********
   
    Serial.print("Heading from compass: \t");
    Serial.println( heading );   
  //
  for(byte i = 0; i < N_FLOATS; i++) {
    Serial.print(g[i]);Serial.print(" "); Serial.print(gpsArray[i], 6);Serial.print(" ");
  }
  Serial.println("");
}

checked with both arduino mega and uno.... works both individually...
when the line commented like this //********, is commented, the gps works fine...

You are printing too much. You should probably get the compass heading only when you receive a GPS update. Then you will print the heading just once per second. Right now, your sketch prints every time through loop. This prevents it from getting all the GPS characters.

Here's a NeoGPS version of your sketch:

#include <NMEAGPS.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>

#define SERIAL_BAUD 115200
#define GPS_BAUD 9600
#define N_FLOATS 4


HMC5883L_Simple Compass;
    
NMEAGPS M8_Gps;
NeoGPS::Location_t previousLocation;
bool havePrevious = false;

// Altitude - Latitude - Longitude - N Satellites
float gpsArray[N_FLOATS] = {0, 0, 0, 0};

void setup() {
   Serial.begin(SERIAL_BAUD);
   Serial1.begin(GPS_BAUD);
   Wire.begin();

  //Colombo - Magnetic declination: -2° 10'
  Compass.SetDeclination(-2, 10, 'W');  
  Compass.SetSamplingMode(COMPASS_SINGLE);
  Compass.SetScale(COMPASS_SCALE_130);
  Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
  //digitalWrite(SDA, LOW);
  //digitalWrite(SDL, LOW);
}

const char *g[5] = { "alt", "lat", "lon", "sat" };

void loop() {

  if (M8_Gps.available( Serial1 )) {
    gps_fix fix = M8_Gps.read();

    gpsArray[0] = fix.altitude();
    gpsArray[1] = fix.latitude();
    gpsArray[2] = fix.longitude(); 
    gpsArray[3] = fix.satellites;

    float heading = Compass.GetHeadingDegrees();  //**********
   
    Serial.print( F("Heading from compass: \t") ); // F macro saves RAM
    Serial.println( heading );   

    Serial.print( F("Heading from GPS: \t") );
    Serial.println( fix.heading() );

    if (havePrevious) {
      Serial.print( F("Heading from previous location: \t") );
      Serial.println( previousLocation.BearingToDegrees( fix.location ) );
    } else
      havePrevious = true;
    previousLocation = fix.location;

    //
    for(byte i = 0; i < N_FLOATS; i++) {
      Serial.print( g[i] ); Serial.print(' ');
      Serial.print( gpsArray[i], 6 ); Serial.print(' ');
    }
    Serial.println();
  }
}

Notes:

* You don't need to use the [u]S[/u]tring class for the labels. You can just use char arrays, also called C strings.

* loop simply asks the GPS object if it has a new update (i.e., a fix structure). When a new fix is available, it reads it and uses the values.

* NeoGPS is smaller, faster and more accurate than all other libraries. You can configure it to parse only the pieces you use (lat, lon, and alt in your case). All other pieces will be quickly skipped, saving even more program space and RAM

Your original sketch uses 14266 bytes of program space and 972 bytes of RAM.
The NeoGPS version uses 11204 bytes of program space and 654 bytes of RAM, a significant savings.

If you want to try it, it is also available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.

Cheers,
/dev


Edited to add printing of GPS heading and NeoGPS calculation of heading from the previous location. The NeoGPS calculation is much more accurate than other libraries.