ethernet SD gps SD conflict

I've got an arduino MEGA 2560 R3 with an Ethernet shield 2 with SD card, a BME280 sensor, a raindrop sensor and a DS3231 RTC. All works fine with an ethernet server running on 2560.

Sensors values are stored on the card without any problem.

I am trying to connect a GPS shield from DuinoPeak with SD card on it.

I have a problem on my sketch SD card initialization failed. Here is my sketch without the main loop section....

Any hint?


#include <SPI.h>
#include <DS3231.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>
#include <SD.h>
#include "SparkFunBME280.h"
#include <LiquidCrystal_I2C.h>
#include "TinyGPS.h"

#define GPS_TX_DIGITAL_OUT_PIN 5
#define GPS_RX_DIGITAL_OUT_PIN 6

#define LCDDELAY 800
#define SERIALBAUD 9600
#define DEBUG 1
//#define SDCARD 1

#define DELIM ";"
#define KPtoBR 0.01

byte mac[] = { 0x90, 0xA2, 0xDA, 0x11, 0x2E, 0x56 };

IPAddress dnServer(192, 168, 10, 1);
IPAddress gateway(192, 168, 10, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress ip(192, 168, 10, 150);

EthernetUDP Udp;

EthernetServer server(80);

DS3231 rtc(SDA, SCL);

File myFile;

BME280 mySensor;

char writebuffer[100], logfilename[13];

unsigned long tepTimer, rainTimer;

unsigned int localPort = 8888;

LiquidCrystal_I2C lcd(0x27, 16, 2);

TinyGPS gps;

float latitude = 0.0, longitude = 0.0, altitude = 0.0, course = 0.0, speed = 0.0;

long startMillis;
long secondsToFirstLocation = 0;

char sz[32];

const int sensorMin = 0;
const int sensorMax = 1024;

void setup() {
lcd.init(); lcd.backlight(); lcd.setCursor(0, 1); lcd.print("Hello, world!");

#ifdef DEBUG
Serial.begin(SERIALBAUD);
Serial.println("Initializing Serial...");
while (!Serial) { ; }
#endif

#ifdef DEBUG
Serial.println("Initializing RTC...");
#endif
rtc.begin();
delay(LCDDELAY);

// rtc.setDOW(THURSDAY); rtc.setTime(11, 8, 00); rtc.setDate(26, 4, 2018);

strcpy(writebuffer, rtc.getDateStr()); strcat(writebuffer, DELIM); strcat(writebuffer, rtc.getTimeStr()); strcat(writebuffer, DELIM);

#ifdef SDCARD
Serial.println("Initializing SD card...");
pinMode(53, OUTPUT);
while (!SD.begin()) {
Serial.println("initialization failed!");
}
strcpy(logfilename, "meteoint.txt");
myFile = SD.open(logfilename, FILE_WRITE);
delay(LCDDELAY);

writemyfile("Initializing SD card...\n");

writemyfile("IP Setup........\n");
#endif

#ifdef DEBUG
Serial.println("IP Setup ...");
#endif
Ethernet.begin(mac, ip, dnServer, gateway, subnet);
delay(LCDDELAY);

Udp.begin(localPort);

#ifdef SDCARD
writemyfile("Initializing BME280...: 0x\n");
#endif
#ifdef DEBUG
Serial.print("Initializing BME280 card... id--> ");
#endif
bme280setup();
delay(LCDDELAY);
#ifdef DEBUG
Serial.println(mySensor.begin(), DEC);
#endif

#ifdef SDCARD
writemyfile("GPS Setup........\n");
#endif
#ifdef DEBUG
Serial.println("GPS Setup ...");
#endif
Serial1.begin(9600);

pinMode(GPS_TX_DIGITAL_OUT_PIN, INPUT);
pinMode(GPS_RX_DIGITAL_OUT_PIN, INPUT);

startMillis = millis();
delay(LCDDELAY);

#ifdef SDCARD
myFile.close();
#endif
#ifdef DEBUG
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
#endif
server.begin();
#ifdef DEBUG
Serial.println("Server Started");
#endif
}

void loop() {
}

johnbotsis:
I have a problem on my sketch SD card initialization failed. Here is my sketch without the main loop section....

Link to shield you are using is required because I can't be bothered to search for it.

.

you mean GPS shied?

How do you have the GPS card connected? Is it between the ethernet shield and the Mega? The SPI data lines are connected to the shields by the ICSP pins, so the ethernet shield's ICSP pins are too short to put the GPS card on top of it.

GPS shield is between Mega and Ethernet shield....

Are you using the SD card on the GPS shield or the ethernet shield?

On the Ethernet shield.

I highly recommend disabling all SPI devices before initializing any of them. This covers the ethernet shield:

void setup()
{
   digitalWrite(4,HIGH);
   digitalWrite(10,HIGH);

// rest of your setup

The same problem....

What I would do is have the GPS shield alone and try to get it to work.
In so doing you will find out its quirks.
Having the GPS and the SD could also give you problems even if you didn't have the Ethernet shield.

I remove Ethernet card but the problem still remain with GPS card alone.

johnbotsis:
I remove Ethernet card but the problem still remain with GPS card alone.

Yeah, you need to figure what it needs.
Perhaps look at whatever documentation DuinoPeak provides.

The board shows that the normal slave select for the SD (D4) is used by the serial selector, and D8 is the SD slave select. Change your call in setup to this:

 while (!SD.begin(8)) {

Take a look at the schematic here.
http://qqtrading.com.my/gps-shield-neo-6m-arduino-shield
Note next to D8 is SD_CS.

Done!!! Thanks!