1

I'm trying to connect multiple sensors to an arduino and write the retrieved data in to a SD. When I test each module separately it works properly, but when I put everything together the SD card module doesn't work, can anybody help me?

This is the code that I'm using:

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <MS5611.h>

// SD
#define GPS_STORED_DATA_FILE_NAME "gps.txt"
#define SDCARD_PIN 10
bool _sdError = false;

// GPS
#define GPS_RX_PIN 9
#define GPS_TX_PIN 8
#define GPS_BAUD 9600
TinyGPSPlus _gps;
SoftwareSerial _ss (GPS_RX_PIN, GPS_TX_PIN);
unsigned long _timeLastMeasurementGps;

// MS5611
MS5611 _ms5611;

void sdTest(double latitude, double longitude, float elevation, double time) {
  if (_sdError) {
    //Serial.print("SD ERROR. Stopping writing");
    return;
  }
  String fileName = GPS_STORED_DATA_FILE_NAME;
  File myFile = SD.open(fileName, FILE_WRITE);

  if (myFile) {
    Serial.print("Writing to ");
    Serial.print(fileName);
    Serial.print("...");
    //myFile.println("testing 1, 2, 3.");
    myFile.print(latitude);
    myFile.print(" | ");
    myFile.print(longitude);
    myFile.print(" | ");
    myFile.print(elevation);
    myFile.print(" | ");
    myFile.println(time);
    myFile.flush();
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening");
    Serial.print(fileName);
    _sdError = true;
  }

  // re-open the file for reading:
  myFile = SD.open(fileName);
  if (myFile) {
    Serial.print(fileName);
    Serial.println(":");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
        // Serial.write(myFile.read());
      myFile.read();
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.print("error opening ");
    Serial.println(fileName);
  }
}

void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (_ss.available())
      _gps.encode(_ss.read());
  } while (millis() - start < ms);
}

void setupSd() {
  Serial.print("Initializing SD...");
  pinMode(SS, OUTPUT);

  if (!SD.begin(SDCARD_PIN))
    Serial.println(" ERROR");
  else
    Serial.println(" DONE");
}

void setupGps() {
  Serial.print("Initializing GPS...");
  _ss.begin(GPS_BAUD);
  smartDelay(1000);
  _timeLastMeasurementGps = millis();
  Serial.println(" DONE");
}

void setupMs5611()
{
   Serial.print("Initializing vario...");
   while(!_ms5611.begin(MS5611_ULTRA_HIGH_RES))
  {
    Serial.println("Could not find a valid MS5611 sensor, check wiring!");
    delay(500);
  }
  // Serial.print("ms5611 Oversampling: ");
  // Serial.println(_ms5611.getOversampling());
  Serial.println(" DONE");
}

void setup()
{
  Serial.begin(115200);
  setupMs5611();
  setupSd();
  setupGps();
}

void loop()
{
  delay (100);
   sdTest(1, 2, 3, 4);
}

Thanks in advance!

2

I suspect that your problem lies with how your SD card is connecting via the SPI bus.

The problem is that a lot of Serial Peripheral Interface (SPI) devices, particularly the cheaper ones, don't properly put the MISO pin into high-impedance when SS is pulled high.

I had almost exactly this situation with one of my projects. I eventually resolved it by placing a tri-state buffer in the MISO line for each of the SPI devices.

I used a 74HC125 (4-channel tri-state buffer).

This is the page on Better SPI Bus Design in 3 steps that finally got me on the right track. I think the explanations are reasonably clear (at least compared with many other sites I found!).

1

If you are using an SPI SD breakout board (like 50 cents on AliExpress), than it does work only when not connected to other SPI devices.

However, when connected to other SPI devices it cannot communicate. The only way I could overcome this problem was to use an SD recorder/logger, which is a shield that fits on an Arduino (and contains a small proto space and sometimes a RTC. Or use a device that has multiple SPI ports.

Type (SD reader) that do not work together with other SPI devices:

enter image description here

Type (SD Data recorder/logger) that do work together with other SPI devices:

enter image description here

  • I just tried to connect the GPS to 5V and the SD worked, but when I plugged the LCD screen that uses 3.3V then it stopped working again. Could you recommend an SD module to buy it? – Joan P.S Oct 16 '17 at 12:05
  • Updated my answer ... – Michel Keijzers Oct 16 '17 at 12:13
0

The SD I have is the following:

https://www.banggood.com/Micro-SD-TF-Card-Memory-Shield-Module-SPI-Mciro-SD-Adapter-For-Arduino-p-919914.html

Using the SdFat library I managed to make it work a "little": if I draw something in the external screen (PCD8544) during the setUp process the SD card will not work, otherwise it will work. I'll end up buying a new SD card reader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.