Go Down

Topic: SD card failed or not present issues [SOLVED] (Read 2684 times) previous topic - next topic

jmppts

Jan 14, 2020, 07:07 pm Last Edit: Jan 28, 2020, 10:54 pm by jmppts
greetings fellow techs!

working on a HVAC controller and I'm having an issue seeing my SD card. complete code below for review however as far as i can see i am following the example for using the SD shield i have.

I can see the sketch is getting to this line

     Serial.println("Card failed, or not present");


however it is not running the code on the lines directly under that containing:

     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("Card Failure or");
     lcd.setCursor(1,1);
     lcd.print("not present");



I suspect I have something coded wrong causing a problem but I am not seeing it.



hardware specs:
Adeept mega 2560- controller

Osepp MSDS 01 - SD card shield

Includes used

Code: [Select]

#include <Servo.h>
#include <LiquidCrystal.h>
#include <dht11.h>
#include <SPI.h>
#include <SD.h>
                  //The SD card interfaces over
                  //SPI (SCK on D13, MISO on D12, MOSI on D11, and CS on D8).



variables assigned in reguards to SD card sheild

Code: [Select]


const int SD_CS = 8;                        //CS pin for SD card reader shield





problem happens in void setup

Code: [Select]

{
  Serial.begin(9600);
  while (!Serial)
    {
      ;                               // wait for serial port to connect. Needed for native USB port only
    }
  Serial.println("Running void setup");
  Serial.println("Initializing LCD");
  lcd.begin(16, 2);                                   // set up the LCD's number of columns and rows:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("       LCD     ");
  lcd.setCursor(1,1);
  lcd.print("   Activated   ");
  delay(2000);
  Serial.print("Initializing SD card...");
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Initializing");
  lcd.setCursor(1,1);
  delay(1500);
  lcd.print("SD card...");
  delay(3000);
                                                   // see if the card is present and can be initialized:
  
  if (!SD.begin(SD_CS))                           //it did not see an SD card
    {
      Serial.println("Card failed, or not present");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Card Failure or");
      lcd.setCursor(1,1);
      lcd.print("not present");
      delay(4000);
                                    // don't do anything else you need to install a SD card
      while (1);
    }
  else
    {
      Serial.println("SD card found");        // if did see the SD card
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("SD card found");

                                          // try to open the config file
  
      File dataFile = SD.open("acconfig.txt");

                                        // if the file is available, read from it
      if (dataFile)
        {
          while (dataFile.available())
            {
              setup_flag = 42;
              Serial.println("Config file Found");
              lcd.setCursor(1,1);
              lcd.print("Config Found");
              delay(3000);
            }
          dataFile.close();
        }
                          // if the file isn't open, then it doesnt exist which means you need to run the setup
      else
        {
          Serial.println("No Config file found Loading setup");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("No Config");
          lcd.setCursor(1,1);
          lcd.print("Loading setup");
          delay(4000);
          virgin();           //this should run the set up if its in the right place
        }
    }
    


 
The Devil drank my milk, and angels stole my blueberry muffins

jmppts

possibly figured out what i did wrong and wondering if I can manually configure to make this easier on me

I just read that the mega does SPI on pins 50, 51, and 52 and my shield is trying to use pins 11, 12, and 13.

I've been looking in the SD library and SPI library for a way to tell my mega to communicate on 11, 12, and 13 instead of the other pins but I can not seem to find any such code.

I know I can route the shield pins to the correct pins on the mega but then i cant stack them and id rather keep them stacked


I figured if i can do a pin assignment for the CS pin I should be able to assign the other pins right?

is there such a code or should i just break down and get a different SD card shield?
The Devil drank my milk, and angels stole my blueberry muffins

jmppts

another update-

as I suggested above I attempted to just jump pins 11, 12, and 13 on the SD shield to pins 50, 51, 52 on the 2560. I did originally have the LCD pins d4-d8 on pins 50-53 which I believe is what was causing the garbage to come up on the LCD screen when it failed to see the SD card.


now that the LCD is moved to pins 28-33 the LCD now correctly displays the no SD card found error like it should.

where in lies the problem of it still does not see the SD card. I have tried the o/i jumpers as noted above as well as going to pins 11,12 and 13 on the 2560 as the SD shield wants but both ways still show no SD card plugged in

I have downloaded and formatted the SD card with the SD Memory Card Formatter

I have quadruple checked my wiring

I have verified none of my hardware by default wants to use an i/o something else is using.

I do see the LED on the SD shield brighten up when it tries to initialize it if that matters.

I am using 3.3v as required on the SD shield


any ideas would be super.
The Devil drank my milk, and angels stole my blueberry muffins

jmppts


yet another update.


in an effort to insure I am not dealing with a hardware issue I have paired the osepp SD shield with an osepp uno max so i do not have to deal with one board trying to default to differant pins.

i have also swapped out my 512mb SD card for an 8Gig SD card now.

I am still failing at the exact same spot.
The Devil drank my milk, and angels stole my blueberry muffins

jmppts

#4
Jan 25, 2020, 06:31 pm Last Edit: Jan 25, 2020, 11:59 pm by jmppts
code updates to go with the new hardware

includes and variables

Code: [Select]

#include <Servo.h>
#include <LiquidCrystal.h>
#include <dht11.h>
#include <SPI.h>
#include <SD.h>
#include <ctype.h>
                  //The SD card interfaces over
                  //SPI (SCK on D13, MISO on D12, MOSI on D11, and CS on D8).
              
                  //HADWARE RELATED VARIABLES
dht11 DHT11;
File myFile;

Servo servo1;                                //create servo object to control the first servo
Servo servo2;                                //create servo object to control the second servo
LiquidCrystal lcd(2,3,4,5,6,7);     // pin assignment for LCD

          // these variables are assigning input and out put pins names
Sd2Card card;
SdVolume volume;
SdFile root;

const int SD_CS = 8;                        //CS pin for SD card reader shield          

const int DLS_Pin = 0;                              // anolog input pin for day light sensor asginment
#define IST_Pin 1                              // anolog input for DHT11 temp / humid sensor
const int OST_Pin = 2;                              // anolog input pin for outside temp sensor
const int SHT_Pin = 3;                              // anolog input pin for solor heater internal temp



setup void is where the problems happen. In the hopes of at least seeing the SD card I have also grabbed some code to read the type of SD card until i finish the code for that section. I included it in case its causing problems

Code: [Select]

void setup()
{
  Serial.begin(9600);
  while (!Serial)
  
    {
      ;                               // wait for serial port to connect. Needed for native USB port only
    }
  Serial.println("Running void setup");
  Serial.println("Initializing LCD");
  lcd.begin(16, 2);                                   // set up the LCD's number of columns and rows:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("       LCD     ");
  lcd.setCursor(1,1);
  lcd.print("   Activated   ");
  delay(2000);
  Serial.println("Initializing SD card...");
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Initializing");
  lcd.setCursor(1,1);
  delay(1500);
  lcd.print("SD card...");
  pinMode(SD_CS, OUTPUT);
  card.init();               //Initialize the SD card and configure the I/O pins.
  volume.init(card);         //Initialize a volume on the SD card.
  root.openRoot(volume);     //Open the root directory in the volume.
  delay(3000);
                                                   // see if the card is present and can be initialized:
  
  if (!card.init(SD_CS))                           //it did not see an SD card
    {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Card Failure or");
      lcd.setCursor(1,1);
      lcd.print("not present");
      Serial.println("Card failed or not present");
      delay(4000);
                                    // don't do anything else you need to install a SD card
      while (1);
    }
  else
    {
      Serial.println("SD card found");        // if did see the SD card
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("SD card found");
      Serial.println();
      Serial.print("Card type:         ");
      switch (card.type())
        {
          case SD_CARD_TYPE_SD1:
          Serial.println("SD1");
        break;
          case SD_CARD_TYPE_SD2:
          Serial.println("SD2");
        break;
          case SD_CARD_TYPE_SDHC:
          Serial.println("SDHC");
        break;
          default:
          Serial.println("Unknown");
        }
        

                                          // try to open the config file
  
      File dataFile = SD.open("acconfig.txt");

                                        // if the file is available, read from it
      if (!volume.init(card))
        {
          Serial.println("Although there is a SD card it is not formated correctly");
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("    SD card    ");
          lcd.setCursor(0,1);
          lcd.print("Invalid Format");
          while(1);
        }
          
          Serial.print("Clusters:          ");
          Serial.println(volume.clusterCount());
          Serial.print("Blocks x Cluster:  ");
          Serial.println(volume.blocksPerCluster());

          Serial.print("Total Blocks:      ");
          Serial.println(volume.blocksPerCluster() * volume.clusterCount());
          Serial.println();

                              // print the type and size of the first FAT-type volume
          uint32_t volumesize;
          Serial.print("Volume type is:    FAT");
          Serial.println(volume.fatType(), DEC);

          volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
          volumesize *= volume.clusterCount();       // we'll have a lot of clusters
          volumesize /= 2;                           // SD card blocks are always 512 bytes (2 blocks are 1KB)
          Serial.print("Volume size (Kb):  ");
          Serial.println(volumesize);
          Serial.print("Volume size (Mb):  ");
          volumesize /= 1024;
          Serial.println(volumesize);
          Serial.print("Volume size (Gb):  ");
          Serial.println((float)volumesize / 1024.0);

          Serial.println("\nFiles found on the card (name, date and size in bytes): ");
          root.openRoot(volume);

          // list all files in the card with date and size
          root.ls(LS_R | LS_DATE | LS_SIZE);
        }
                          // if the file isn't open, then it doesnt exist which means you need to run the setup
    
The Devil drank my milk, and angels stole my blueberry muffins

ShermanP

Nobody is giving you any help on this, and I'm afraid I have no experience with any of the stuff you're using.  All I can suggest is that if your LCD also uses SPI, there may be a problem using two SPI devices.  You said you have the LCD working right if the SD isn't there, but you need to get the SD card working the other way - without the LCD, and using regular serial to give you feedback.  And maybe using  a simple Example sketch like CardInfo.ino to test with.  I'm really just guessing, but the idea is to get the SD working at the simplest level, with no competition, then try to figure out why it doesn't play well with others.  Regular microSD modules have a design flaw that causes this problem, but I don't know about your SD shield.


jmppts

#6
Jan 25, 2020, 11:41 pm Last Edit: Jan 25, 2020, 11:54 pm by jmppts
I appreciate you taking the time to reply. it's definitely something with the code i can load the example sketch and it works fine plugged into the working design. just when i incorporate it into my sketch is where the problem is.

this is the example

Code: [Select]

/*
    Basic example reading and writing to the microSD shield
    Based on the SdFat Library by Bill Greiman
    SparkFun Electronics

    all ive done so far is changed it from the SdFat Library to the SD library
    and removed the SdFatUtil.h library it seems to work i can put in a blank
    Sd card and it does the below.

    
    This example creates a file (or opens it if the file already exists) named 'Test.txt.'
    A string with the value 'Millis: (number of millis)' is appended to the end of the file.
    After writing to the file, the contents of the entire file are printed to the serial port.
 */
 
//Add the SdFat Libraries or the SD library
#include <SD.h>
#include <SPI.h>
#include <ctype.h>

//Create the variables to be used by SdFat Library
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

char name[] = "Test.txt";     //Create an array that contains the name of our file.
char contents[256];           //This will be a data buffer for writing contents to the file.
char in_char=0;
int index=0;                  //Index will keep track of our position within the contents buffer.

void setup(void)
{  
    Serial.begin(9600);        //Start a serial connection.
    pinMode(8, OUTPUT);       //Pin 10 must be set as an output for the SD communication to work.
    card.init();               //Initialize the SD card and configure the I/O pins.
    volume.init(card);         //Initialize a volume on the SD card.
    root.openRoot(volume);     //Open the root directory in the volume.
}
void loop(void){    
    file.open(root, name, O_CREAT | O_APPEND | O_WRITE);    //Open or create the file 'name' in 'root' for writing to the end of the file.
    sprintf(contents, "Millis: %d    ", millis());    //Copy the letters 'Millis: ' followed by the integer value of the millis() function into the 'contents' array.
    file.print(contents);    //Write the 'contents' array to the end of the file.
    file.close();            //Close the file.
    
    file.open(root, name, O_READ);    //Open the file in read mode.
    in_char=file.read();              //Get the first byte in the file.
    //Keep reading characters from the file until we get an error or reach the end of the file. (This will output the entire contents of the file).
    while(in_char >=0){            //If the value of the character is less than 0 we've reached the end of the file.
        Serial.print(in_char);    //Print the current character
        in_char=file.read();      //Get the next character
    }
    file.close();    //Close the file
    delay(1000);     //Wait 1 second before repeating the process.



works just fine


oh and there is a misunderstanding i did fix the LCD issue and it was a conflict with the pins the mega was trying to communicate on but i fixed that. the issue is still with the SD card not being seen if i try it with my code instead of theirs.

i'll keep commenting my code out till i find the problem thanks for the suggestion. it gives me a place to concentrate.
The Devil drank my milk, and angels stole my blueberry muffins

ShermanP

Could you post the full sketch up to the point where it fails to find the card?


jmppts

i posted it in sections above but here it is all at once

Code: [Select]

#include <Servo.h>
#include <LiquidCrystal.h>
#include <dht11.h>
#include <SPI.h>
#include <SD.h>
#include <ctype.h>
                  //The SD card interfaces over
                  //SPI (SCK on D13, MISO on D12, MOSI on D11, and CS on D8).
               
                  //HADWARE RELATED VARIABLES
dht11 DHT11;
File myFile;

Servo servo1;                                //create servo object to control the first servo
Servo servo2;                                //create servo object to control the second servo
LiquidCrystal lcd(2,3,4,5,6,7);     // pin assignment for LCD

          // these variables are assigning input and out put pins names
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

const int SD_CS = 8;                        //CS pin for SD card reader shield         

const int DLS_Pin = 0;                              // anolog input pin for day light sensor asginment
#define IST_Pin 1                              // anolog input for DHT11 temp / humid sensor
const int OST_Pin = 2;                              // anolog input pin for outside temp sensor
const int SHT_Pin = 3;                              // anolog input pin for solor heater internal temp

const int Control_menu = 22;                        // digital input for menu button
const int Control_exit = 23;                        // digital input for exit button
const int Control_left = 24;                        // digital input for left/decrease button
const int Control_right = 25;                       // digitral input for right/increase button

int joy_pinx = 4;                                   //anolog input for thumb stick x axis
int joy_piny = 5;                                   //anolog input for thumb stick y axis
int joy_pinsw = 9;                                 //digital input for thumbstick press (shared with Control menu as they do the same thing on all menus

const int Fan_F = 26;                               // INA on fan at the moment
const int Fan_R = 27;                               // INB on fan at the moment

const int setup_F_LED = 28;
const int setup_P_LED = 29;
const int DLS_F_LED = 30;
const int DLS_P_LED = 31;

const int CM_triggered_LED = 32;

              //HARDWARE TO SOFTWARE VARIABLES HERE
           
            // variables for controls are blow here

int ButtonPressCount = 0;                       //counts the number of times a button is pressed
int CM_state;                                   //current state of the control menu button
int CE_state;                                   //current state of the control exit button
int CL_state;                                   //current state of the left or decrease control button
int CR_state;                                   //current state of the right or increase control button
int CM_LBS;                                     // menu button last button state
int CE_LBS;                                     // exit button last button state
int CL_LBS;                                     // left button last button state
int CR_LBS;                                     // right button last button state


        //temperature related variables

int ITT = 0;                            //internal target temp (need to move this)     

int IST_data;
int readData;
int IST_T_C;
int IST_H;
int IST_T_F;

int OST = analogRead(OST_Pin);             // raw outside temp sensor data
int C_OST = (OST*500/1023);              // outside temp in celcius
int F_OST = ((C_OST * 9) / 5) + 32;       // outside temp in fahrenheit

int SHT = analogRead(SHT_Pin);              // raw solar heater temp sensor data
int C_SHT = (SHT*500/1023);               // solar heater temp in Celcius
int F_SHT = ((C_SHT * 9) / 5) + 32;         // solar heater temp in fahrenheit

int DLS_value = analogRead(DLS_Pin);        //raw data from the day light sensor input


              //SOFTWARE Variables

                            // special variables

int setup_flag;                   //setup flag so it knows to load from SD card or go through the setup
int self_test = 0;

int DLS_Trigger;                  //day light sensor trigger used for if check statement

int SD_test_data_0 = 0;
int SD_test_data_1 = 123;
int SD_test_data_2 = 456;
int SD_test_data_3 = 789;


                                             // arrays below here

                                             // this array is for the SD card data
 
                                           
                                             // this array is for the servos

int VSC1[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180}; //angle array for vent servos (wont use all of them)


          // other varibles

int Fan_speed;
int Fan_min_speed = 0;
int Fan_max_speed = 150;



void setup()
{
  Serial.begin(9600);
  while (!Serial)
 
    {
      ;                               // wait for serial port to connect. Needed for native USB port only
    }
  Serial.println("Running void setup");
  Serial.println("Initializing LCD");
 // lcd.begin(16, 2);                                   // set up the LCD's number of columns and rows:
 // lcd.clear();
 // lcd.setCursor(0,0);
 // lcd.print("       LCD     ");
 // lcd.setCursor(1,1);
 // lcd.print("   Activated   ");
  delay(2000);
  Serial.println("Initializing SD card...");
 // lcd.clear();
 // lcd.setCursor(0,0);
 // lcd.print("Initializing");
 // lcd.setCursor(1,1);
 // delay(1500);
 // lcd.print("SD card...");
  pinMode(SD_CS, OUTPUT);
  card.init();               //Initialize the SD card and configure the I/O pins.
  volume.init(card);         //Initialize a volume on the SD card.
  root.openRoot(volume);     //Open the root directory in the volume.
  delay(3000);
                                                   // see if the card is present and can be initialized:
 
  if (!card.init(SD_CS))                           //it did not see an SD card
    {
//      lcd.clear();
//      lcd.setCursor(0,0);
//      lcd.print("Card Failure or");
//      lcd.setCursor(1,1);
//      lcd.print("not present");
      Serial.println("Card failed or not present");
      delay(4000);
                                    // don't do anything else you need to install a SD card
      while (1);
    }
  else
    {
      Serial.println("SD card found");        // if did see the SD card
//      lcd.clear();
//      lcd.setCursor(0,0);
//      lcd.print("SD card found");
The Devil drank my milk, and angels stole my blueberry muffins

ShermanP

#9
Jan 26, 2020, 06:15 pm Last Edit: Jan 26, 2020, 06:26 pm by ShermanP
I think you are a better programmer than I am, so I'm not sure I'll be of much help.  But a few comments:

First, I'm confused by what appears to be library mixing.  SD and SDFat are not the same library, and their examples don't work the same.  Where does card.init() come from?  Anyway, it looks like you are using an example that was written for SDFat, but you are actually #including SD.  I'm surprised it works.

Speaking of card.init, in your code you call it twice - once by itself with no CS specified, and then again in the If statement with SD_CS.  In the example, only the first call is present.  In fact, in the example you never actually specify what the CS pin is.  You set pin 8 to output, but you never say that pin 8 is CS.  Yet it runs successfully.  Maybe the format of card.init(CS) is invalid, and that's why it's erroring out.  Or maybe CS really isn't pin 8 (the original example sets pin 10 to output, so maybe it assumes CS is pin 10).

If the example works, then it must be finding the right pins for everything.  I don't know how it does that, but it would seem you need to duplicate the example code as much as possible.

For what it's worth, CS is pin 2 of the microSD card metal holder.  You might be able to follow that back through the HC4050 gate and see what it actually connects to on the Arduino.

Edit:  According to the adapter schematic, CS  goes from pin 2 of the holder through the HC4050, then connects to pin 1 of J6 and J7.



jmppts

I think you are a better programmer than I am, so I'm not sure I'll be of much help.  But a few comments:

thanks for thinking that but my base for programming is from gw and qbasic with some pascal back in the late 80's so most of what you see if based off someone else's hard work. I'm just good and figuring out where to splice what in where usually.

First, I'm confused by what appears to be library mixing.  SD and SDFat are not the same library, and their examples don't work the same.  Where does card.init() come from?  Anyway, it looks like you are using an example that was written for SDFat, but you are actually #including SD.  I'm surprised it works.

I was actually surprised this worked myself to be honest.  I'm going to download some old copies of the SDfat and utils libraries and see if i can get it to work with the outdated stuff if trying different CS pins stuff doesn't work.

I
Speaking of card.init, in your code you call it twice - once by itself with no CS specified, and then again in the If statement with SD_CS.  In the example, only the first call is present.  In fact, in the example you never actually specify what the CS pin is.  You set pin 8 to output, but you never say that pin 8 is CS.  Yet it runs successfully.  Maybe the format of card.init(CS) is invalid, and that's why it's erroring out.

i figured the first call was just an initialization call for the card since it didn't specify any of the normal CS pin assign or SPI rate. so yeah i was thinking it wouldn't hurt to call it to maybe "wake it up"? before i do the !card.int statement i guess...

Or maybe CS really isn't pin 8 (the original example sets pin 10 to output, so maybe it assumes CS is pin 10).
I think the reason the example is working right now is it is an osepp shield talking to an osepp controller and without defining CS it automatically works. Ill try removing the pin assignment and also setting to 10 to match the original example however the osepp page expressly says it is suppose to be using pin 8 for CS so im not sure about that one. I've got a smaller simpler SD shield on the way since I need to use a mega for this so the uno max working wont matter much if it doesn't transfer over to the mega when its all said and done.



Also the LCD example doesn't load the SDI library so unless its doing the SDI communication in the LCD library it should be ok i think. I haven't dug into the library yet like you said ill worry about reading the SD card first then get back to the LCD.

The Devil drank my milk, and angels stole my blueberry muffins

jmppts

found the problem for the uno max.

had to change the

if (!card.init(SD_CS))

I tried changing SD_CS to 10 first with no effect

to

if (!card.init())



works fine now.

so from that i don't think CS is on pin 8 or 10 ....

so it is a pin assignment issue on the CS I'll toss it back on the mega and do some pin monitors and figure out what pin its really using.

The Devil drank my milk, and angels stole my blueberry muffins

ShermanP

It may be pin 8 on a 328P, but something different on a 2560.  Anyway, it sound like CS is the issue.

jmppts

yup it was totally a CS issue. turns out it was in fact using pin 10 for CS even though the manufacture says pin 8.

this is why it worked with the uno max board with no issues

and the issue on the mega was it REALLY wants to use pin 53 for CS even assigning the CS pin to 10 the mega said no.

i can wire the osepp shield with jumpers and it will work on the mega with pins 50-53 now, however i went ahead and got a small SD breakout and mounted it on the mega shield i already have in place for the rest of the components i have to add in myself. I'll save the osepp pair for something else.






The Devil drank my milk, and angels stole my blueberry muffins

ShermanP

It's good that you got it working.

One advantage of the Osepp board is that, according to the schematic, it plays well with others.  So if two SPI devices share the same Clk, MOSI and MISO pins, but different CS pins, the Osepp will release the MISO line when its CS is not asserted.  I don't know what you got to replace it, but many microSD adapters do not release the line when they should.  It's just bad design.  If you have a link to the one you bought, maybe I can tell whether it suffers from this problem.

Go Up