microSD: can read but cannot create nor write

I am building a temperature 3-sensors system. I want it to save data on a microSD card.
I am using an Arduino Uno and a Catalex microSD reader.

I can read the card's info (type, size,...) but I cannot create a file and write data on it.

Here is my code:

/*
  Triple sensor de tenperatura con almacenamiento en fichero de log
  dic2014 by Nigel

  SD card test

 This example shows how use the utility libraries on which the'
 SD library is based in order to get info about your SD card.
 Very useful for testing a card when you're not sure whether its working or not.

 The circuit:
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
                Pin 4 used here for consistency with other Arduino examples


 created  28 Mar 2011
 by Limor Fried
 modified 9 Apr 2012
 by Tom Igoe
 */

// include the SD library:
#include <SD.h>
// #include <SPI.h>

File logFile;

// 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 = 4;
 const int chipSelect = 10;    // Funciona Ok con tarjeta micro SD
Catalex 12/03/2014


// Pines sensores de tenp (TMP36)
int temp = 0;  //Pin analógico A5 del Arduino donde conectaremos el
pin de datos del sensor TMP36
int temp2 = 1; // entrada temperatura A3
int temp3 = 2; // entrada temperatura A1


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(10, OUTPUT);     // change this to 53 on a mega

// pin dig 2 a tierra (0v)
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);

// pin dig 3 a Vcc (5v)
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);

  // 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?");
    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);

 // Primer intento de abrir el fichero:
  [color=blue]logFile = SD.open("temp.log", FILE_WRITE);[/color]
  if (logFile)
  {
   Serial.print("Logfile encontrado 1. Añadiendo datos");
   logFile.println("Linea de prueba");
    // close the file:
   Serial.print("Cierro -1- el fichero");
    logFile.close();
  }
   else {
    // if the file didn't open, print an error:
    Serial.println("\n **** En Setup: Error abriendo el fichero *****");
  }

}


void loop(void) {

// En esta prueba no necesitamos nada más

}

And here's the serial output:

Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC

Volume type is FAT32

Volume size (bytes): 3667918848
Volume size (Kbytes): 3581952
Volume size (Mbytes): 3498

Files found on the card (name, date and size in bytes):
DCIM/         2013-01-01 00:00:02
  100DSCIM/     2013-01-01 00:00:12
    PICT0001.JPG  2013-01-01 00:00:14 742236

 **** En Setup: Error abriendo el fichero *****

I have been looking for help in this and other forums, and I have tried (UNSUCCESSFULLY) a few ways to fix this issue:

  • Connect the microSD card to 3.3v (instead of 5.0v)
  • Connect the microSD card to an extra power supply (see second attachment).
  • Add the include SPI command
  • Format the microSD card with the "official" tool
  • ...

I need help !! Please !!

  if (logFile)

You don't do anything before this statement to open or create logFile. It is therefore NULL and you'll get the error message.

Pete

Hi, Pete!

Some how, when copying this code to this forum, I missed the command just before the IF:

logFile = SD.open("temp.log", FILE_WRITE);

(I have been modifying the sketch a all day long).

The serial output is the very same one :frowning:

When I create a file with SD.h I have not been using FILE_WRITE. I have a vague memory that it causes problems, perhaps because it not only specifies opening with write and create access but also with read.
Try this instead:

  logFile = SD.open("temp.log", O_APPEND | O_WRITE);

Pete

The example you started with is not intended as a base for normal applications. You should not have Sd2Card, SdVolume, or the SdFile root objects. These are all in the SD object so a second copy causes problems.

Start from something like the ReadWrite example. It initializes the card properly with

 if (!SD.begin(CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }

Opens a file like this:

  myFile = SD.open("test.txt", FILE_WRITE);

FILE_WRITE is the correct argument for open.

You can then print to and close the file like this:

  myFile.println("testing 1, 2, 3.");
  // close the file:
  myFile.close();

I have rebuilt my code based on the ReadWrite example and everything is coming to life :slight_smile:

In a few days I hope I can share with you a working sketch.

THANK YOU !!!

:slight_smile:

Well, I am back.

And my sketch is working ! :slight_smile:

Let's see the code:

/*
  Triple sensor de temperatura con almacenamiento de valores en microSD
  Developed by PentiumCadiz - dic2014
  Based on "SD card read/write", created by David A. Mellis and
modified by Tom Igoe
  This code is in the public domain.

 microSD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10

 */

 // include the SD library:
#include <SD.h>

File logfile;  // Fichero de log de las temperaturas

const int chipSelect = 10;    // Funciona Ok con tarjeta micro SD
Catalex dic2014

// Pines sensores de tenp (TMP36)
int tempM = 1;  //entrada temperatura del motor. Pin analógico A0 al
sensor TMP36
int tempV = 3; // entrada temperatura del Variador
int tempB = 5; // entrada temperatura de la Batería

// Medición de la temperatura
float voltajeM, gradosM;
// variables tipo float para guardar los valores de lectura de los sensores
float voltajeV, gradosV, voltajeB, gradosB;

boolean unavez = true;

//  -------  Fin de declaración de variables -----------

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(10, OUTPUT);

 // See if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

// Asignación de pines digitales a Vcc y Gnd

// pin D2 a tierra (0v)
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);

// pin D4 a tierra (0v)
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);

// pin D6 a tierra (0v)
  pinMode(6, OUTPUT);
  digitalWrite(6, LOW);

}

void loop(void) {

 // Convertir el valor que nos devuelve el analogRead(n), comprendido
entre 0 y 1023,
 // a un valor comprendido entre los 0.0 y los 5.0 voltios
  voltajeM = analogRead(tempM) * 0.004882814;
  voltajeV = analogRead(tempV) * 0.004882814;
  voltajeB = analogRead(tempB) * 0.004882814;

// Fórmula del datasheet para convertir el valor del voltaje a grados
centigrados
  gradosM = (voltajeM - 0.5) * 100.0;
  gradosV = (voltajeV - 0.5) * 100.0;
  gradosB = (voltajeB - 0.5) * 100.0;

if (unavez)
{
    Serial.print("Medidas actuales\n");
}

 //Mostramos mensaje con las temperaturas
  Serial.print("Motor: ");
  Serial.print(gradosM);
  Serial.print("\t\tVariador: ");
  Serial.print(gradosV);
  Serial.print("\t\tBatería: ");
  Serial.println(gradosB);

  // Grabar los datos leidos en la tarjeta

// Open the file.
  File logFile = SD.open("temp.log", FILE_WRITE);
  if (logFile)
  {
if (unavez)
{
    logFile.println("Grabando Medidas actuales ********************\n");
}
   logFile.print(gradosM);
   logFile.print("\t");
   logFile.print(gradosV);
   logFile.print("\t");
   logFile.println(gradosB);
   // close the file:
   logFile.close();
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening temp.log");
  }
  delay(3000);  //Usamos un retardo de 3 segundos entre lectura y lectura
  unavez = false;

}

This is the hardware:

Here is my RC car with the arduino nano on:

And finally, this is the result of my project:

I hope you like it.

Again, thank you all !!

1 Like