Go Down

Topic: SD logging changes on battery power (Read 814 times) previous topic - next topic

apanders

Nov 18, 2013, 09:49 pm Last Edit: Nov 18, 2013, 09:53 pm by apanders Reason: 1
Hi all!  Thank you in advance for your help.

I am using an Arduino Micro to log data from 12 analog sensors to a micro SD card. When tethered to the computer over USB, the data both prints to the serial monitor and to the SD card fine. I have a forced delay of 10ms, so it logs approximately every 30ms. However, as soon as I plug in the battery and unplug the computer, the data only writes to the card every 10-13 seconds or so. Initially I used an alkaline 9V. My set up draws about 150mA, which should be fine for that type of battery.  Suspecting power issues, I also tried a lithium 9V and 6 AA batteries, but it made no difference.  I then suspected timing issues with writing to the card, so I played around with implementing different delays both at the end of the code and before I print my data string to the card. This also didn't really have an effect. Finally, I tried to print directly to the card after reading each sensor, rather than forming a data string. This changed my output but didn't solve the problem. Essentially, because I was opening and closing the connection to the SD card 12 times, while tethered the card wrote every 200ms. Once on battery power, that immediately jumped to every 2 seconds. The over all effect isn't as bad as before, but there is still an unexplained jump in time between logs while on battery power.

Here is the code when I form the data string
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
modified 9 Apr 2012
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 = 4;

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("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:
   return;
 }
 Serial.println("card initialized.");
}

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

 // read three sensors and append to the string:
 for (int analogPin = 0; analogPin < 12; analogPin++) {
   int sensor = analogRead(analogPin);
   dataString += String(sensor);
   if (analogPin < 12) {
     dataString += ",";
   }
 }
 unsigned long time;
 time = millis();
 dataString += time;

 // 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("datalog.txt", FILE_WRITE);

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





and here is my implementation of writing to the SD card each read, rather than forming a data string:
Code: [Select]
#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 = 4;

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("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:
   return;
 }
 Serial.println("card initialized.");
}

void loop()
{
 // read sensors and append to the string:
 for (int analogPin = 0; analogPin < 12; analogPin++) {
   int sensor = analogRead(analogPin);
   if (analogPin < 12) {
      File dataFile = SD.open("datalog.txt", FILE_WRITE);
      dataFile.print(sensor);
      dataFile.print(",");
      dataFile.close();
   }
 }
 unsigned long time;
 time = millis();

 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 dataFile.println(time);
 dataFile.close();

   // print to the serial port too:
   Serial.println(time);

 delay(50);
}


For my application, I'd like to log data very quickly, on the order of every 30ms. Any advice you can give as to why I might be having this problem would be greatly appreciated!

Go Up