Forum >Using the DFRobot SD Card Shield with Arduino Uno Rev 3
Using the DFRobot SD Card Shield with Arduino Uno Rev 3

Ladies and Gents,
I am sort of a n00b and I would like some help with regard to reading/writing to SD card.
I have an Arduino Rev 3 and a DFRobot SD Card shield (513137). The instructions I am referring to are from "http://www.dfrobot.com/wiki/index.php?title=SD_Module_(SKU:_DFR0071)". I have connected my SD card module in the same manner, and I have copied the code exactly as is from the Wiki to the Sketch where I am working.
[code]/*
* Install the SdFatV2Demo20110108 library!
* Append Example
*
* This sketch shows how to use open for append.
* The sketch will append 100 line each time it opens the file.
* The sketch will open and close the file 100 times.
*/
#include <SdFat.h>
// file system object
SdFat sd;
// create Serial stream
ArduinoOutStream cout(Serial);
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
void setup() {
// filename for this example
char name[] = "APPEND.TXT";
Serial.begin(9600);
// pstr() stores strings in flash to save RAM
cout << endl << pstr("Type any character to start\n");
while (!Serial.available());
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!sd.init(SPI_HALF_SPEED)) sd.initErrorHalt();
cout << pstr("Appending to: ") << name;
for (uint8_t i = 0; i < 100; i++) {
// open stream for append
ofstream sdout(name, ios::out | ios::app);
if (!sdout) error("open failed");
// append 100 lines to the file
for (uint8_t j = 0; j < 100; j++) {
// use int() so byte will print as decimal number
sdout << "line " << int(j) << " of pass " << int(i);
sdout << " millis = " << millis() << endl;
}
// close the stream
sdout.close();
if (!sdout) error("append data failed");
// output progress indicator
if (i % 25 == 0) cout << endl;
cout << '.';
}
cout << endl << "Done" << endl;
}
//------------------------------------------------------------------------------
void loop() {}[/code]
I noticed that in the code it mentioned to install a special library (SdFatV2Demo20110108). I downloaded this library from "http://code.google.com/p/beta-lib/downloads/detail?name=SdFatV2Demo20110108.zip&can=4&q=".
With only the new sketch containing the code from the wiki, I select "Verify/Compile" and receive the following errors:
SDCard_DFRobot:11: error: 'SdFat' does not name a type
SDCard_DFRobot:14: error: 'ArduinoOutStream' does not name a type
SDCard_DFRobot.cpp: In function 'void setup()':
SDCard_DFRobot:26: error: 'cout' was not declared in this scope
SDCard_DFRobot:26: error: 'endl' was not declared in this scope
SDCard_DFRobot:26: error: 'pstr' was not declared in this scope
SDCard_DFRobot:31: error: 'sd' was not declared in this scope
SDCard_DFRobot:31: error: 'SPI_HALF_SPEED' was not declared in this scope
SDCard_DFRobot:37: error: 'ofstream' was not declared in this scope
SDCard_DFRobot:37: error: expected `;' before 'sdout'
SDCard_DFRobot:38: error: 'sdout' was not declared in this scope
SDCard_DFRobot:38: error: 'sd' was not declared in this scope
SDCard_DFRobot:43: error: 'sdout' was not declared in this scope
SDCard_DFRobot:47: error: 'sdout' was not declared in this scope
SDCard_DFRobot:49: error: 'sd' was not declared in this scope
Starting out...I figured that I probably don't have the library properly installed. How do I properly "install" the library? How do I make sure that the code is seeing this library? I assumed that a simple #include would do the trick, but the following variations didn't work:
#include <SdFat.h>
#include <SdFat/SdFat.h>
#include "SdFat/SdFat.h"
I seems to me that since the SdFat.h file has a SdFat class, there shouldn't be a reason for SdFat to not be a valid type.
I have the sketch (.ino) file in the same directory as the directory containing the SdFat.h file. That is why I chose the SdFat/SdFat.h option.
I clearly am doing something wrong, does anyone have any clues how to help or pointers on methods to try?
Scott
I am sort of a n00b and I would like some help with regard to reading/writing to SD card.
I have an Arduino Rev 3 and a DFRobot SD Card shield (513137). The instructions I am referring to are from "http://www.dfrobot.com/wiki/index.php?title=SD_Module_(SKU:_DFR0071)". I have connected my SD card module in the same manner, and I have copied the code exactly as is from the Wiki to the Sketch where I am working.
[code]/*
* Install the SdFatV2Demo20110108 library!
* Append Example
*
* This sketch shows how to use open for append.
* The sketch will append 100 line each time it opens the file.
* The sketch will open and close the file 100 times.
*/
#include <SdFat.h>
// file system object
SdFat sd;
// create Serial stream
ArduinoOutStream cout(Serial);
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
void setup() {
// filename for this example
char name[] = "APPEND.TXT";
Serial.begin(9600);
// pstr() stores strings in flash to save RAM
cout << endl << pstr("Type any character to start\n");
while (!Serial.available());
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!sd.init(SPI_HALF_SPEED)) sd.initErrorHalt();
cout << pstr("Appending to: ") << name;
for (uint8_t i = 0; i < 100; i++) {
// open stream for append
ofstream sdout(name, ios::out | ios::app);
if (!sdout) error("open failed");
// append 100 lines to the file
for (uint8_t j = 0; j < 100; j++) {
// use int() so byte will print as decimal number
sdout << "line " << int(j) << " of pass " << int(i);
sdout << " millis = " << millis() << endl;
}
// close the stream
sdout.close();
if (!sdout) error("append data failed");
// output progress indicator
if (i % 25 == 0) cout << endl;
cout << '.';
}
cout << endl << "Done" << endl;
}
//------------------------------------------------------------------------------
void loop() {}[/code]
I noticed that in the code it mentioned to install a special library (SdFatV2Demo20110108). I downloaded this library from "http://code.google.com/p/beta-lib/downloads/detail?name=SdFatV2Demo20110108.zip&can=4&q=".
With only the new sketch containing the code from the wiki, I select "Verify/Compile" and receive the following errors:
SDCard_DFRobot:11: error: 'SdFat' does not name a type
SDCard_DFRobot:14: error: 'ArduinoOutStream' does not name a type
SDCard_DFRobot.cpp: In function 'void setup()':
SDCard_DFRobot:26: error: 'cout' was not declared in this scope
SDCard_DFRobot:26: error: 'endl' was not declared in this scope
SDCard_DFRobot:26: error: 'pstr' was not declared in this scope
SDCard_DFRobot:31: error: 'sd' was not declared in this scope
SDCard_DFRobot:31: error: 'SPI_HALF_SPEED' was not declared in this scope
SDCard_DFRobot:37: error: 'ofstream' was not declared in this scope
SDCard_DFRobot:37: error: expected `;' before 'sdout'
SDCard_DFRobot:38: error: 'sdout' was not declared in this scope
SDCard_DFRobot:38: error: 'sd' was not declared in this scope
SDCard_DFRobot:43: error: 'sdout' was not declared in this scope
SDCard_DFRobot:47: error: 'sdout' was not declared in this scope
SDCard_DFRobot:49: error: 'sd' was not declared in this scope
Starting out...I figured that I probably don't have the library properly installed. How do I properly "install" the library? How do I make sure that the code is seeing this library? I assumed that a simple #include would do the trick, but the following variations didn't work:
#include <SdFat.h>
#include <SdFat/SdFat.h>
#include "SdFat/SdFat.h"
I seems to me that since the SdFat.h file has a SdFat class, there shouldn't be a reason for SdFat to not be a valid type.
I have the sketch (.ino) file in the same directory as the directory containing the SdFat.h file. That is why I chose the SdFat/SdFat.h option.
I clearly am doing something wrong, does anyone have any clues how to help or pointers on methods to try?
Scott
2012-07-04 06:20:03 Ah yes, success...I downloaded the Arduino 0023 ([url=http://code.google.com/p/arduino/downloads/detail?name=arduino-0023.zip&can=2&q=]http://code.google.com/p/arduino/downloads/detail?name=arduino-0023.zip&can=2&q=[/url]) version and compiled my code, while making sure to install the library the same as I did for the newer Arduino 1.0.1 software. I ran a few tests and everything seems to be working quite well. Thank you.
Scott
wood6626
Scott

2012-07-03 18:20:58 Hi wood,
I'm not sure, but it might be that the Arduino 1.0 is not compatible with this version of the library.
You might want to try downloading version 0023 of the Arduino IDE. Install the library in the same manner and try to compile the sketch.
Some libraries have not been updated to be compatible with the latest Arduino IDE, so, it is a good idea to keep the older IDE on hand to test libraries.
Please let me know. Also, if I didn't explain my self clearly ask for clarification.
Hector
I'm not sure, but it might be that the Arduino 1.0 is not compatible with this version of the library.
You might want to try downloading version 0023 of the Arduino IDE. Install the library in the same manner and try to compile the sketch.
Some libraries have not been updated to be compatible with the latest Arduino IDE, so, it is a good idea to keep the older IDE on hand to test libraries.
Please let me know. Also, if I didn't explain my self clearly ask for clarification.

2012-07-03 17:19:56 Thank you for your reply. Adding the library in the manner you spoke of worked. Now...for the rest of the problems...
I went to compile the code and received a few other sets of errors:
[code]In file included from C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/SdStream.h:26,
from C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/SdFat.h:26,
from SDCard_DFRobot.cpp:9:
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/SdFile.h:315: error: conflicting return type specified for 'virtual void SdFile::write(uint8_t)'
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:48: error: overriding 'virtual size_t Print::write(uint8_t)'
In file included from C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/SdFat.h:27,
from SDCard_DFRobot.cpp:9:
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:40: error: expected `)' before '&' token
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:70: error: ISO C++ forbids declaration of 'Stream' with no type
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:70: error: expected ';' before '*' token
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h: In member function 'void ArduinoInStream::readline()':
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:50: error: 'hw_' was not declared in this scope
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:53: error: 'millis' was not declared in this scope
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:54: error: 'hw_' was not declared in this scope
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:61: error: 'hw_' was not declared in this scope[/code]
By looking at the errors listed, I see many errors that are related to the original, publicly accepted, code, not necessarily the sketch that I pulled from the DFRobot website. Since I am only a novice, I don't feel comfortable, nor do I see reason in, changing "working" code without expert advice.
Is there something that I am not seeing?
Attached, you will find a zip file with 1) my sketch, and 2) the library that I have downloaded, which I installed in C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries. I tested to make sure that the library was installed properly as you requested, and all seems to be working fine.
Thank you for your reply...
wood6626
I went to compile the code and received a few other sets of errors:
[code]In file included from C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/SdStream.h:26,
from C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/SdFat.h:26,
from SDCard_DFRobot.cpp:9:
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/SdFile.h:315: error: conflicting return type specified for 'virtual void SdFile::write(uint8_t)'
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:48: error: overriding 'virtual size_t Print::write(uint8_t)'
In file included from C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/SdFat.h:27,
from SDCard_DFRobot.cpp:9:
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:40: error: expected `)' before '&' token
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:70: error: ISO C++ forbids declaration of 'Stream' with no type
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:70: error: expected ';' before '*' token
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h: In member function 'void ArduinoInStream::readline()':
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:50: error: 'hw_' was not declared in this scope
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:53: error: 'millis' was not declared in this scope
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:54: error: 'hw_' was not declared in this scope
C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries\SdFat/ArduinoStream.h:61: error: 'hw_' was not declared in this scope[/code]
By looking at the errors listed, I see many errors that are related to the original, publicly accepted, code, not necessarily the sketch that I pulled from the DFRobot website. Since I am only a novice, I don't feel comfortable, nor do I see reason in, changing "working" code without expert advice.
Is there something that I am not seeing?
Attached, you will find a zip file with 1) my sketch, and 2) the library that I have downloaded, which I installed in C:\Program Files\Arduino\arduino-1.0.1-windows\arduino-1.0.1\libraries. I tested to make sure that the library was installed properly as you requested, and all seems to be working fine.
Thank you for your reply...

2012-07-03 00:22:38 Hi,
You need to close the Arduino IDE, then place the library in:
"Arduino-1.0/libraries/SDFat/"
The name of the directory should be EXACTLY the same as the library name without the extension.
once you have placed the library in the correct folder re-open the Arduino IDE go the "sketch" menu and go to "import library" you should be able to find the SDFat library in that list. That is how you know the library is being recognized by Arduino. If it does not appear in that list, then you have not properly installed the library.
Once the "include" line is in the code, you do not need to "import" the library again, this is just a way for you to verify if it was properly installed.
Hector
You need to close the Arduino IDE, then place the library in:
"Arduino-1.0/libraries/SDFat/"
The name of the directory should be EXACTLY the same as the library name without the extension.
once you have placed the library in the correct folder re-open the Arduino IDE go the "sketch" menu and go to "import library" you should be able to find the SDFat library in that list. That is how you know the library is being recognized by Arduino. If it does not appear in that list, then you have not properly installed the library.
Once the "include" line is in the code, you do not need to "import" the library again, this is just a way for you to verify if it was properly installed.
