SD card not recording data

Hi, guys.

I’m trying to run the examples of SD library but it is not working. I’m able to read the card (CardInfo example), and to create a new file when I run examples like Datalogger.
I’m using an Arduino MEGA 2560 and a Sandisk SD2 1GB card with a module identical to the one in the image


The pins are connected as follows:

MISO - 50;
SCK - 52;
MOSI - 51;
CS - 53.

The code:

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 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
 		Pin 4 used here for consistency with other Arduino examples
 
 created  24 Nov 2010
 modified 9 Apr 2012 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <SPI.h>
#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 53;

File dataFile;

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...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("card initialized.");
  
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }

  dataFile.println(dataString);

  // print to the serial port too:
  Serial.println(dataString);
  
  // The following line will 'save' the file to the SD card after every
  // line of data - this will use more power and slow down how much data
  // you can read but it's safer! 
  // If you want to speed up the system, remove the call to flush() and it
  // will save the file only every 512 bytes - every time a sector on the 
  // SD card is filled with data.
  dataFile.flush();
  
  // Take 1 measurement every 500 milliseconds
  delay(500);
}

The Serial Monitor shows the strings, and the file named datalog (or other name) is created if not existing, but nothing is recorded to that file.
I even tryed to use another pin instead of 53 (also changing that value in the code) but the same thing happened. What’s going on?

Thanks!

You have called for pin 53 for select, but I think you still need to call for it as an output

preamble

const uint8_t chipSelect = 53; // SD CSpin

setup

pinMode(53, OUTPUT);//MEGA

You seem to be using "pin SS"

I believe that the module you are using does not have the spi signal level shifters, and many (all?) cards will not work properly. See https://learn.adafruit.com/adafruit-micro-sd-breakout-board-card-tutorial/look-out

You may need to use one of the level shifting chips recommended in the link.

Nick_Pyner, I tried that but also didn't work :( cattledog, I'll check that out and tell you what I found out, thanks a lot

I tried it with the same module and same SD card in an Arduino Uno and it worked, working with 3.3 V or 5 V it worked well, even if I didn't select the SS pin as output. But when I use the Mega then it doesn't work just like before... And yes, I changed the port and etc. Go figure?

IvanRocha: Go figure?

No, that's for you to do, and you might start by reading reply #1 again. You might conclude that there is no such thing as "pin SS", and therefore writing a pinmode command for it is probably a bad idea.