Go Down

Topic: [solved] SD Card Module not Initializing on Mega R3 (Read 1 time) previous topic - next topic

ebenezerv99

May 21, 2019, 06:30 pm Last Edit: May 28, 2019, 12:54 pm by ebenezerv99
Hello guys,

I am using an SD Card Module which I bought from the following link
https://robu.in/product/sd-card-reading-writing-module-arduino/
Sorry that I couldnt find its model number.
I did everything according to the tutorial in the following link
https://www.youtube.com/watch?v=HmWf25-8uEY
but could not get the module to work.

I am using the following code to test try the SD Card. It is a standard example from the library I downloaded. Link below.
https://github.com/adafruit/SD

I formatted the SD Card with the SDCardFormatter which is suggested in the forums.
I am using a 512MB SD Card. The next big SD Card I have is of 16GB(that doesnt work too)

I have connected the pins as below
CS       - 4
MOSI   - 51
SCK     - 52
MISO   - 50

The code I am using
Code: [Select]
/*
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header

 
 created   Nov 2010  by David A. Mellis
 modified 9 Apr 2012  by Tom Igoe
 
 This example code is in the public domain.
 
 */
 
#include <SPI.h>
#include <SD.h>

File myFile;

// change this to match your SD shield or module;
//     Arduino Ethernet shield: pin 4
//     Adafruit SD shields and modules: pin 10
//     Sparkfun SD shield: pin 8
const int chipSelect = 4;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(SS, OUTPUT);
   
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
 
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
// close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
   
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
// nothing happens after setup
}



 

Juraj

try the CardInfo example (in IDE examples menu). it is a diagnostics sketch
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

ebenezerv99

#2
May 21, 2019, 07:06 pm Last Edit: May 21, 2019, 07:17 pm by ebenezerv99
Thank you for your response.

I already tried the CardInfo sketch and get the following output in the Serial Monitor
Code: [Select]

Initializing SD card...initialization failed. Things to check:
* is a card inserted?
* is your wiring correct?
* did you change the chipSelect pin to match your shield or module?

 The card is inserted and I checked the wiring a thousand times.
The CS pin is connected to the digital PWM 4

I have connected MOSI,MISO,SCK of ethernet module in parallel connections which in turn is connected to their respective pins on the Arduino. Can that be causing any problems. The ethernet module is working like a charm so I dont think it is.

I would greatly appreciate any help provided
Thank You in advance

Juraj

#3
May 21, 2019, 07:58 pm Last Edit: May 22, 2019, 08:55 am by Juraj
in the SD test sketch set Ethernet shield CS pin HIGH if the Ethernet module is on the bus too.
what shield? it doesn't have SD adapter? doesn't it connect to the ICSP header?

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

ebenezerv99

#4
May 22, 2019, 08:48 am Last Edit: May 22, 2019, 08:49 am by ebenezerv99
Thank you for your response

It doesnt work even though I set the CS pin to HIGH
I am not using a shield.
I have attached the photo of the module 


https://robu.in/wp-content/uploads/2017/09/Blue-Perfect-SD-Card-Module-Slot-Socket-Reader-For-Arduino-ARM-MCU-Read-Write.jpg

Juraj

Thank you for your response

It doesnt work even though I set the CS pin to HIGH
I am not using a shield.
I have attached the photo of the module 


https://robu.in/wp-content/uploads/2017/09/Blue-Perfect-SD-Card-Module-Slot-Socket-Reader-For-Arduino-ARM-MCU-Read-Write.jpg

sorry I thought you have Ethernet shield, but you have a module.
did you test the SD module without the Ethernet module wired to the board?
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

ebenezerv99

Yes I did, that failed too.
I removed all the connections on my arduino and just connect my sd card module to their respecitve spi pins i.e.
50,51,52. And cs to pin 4.
I am giving 5V supply. The arduino is connected to 9V 1A supply from the wall.
There is no led on the sd card to tell if the module is powered. So is there a chance the module is busted?

ebenezerv99

#7
May 28, 2019, 12:53 pm Last Edit: May 28, 2019, 12:58 pm by ebenezerv99
I finally managed to solved this problem. All i had to do was use a a logic level convertor for the SD card module. Following is the code I used : I am using Arduino Mega R3

Code: [Select]
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 53;    

void setup()
{
// Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("\nInitializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(53, OUTPUT);     // change this to 53 on a mega


  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.print("\nCard 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");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
  
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);

  
  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);
}


void loop(void) {
  
}


I have attached the schematic for the module too.
Note: The Schematic is for UNO

For Arduino Mega, connect as listed below:

pin 50 instead of pin 12 (MISO)
pin 51 instead of pin 11 (MOSI)
pin 52 instead of pin 13 (SCK)
Pin 53 instead of pin 10 (CS)

I hope someone finds this useful

I used the Adafruit Library for SD card module
https://github.com/adafruit/SD

Juraj

the module has 5 V pin and doesn't have logic level conversion? did you try the second row of pins?

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

ebenezerv99


Go Up