Go Down

Topic: SD card read/write with Arduino (Read 213644 times) previous topic - next topic


hi friends i am doing an data logger project i'am using the below code... when i interact with harware i could'nt inizilaise the card this is ma error could any one help me out....

#include <Spi.h>
#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>

int print_disk_info();
int sample();
int readDisk();

byte incomingByte;
void printWelcome();
long int address;
byte tempBytes[2];

void setup()


    Serial.println("MMC/SD initialization failed");

void loop()
   int i;


          case 114:
          case 115:

int sample()
   int i,j;
   int temp;
   byte low;
   byte high;
   byte inByte;

           if(inByte==113) return 0;
       Serial.print(" ");

       //Convert int to 2 bytes
      // Serial.print(temp,DEC);


         Serial.print("Write error");



   return 1;

int readDisk()
   byte low;
   byte high;
   byte info[2];
   int i;
   int  result;

       //Serial.print(" ");
       Serial.print(" ");
       Serial.print(" ");


void printWelcome()
   Serial.println("Data sampling system");
   Serial.println("send r to read disk");
   Serial.println("send s to start sampling");
   Serial.println("send q to stop sampling");

int print_disk_info()

   struct sd_raw_info disk_info;
       return 0;

   Serial.print("rev:    ");
   Serial.print("serial: 0x");
   Serial.print("date:   ");
   Serial.print("size:   ");
   Serial.print("copy:   ");
   Serial.print("wr.pr.: ");
   Serial.print("format: ");
   Serial.print("free:   ");

   return 1;


Hi all, I was tryning without success to install a Libelium Sd Module on a Arduino Mega (ATMEGA1280) :'(. I've searched on the net for some information; everyone say that this module doesn't work with Arduino Mega and nobody has a solution :o.
So anyone tried to use this circuit and this library with Arduino Mega?
Thank you for help.


Mar 27, 2010, 01:14 am Last Edit: Mar 27, 2010, 01:36 am by cadrogui Reason: 1
anyone make the pcb of this???



if anyone want the PCB file, send me a PM, its make on eagle software



Hello All!
I'm am new to the Sd card phase. My question is which of the libraries and setups do you recommend for logging gps data strings to an sd card. Yes I know about Ladyadas shield and code but I am overwhelmed. I bought the sparkfun arduino sd shield without realizing it was a failed product. Through the forums I found how to wire it up and got the sparkfun code working but I'm still having small issues. So i'm coming here to find out where i should start.



I am looking for a library that solves my needs or information on how to do it.  I have a project that will use asynclab's wishield and a microsd card.  All I need to do is read files from an SD card.  There can be any number of files on the SD card so I think uFAT is out.  I can do what I want with sdlaftlib after I make my own read function.  I need to be able to read a byte at a specific position in a file.  The files will be created on a PC.

When I include both the wishield and sdfatlib they don't leave much memory left for the program.  In fact, right now my program is 1.3k over the limit of a 368 arduino.  I went through the sdfatlib and took out everything that deals with writing to files as I don't need that.  I also went through my code and made sure I didn't allocate memory when I didn't need to.  I got it down to 1k over the limit.

Since I just need to read from a microsd I was wondering if there is a library that takes up a smaller footprint that will do that.


I was not sucessfull in write/read SD Card using this schematics and library. Could someone give a help? I'm using an Kingston 1GB SD Card, and an Freeduino v1.0.  I had connected the SD Card like this picture, but I was not sucessful.


someone might use it? (the circuit and the lib)



Just to say thanks. I got a seeeduino a couple of days ago, tried using a card holder hacked from a broken camera and couldn't get the DevicePrintDemo to work (mmc::initialize() returned 3). All the connections tested ok with a multimeter on beep but I noticed a (2k) short on one of the connections. So I took the advice elsewhere in this or parallel thread and soldered wires directly to a micro-adapter, set the + to 3v3 and it just worked - no resistors needed!!

The adapter seemed to have pin 3 connected to pin 6 internally so that saved one of the connections.


  as soon as I try and run this example I receive the following error:
My Documents\arduino-0018\libraries\SDcard\arduino sd card example.cpp:161: error: 'DEC' was not declared in this scope

This makes no sense to me as "DEC" seems to be used in the correct way throughout the example. Can anyone help?

Many thanks!

NOTE: I'm using Arduino_0018 and the "libraries" folder is not within the "hardware" folder, does this matter?


Hi to all,

This is N.Nandhakumar again. After a lot of struggle I have purchased the EM-406a GPS module, Adafruit GPS shield V1.1, Freeduino ATMega328 for my project.
I soldered the GPS shield , interfaced it with the Arduino and the GPS logged data successfully into the SD card.
Now, I have a problem. I want to read back the logged data from the log file stored in SD card for doing some processing, comparison with the data. I made use of SdFat library's SdFatRead example code and I was able to read back the file contents .

