Go Down

Topic: microSD: can read but cannot create nor write (Read 137 times) previous topic - next topic

Dec 28, 2014, 06:08 pm Last Edit: Dec 28, 2014, 08:59 pm by pentiumcadiz
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:

Code: [Select]

/*
  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:

Code: [Select]

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 !!

el_supremo

Code: [Select]
  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  :-(

 

el_supremo

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:
Code: [Select]
  logFile = SD.open("temp.log", O_APPEND | O_WRITE);

Pete

fat16lib

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
Code: [Select]

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


Opens a file like this:
Code: [Select]

  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:
Code: [Select]

  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  :-)

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

THANK YOU !!!

:-)


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy