SD Card error

hello,

I'm developing a simple program that logs some data to a sd card.
however the sd card never initializes.
i'm using:
Arduino uno
Sd card shield (http://imall.iteadstudio.com/im120417007.html) (switch at 3.3v)
LCD shield (Gravity: 1602 LCD Keypad Shield For Arduino - DFRobot)

here's my code:

#include <SD.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <SPI.h>

LiquidCrystal lcd(8,13,9,4,5,6,7);
RTC_DS1307 RTC;
File myFile;
const int hall_sensor = 2;
const int led = 13;
const int piez = 2;
const int override = 3;
const int chipSelect = 10;

void setup(){
  RTC.begin();
  Serial.begin(9600);
  Wire.begin();
  lcd.begin(16, 2);
  SD.begin();
  //  RTC.adjust(DateTime(__DATE__, __TIME__)); 


  pinMode(hall_sensor, INPUT);  
  pinMode(led, OUTPUT);  
  pinMode(piez, OUTPUT);
  pinMode(override, INPUT);


  lcd.setCursor(0,0);
  lcd.print("   A  INICIAR   ");
  lcd.setCursor(0,1);
  lcd.print("     ESPERE     ");
  delay(5000); //TEMPO DE CALIBRAÇÃO
  lcd.clear();
  lcd.setCursor(0,0);

  lcd.print(" SISTEMA ACTIVO ");
  delay(2000);
  Serial.println("A INICIAR O CARTÃO.");


  pinMode(10, OUTPUT);

  if (!SD.begin(chipSelect)) {
    Serial.println("A INICIALIZAÇÃO DO SD FALHOU");
    return;
  }0
  Serial.println("INICIALIZAÇÃO BEM SUCEDIDA.");

  // ABRIR O FICHEIRO
  myFile = SD.open("logSENSE.txt", FILE_WRITE);

  // SE O FICHEIRO ABRIR:
  if (myFile) {
    Serial.print("A ESCREVER NO logSENSE.txt...");
    myFile.println("teste 1, 2, 3.");
    // FECHAR O FICHEIRO:
    myFile.close();
    Serial.println("done.");
  } 
  else {
    // SE O FICHEIRO NÃO ABRIR
    Serial.println("ERRO AO ABRIR logSENSE.txt");
  }

  // RE-ABRIR O FICHEIRO PARA LEITURA
  myFile = SD.open("logSENSE.txt");
  if (myFile) {
    Serial.println("logSENSE.txt:");

    // LER O FICHEIRO ATÉ AO FIM E POSICIONAR O CURSOR NA ULTIMA LINHA
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // FECHAR O FICHEIRO
    myFile.close();
  } 
  else {

    Serial.println("ERRO AO ABRIR O FICHEIRO");
  }
}

void indicator_hall_sensor(){
  digitalWrite(led, HIGH);
  tone(piez, 300);
  delay(1000);
  digitalWrite(led, LOW);
  delay(1000);
}

void key(){
  DateTime now = RTC.now();
  //delay(2500);  
  int override  = digitalRead(3);
  //delay(2500);
  if (override == HIGH){
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(" CHAVE   ACEITE ");
    delay(2500);  
    //INICIA O OUTPUT PARA A SERIAL 9600
    Serial.println("Registado acesso autorizado");
    Serial.print(now.hour(), DEC);
    Serial.print(":");
    Serial.print(now.minute(), DEC);
    Serial.print(":");
    Serial.println(now.second(), DEC);
    Serial.print(now.day(), DEC);
    Serial.print("/");
    Serial.print(now.month(), DEC);
    Serial.print("/");
    Serial.println(now.year(), DEC);

    //INICIA O OUTPUT PARA O CARTÃO SD
    myFile.println("Registado acesso autorizado");
    myFile.print(now.hour(), DEC);
    myFile.print(":");
    myFile.print(now.minute(), DEC);
    myFile.print(":");
    myFile.println(now.second(), DEC);
    myFile.print(now.day(), DEC);
    myFile.print("/");
    myFile.print(now.month(), DEC);
    myFile.print("/");
    myFile.println(now.year(), DEC);

  }
  else{
  }
}

void trip_hall_sensor(){ 
  DateTime now = RTC.now();

  delay(2500);
  int override  = digitalRead(3);
  delay(2500);
  if (override == HIGH){
    key();  
  }

  else if (override == LOW){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("INICIAR  REGISTO");

      //INICIA O OUTPUT PARA A SERIAL 9600
      Serial.println("Registado acesso não autorizado");
      Serial.print(now.hour(), DEC);
      Serial.print(":");
      Serial.print(now.minute(), DEC);
      Serial.print(":");
      Serial.println(now.second(), DEC);
      Serial.print(now.day(), DEC);
      Serial.print("/");
      Serial.print(now.month(), DEC);
      Serial.print("/");
      Serial.println(now.year(), DEC);


      //INICIA O OUTPUT PARA O CARTÃO SD
      myFile.println("Registado acesso não autorizado");
      myFile.print(now.hour(), DEC);
      myFile.print(":");
      myFile.print(now.minute(), DEC);
      myFile.print(":");
      myFile.println(now.second(), DEC);
      myFile.print(now.day(), DEC);
      myFile.print("/");
      myFile.print(now.month(), DEC);
      myFile.print("/");
      myFile.println(now.year(), DEC);
      indicator_hall_sensor();

    }
  
}
void loop(){
  int override = digitalRead(3);
  int hall_sensor = digitalRead(2);
  if ( hall_sensor == HIGH){

    //delay(10000);
    trip_hall_sensor();
  }

  else
    digitalWrite(led, LOW);
  noTone(piez);


  DateTime now = RTC.now();
  /*
  //INICIA O OUTPUT PARA A SERIAL 9600
   Serial.println("A porta está fechada");
   Serial.print(now.hour(), DEC);
   Serial.print(":");
   Serial.print(now.minute(), DEC);
   Serial.print(":");
   Serial.println(now.second(), DEC);
   Serial.print(now.day(), DEC);
   Serial.print("/");
   Serial.print(now.month(), DEC);
   Serial.print("/");
   Serial.println(now.year(), DEC);
   
   
   //INICIA O OUTPUT PARA O CARTÃO SD
   myFile.println("A porta está fechada");
   myFile.print(now.hour(), DEC);
   myFile.print(":");
   myFile.print(now.minute(), DEC);
   myFile.print(":");
   myFile.println(now.second(), DEC);
   myFile.print(now.day(), DEC);
   myFile.print("/");
   myFile.print(now.month(), DEC);
   myFile.print("/");
   myFile.println(now.year(), DEC);
   */
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" SISTEMA ACTIVO ");
  lcd.setCursor(0,1);
  lcd.print("    ");
  lcd.print(now.hour(),DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
  delay(200);

}

thanks in advance

The SD card is an SPI device. On a 328-based Arduino, that means that it uses pins 10, 11, 12, and 13 AND that you can't use those pins for ANYTHING else.

You are currently trying to use pin 13 for 3 different purposes. Get real.

so, raw solution, discard the lcd shield?

so, raw solution, discard the lcd shield?

I wouldn't discard it. I'd send it back, and demand my money back. Only idiots design shields that use the SPI pins for non-SPI purposes.

They are nearly as bad as people that decide that pins 2 and 3 MUST be used in place of the hardware serial pins, since those are the only external interrupt pins available on the 328-based Arduinos. Yes, it makes routing the traces easier, but that is no damned excuse.

i meant discard it from the project...

thanks a lot.

i'll try removing the lcd shield and the code to it when i get home a then i'll give it another try.

thanks again!

ok, so i removed the LCD shield and commented all the lcd and led parts of my sketch, still getting initialization failed.

here's the code:

#include <SD.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <SPI.h>

//LiquidCrystal lcd(8,13,9,4,5,6,7);
RTC_DS1307 RTC;
File myFile;
const int hall_sensor = 2;
//const int led = 13;
const int piez = 2;
const int override = 3;
const int chipSelect = 10;

void setup(){
  RTC.begin();
  Serial.begin(9600);
  Wire.begin();
  //lcd.begin(16, 2);
  SD.begin();
  //  RTC.adjust(DateTime(__DATE__, __TIME__)); 


  pinMode(hall_sensor, INPUT);  
  //pinMode(led, OUTPUT);  
  pinMode(piez, OUTPUT);
  pinMode(override, INPUT);

/*
  lcd.setCursor(0,0);
  lcd.print("   A  INICIAR   ");
  lcd.setCursor(0,1);
  lcd.print("     ESPERE     ");
  */
  delay(5000); //TEMPO DE CALIBRAÇÃO
  /*
  lcd.clear();
  lcd.setCursor(0,0);

  lcd.print(" SISTEMA ACTIVO ");
  */
  delay(2000);
  
  Serial.println("A INICIAR O CARTÃO.");


  pinMode(10, OUTPUT);

  if (!SD.begin(chipSelect)) {
    Serial.println("A INICIALIZAÇÃO DO SD FALHOU");
    return;
  }
  Serial.println("INICIALIZAÇÃO BEM SUCEDIDA.");

  // ABRIR O FICHEIRO
  myFile = SD.open("logSENSE.txt", FILE_WRITE);

  // SE O FICHEIRO ABRIR:
  if (myFile) {
    Serial.print("A ESCREVER NO logSENSE.txt...");
    myFile.println("teste 1, 2, 3.");
    // FECHAR O FICHEIRO:
    myFile.close();
    Serial.println("done.");
  } 
  else {
    // SE O FICHEIRO NÃO ABRIR
    Serial.println("ERRO AO ABRIR logSENSE.txt");
  }

  // RE-ABRIR O FICHEIRO PARA LEITURA
  myFile = SD.open("logSENSE.txt");
  if (myFile) {
    Serial.println("logSENSE.txt:");

    // LER O FICHEIRO ATÉ AO FIM E POSICIONAR O CURSOR NA ULTIMA LINHA
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // FECHAR O FICHEIRO
    myFile.close();
  } 
  else {

    Serial.println("ERRO AO ABRIR O FICHEIRO");
  }
}

void indicator_hall_sensor(){
  //digitalWrite(led, HIGH);
  //tone(piez, 300);
 // delay(1000);
  //digitalWrite(led, LOW);
  delay(1000);
}

void key(){
  DateTime now = RTC.now();
  //delay(2500);  
  int override  = digitalRead(3);
  //delay(2500);
  if (override == HIGH){
    //lcd.clear();
    //lcd.setCursor(0,0);
    //lcd.print(" CHAVE   ACEITE ");
    delay(2500);  
    //INICIA O OUTPUT PARA A SERIAL 9600
    Serial.println("Registado acesso autorizado");
    Serial.print(now.hour(), DEC);
    Serial.print(":");
    Serial.print(now.minute(), DEC);
    Serial.print(":");
    Serial.println(now.second(), DEC);
    Serial.print(now.day(), DEC);
    Serial.print("/");
    Serial.print(now.month(), DEC);
    Serial.print("/");
    Serial.println(now.year(), DEC);

    //INICIA O OUTPUT PARA O CARTÃO SD
    myFile.println("Registado acesso autorizado");
    myFile.print(now.hour(), DEC);
    myFile.print(":");
    myFile.print(now.minute(), DEC);
    myFile.print(":");
    myFile.println(now.second(), DEC);
    myFile.print(now.day(), DEC);
    myFile.print("/");
    myFile.print(now.month(), DEC);
    myFile.print("/");
    myFile.println(now.year(), DEC);

  }
  else{
  }
}

void trip_hall_sensor(){ 
  DateTime now = RTC.now();

  delay(2500);
  int override  = digitalRead(3);
  delay(2500);
  if (override == HIGH){
    key();  
  }

  else if (override == LOW){
      //lcd.clear();
      //lcd.setCursor(0,0);
      //lcd.print("INICIAR  REGISTO");

      //INICIA O OUTPUT PARA A SERIAL 9600
      Serial.println("Registado acesso não autorizado");
      Serial.print(now.hour(), DEC);
      Serial.print(":");
      Serial.print(now.minute(), DEC);
      Serial.print(":");
      Serial.println(now.second(), DEC);
      Serial.print(now.day(), DEC);
      Serial.print("/");
      Serial.print(now.month(), DEC);
      Serial.print("/");
      Serial.println(now.year(), DEC);


      //INICIA O OUTPUT PARA O CARTÃO SD
      myFile.println("Registado acesso não autorizado");
      myFile.print(now.hour(), DEC);
      myFile.print(":");
      myFile.print(now.minute(), DEC);
      myFile.print(":");
      myFile.println(now.second(), DEC);
      myFile.print(now.day(), DEC);
      myFile.print("/");
      myFile.print(now.month(), DEC);
      myFile.print("/");
      myFile.println(now.year(), DEC);
      indicator_hall_sensor();

    }
  
}
void loop(){
  int override = digitalRead(3);
  int hall_sensor = digitalRead(2);
  if ( hall_sensor == HIGH){

    //delay(10000);
    trip_hall_sensor();
  }

  else
    //digitalWrite(led, LOW);
 // noTone(piez);


  DateTime now = RTC.now();
  /*
  //INICIA O OUTPUT PARA A SERIAL 9600
   Serial.println("A porta está fechada");
   Serial.print(now.hour(), DEC);
   Serial.print(":");
   Serial.print(now.minute(), DEC);
   Serial.print(":");
   Serial.println(now.second(), DEC);
   Serial.print(now.day(), DEC);
   Serial.print("/");
   Serial.print(now.month(), DEC);
   Serial.print("/");
   Serial.println(now.year(), DEC);
   
   
   //INICIA O OUTPUT PARA O CARTÃO SD
   myFile.println("A porta está fechada");
   myFile.print(now.hour(), DEC);
   myFile.print(":");
   myFile.print(now.minute(), DEC);
   myFile.print(":");
   myFile.println(now.second(), DEC);
   myFile.print(now.day(), DEC);
   myFile.print("/");
   myFile.print(now.month(), DEC);
   myFile.print("/");
   myFile.println(now.year(), DEC);
   */
  
  /*
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" SISTEMA ACTIVO ");
  lcd.setCursor(0,1);
  lcd.print("    ");
  lcd.print(now.hour(),DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
  */
  delay(200);

}

i've been going arround the code for a while now and i can't find the problem... probably it's right in front of my eyes and i cant see it... :roll_eyes:

MidGard0898:
i've been going arround the code for a while now and i can't find the problem... probably it's right in front of my eyes and i cant see it... :roll_eyes:

Ditch all the crap that has nothing to do with the SD card. You don't need to include RTClib, Wire.h, LiquidCrystal, etc. And do NOT create instances of them.

Have you tried the examples that come with the SD class?

i'll try making a test sketch only for the sd...