Code: [Select]
* This sketch reads and prints the file
* PRINT00.TXT created by SdFatPrint.pde or
* WRITE00.TXT created by SdFatWrite.pde
#include <SdFat.h>
#include <SdFatUtil.h>

Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char *str)
 PgmPrint("error: ");
 if (card.errorCode()) {
   PgmPrint("SD error: ");
   Serial.print(card.errorCode(), HEX);
   Serial.println(card.errorData(), HEX);
void setup(void)
 Serial.println("type any character to start");
 while (!Serial.available());
 // initialize the SD card
 if (!card.init()) error("card.init");
 // initialize a FAT volume
 if (!volume.init(card)) error("volume.init");
 // open the root directory
 if (!root.openRoot(volume)) error("openRoot");
 // open a file
 if (file.open(root, "PRINT00.TXT", O_READ)) {
   Serial.println("Opened PRINT00.TXT");
 else if (file.open(root, "WRITE00.TXT", O_READ)) {
   Serial.println("Opened WRITE00.TXT");    
 // copy file to serial port
 int16_t n;
 uint8_t buf[7];// nothing special about 7, just a lucky number.
 while ((n = file.read(buf, sizeof(buf))) > 0) {
   for (uint8_t i = 0; i < n; i++) Serial.print(buf[i]);
 /* easier way
 int16_t c;
 while ((c = file.read()) > 0) Serial.print((char)c);

void loop(void) {}

The problem is that the file contents are read in raw format using uint8_t [ I googled about this datatype . But I am not clear with this datatype and how it works  :( ] . It will be more useful If I could read the data from the file in char or string format .

I searched for a better way to convert the uint8_t data to char datatype. But I get only junk characters.

I need the forum's help to solve this issue. Please give your suggestions on this.
Please help me out. Waiting for your valuable suggestions.

My works on the project at : http://walkingwithtux.wordpress.com/2010/05/02/tran-duino-under-progress/


N.Nandhakumar  :)



Do you think you could post here what is the content of the file you are reading and what you are seeing in the serial window? I'm not sure if I can help you out, but I think that information would be helpful.

Good luck!


Hi cemath,

Thanks for the reply. THe code which I edited in the SdFatRead Example is

Code: [Select]
 int16_t n;
 char msg;
 uint8_t buf[7];// nothing special about 7, just a lucky number.
 while ((n = file.read(buf, sizeof(buf))) > 0)
   for(uint8_t i=0;i < n;i++)
      msg = (char)buf[i];

I am trying to read the file contents , convert them to char type from uint8_t type.

The file which I am trying to read from SD card is the GPSLOG00.TXT . the file contains GPS sentences logged into a text file from GPS.
The contents are

Code: [Select]


When I uploaded the code , The serial monitor contents are,
Code: [Select]




The contents in the serial monitor are of different format [ I think its of HEX type]
and the full contents are also not fully converted and displayed in the serial monitor. Please help me out and tell me where I made wrong in the code. I also need to convert the full contents into char, since the log file is very huge.

Thanks in advance


Hi Nnk,

I found a pattern while comparing the data in your file and the data that is displayed in the monitor window. If you break the file data in blocks of 7 characters (the last caracter of a line, break line, counts as a caracter), and you keep only the last caracter of each block, then you can reconstruct what you are seeing in the monitor window. The problem must be in the example code you use to display the caracters, but I do not see it. I suspect that by reading a single byte at a time (using uint8_t buf[1] instead of uint8_t buf[7]) it should correct the problem, but that doesnt not explain it.

I suggest that you debug the code step by step by displaying the value of 'n' and other variables to verify that the program is doing what it should.

Also, I think that going throught the 'char' intermediate step is not necessary, because the formats 'uint8_t' and 'char' are very similar. I think uint8_t is equivalent to the format 'byte', which is 8 bits unsigned, and char is 8 bits signed. But since the caracters in ASCII are encoded on a range from 0 to 127, signed or unsigned bytes make no difference.

I hope that you find the solution to your problem quickly!

Good luck,



May 19, 2010, 03:59 am Last Edit: May 19, 2010, 04:13 am by max88poon Reason: 1
Hi guys

i am in a serious situation, i can't graduate if i don't get done with the datalogging...
so please, help , i very appreciate it
here is the case:
i am having a project that display a speed and record it
the former part is done, however, the later part is very difficult to me
i am trying to save the speed into the SDcard, don't need to read or copy, just write.
here is the code for the former part:
Code: [Select]
#define FREQ_PIN 3
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
 pinMode(FREQ_PIN, INPUT);
lcd.begin(16, 2);

void loop()

unsigned long t1 = pulseIn(FREQ_PIN, HIGH);// Time how long it takes to go HIGH again
unsigned long t2 = pulseIn(FREQ_PIN, LOW); // and how long it takes to go low again.
double t = t1 + t2;
        double f = 1000000.0/t;
          double w = 2 * 3.14 * f;
          double v = w * 0.05;

Help please, I very very appreciate it.

i have the exactly the same circuit with this post, but i don't know how to write, thanks a lot

The code above is created by members, they are Pauls, Groove, AWOL and mem, i couldn't do this without their help, thanks a lot  :)

Go Up