Go Down

Topic: Programming with SD Library (Read 688 times) previous topic - next topic



I am trying to implement the Arduino Nano (with ATmega 328) in one of my projects. For this, I have attached an FSR Force Sensing Resistor to the Arduino. I currently have a program which outputs a value to the serial monitor corresponding to the force that I push with.

Now, I am trying to get the data to write to a micro SD card..

Here is my code:
Code: [Select]
  SD card datalogger

This example shows how to log data from three analog sensors
to an SD card using the SD library.

The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created  24 Nov 2010
updated 2 Dec 2010
by Tom Igoe

This example code is in the public domain.


#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;
int counter;

void setup()
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");
  counter = 0;

void loop()
  // make a string for assembling the data to log:
  String dataString = "";
  counter = counter + 1;

  //read sensor value and output to serial monitor
  int analogPin = 0;
  int sensor = analogRead(analogPin);
  Serial.print("Analog reading = ");
  //store sensor value in dataStr
  dataString = (String)counter + "," + (String)sensor + "\n";
  // read three sensors and append to the string:
  //for (int analogPin = 0; analogPin < 3; analogPin++) {
    //int sensor = analogRead(analogPin);
    //dataString += String(sensor);
    //if (analogPin < 2) {
      //dataString += ",";

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog2.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    // print to the serial port too:
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog2.txt");

After running the CardInfo demo, I verified that the wiring was correct. This program/set-up works for the most part, but I'm having two minor issues..

1) Randomly while running, the "Initializing SD card..." message will show up in the serial monitor and the counter will reset... This indicates to me that setup() is being run again, but I don't understand why.. Any ideas?

Analog reading = 1010

Analog readiInitializing SD card...card initialized.
Analog reading = 1013

Analog reading = 0

2) Randomly while running, the serial monitor will hang up and stop outputting to the screen. Any ideas why this may be caused?

Also, I would like to have another counter for the number of times setup() is run, so that I can store separate data files for each time this happens. Any ideas on how to implement that?



Code: [Select]
  dataString = (String)counter + "," + (String)sensor + "\n";
The SD library uses a LOT of SRAM (of necessity). The String class uses a lot of SRAM, too, and it is NOT necessary. Learn how to use char arrays and the string functions, instead. sprintf() might be useful, too.

Casting an int to a String is not a useful thing to do.

This indicates to me that setup() is being run again, but I don't understand why.. Any ideas?

When you run out of memory, bad things happen.
The art of getting good answers lies in asking good questions.


Sounds like the board sometimes resets itself.
A decoupling capacitor (100nF) between Vcc and Gnd may help.

Go Up