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