Writing to SD card, 0 bytes written

Hello!

I am helping my daughter with a high school project. She has to connect a sensor to athletes finger and take readings while they go through their workouts. She and I put together an Arduino UNO based project, powered by a 9V standard alkaline battery. Long story short, with a micro-SD card reader capturing sensor data, the device did not work. So I am discovering fun things about Arduino UNO linear power regulators and such!

After some discussion on the storage forum, I settled on getting a 6xaa battery holder to power the UNO through its barrel jack. I could use alkaline batteries (1.5x6=9 V), but I was wondering if rechargeable NiMH batteries would not be a better choice (1.2x6=7.2 V). No way of knowing how quickly the alkalines will run out of juice. Would appreciate feedback on that from the experts here who might have used this arrangement. We are aiming for the device to last 1.5 hours in the field.

Short answer, yes, and if you do things carefully you can use an analog input(with appropriate resistor attenuation network!) to monitor the battery voltage so it can tell you when the battery is getting low.

Hello camsysca,

That's a lovely idea. If I understand correctly, you are suggesting to take "some" signal from the UNO - maybe the 5 V power, drive it down a resistor network to some safe voltage, take the signal to an analog input and monitor the voltage level on that from code in Arduino. Right? That sounds simple enough - in principle.

vijaykam

Please do not cross post. Continue your previous thread on this topic.

That's a crappy powering. 4/9 of the energy will be lost as heating and the run time will be short, if it even works at all. Better powering please.

@vijaykam ,

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

The one I used in a fan control project required ~60mA to stay active. I turned on a 60mA load through a transistor when the fans were turned off to keep it active. This was an easy solution for me. I plugged the battery bank into the USB connector and pulled the 60mA off the +5V pin.
image

vijaykam, we are talking about something like this:
https://www.amazon.com/Anker-PowerCore-Ultra-Compact-High-Speed-Technology/dp/B01CU1EC6Y?source=ps-sl-shoppingads-lpcontext&ref_=fplfs&psc=1&smid=A294P4X9EWVXLJ

Hello 2112,

Thanks for the suggestion - appreciate it. Very clever solution!

For now we are using a simple solution suggested by @madmark2150 - to gang up 6xaa batteries. My daughter got her first good run of over 1 hour of data.

I am very grateful for all the help and suggestions from expert members of this forum. Hopefully we are on to the science part of the project from here on.

Cheers!
vijaykam

Hello,

Just updating on the status. We are still not out of the woods it seems. The 6xAA battery power supply onto the barrel jack appears to work - maybe for an hour. This part is puzzling. We have successful data collections and failed data collections, when running past about 1 hour. Under 1 hour, it seems to work reliably. Also, we broke up the data into multiple files. Each file collects data for 30 min - about 180K (in test.txt). Subsequent data collections update files test1.txt, test2.txt etc - once every 30 min. In the successful case, we can match up start and finish timestamps on each file. But in the failed case, we just see test.txt with 0 bytes in it. None of the other files are there. All the failed cases have been runs of 1.5 hours plus. We tried 6xAA alkalines as well as 6xAA rechargeables.
I am going to take this setup to work to measure current draw, but not sure what we are going to do.

#include <SD.h>
#include <SPI.h>
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;  // the number of the pushbutton pin
const int ledPin = 8;  // the number of the LED pin
const long interval = 1000;  // interval at which to blink (milliseconds)const int buttonPin = 2;  // the number of the pushbutton pin
const long fileInterval = 1800000;
// variables will change:
bool startProcess = false;
int ledState = LOW;  // ledState used to set the LED
unsigned long previousMillis = 0;  // will store last time LED was updated
int buttonState = 0;  // variable for reading the pushbutton status
File myFile;
const int GSR=A0;
int sensorValue=0;
int gsr_average=0;
int counter = 0;
String fileName = "";
unsigned long prevFileSwitch = 0;

void setup() {
  //Serial.begin(9600);
  //while (!Serial) {
    //;
  //}   
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  if (!SD.begin(4)) {
    digitalWrite(ledPin, HIGH);
    while (1); 
  }
}

void loop() {
  if (!startProcess && buttonPressAndRelease())
  {
    startProcess = true; 
    //Serial.println("Serial started");
    digitalWrite(ledPin, LOW);
    myFile = SD.open("test.txt", FILE_WRITE);
    if (!myFile) {
      digitalWrite(ledPin, HIGH);
      while (1);
    }  
    unsigned long mi = millis();
    myFile.print("Start: ");
    myFile.println(mi);   
  }
  else if (startProcess && buttonPressAndRelease())
  {
    startProcess = false;
    unsigned long mi = millis();
    myFile.print("Finish: ");
    myFile.println(mi);
    digitalWrite(ledPin, LOW);
    myFile.flush();
    myFile.close();
    exit(0);
  }
  if (startProcess)
  {
    toggleLED();
    switchFile();
    int data = gsrGetData();
    myFile.println(data);
  }
}

bool buttonPressAndRelease(){
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // Confirm button is pressed - debounce
    delay(50);
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      // Confirmed - button pressed - now wait for release
      while ((buttonState = digitalRead(buttonPin)) == HIGH) {
        delay(50);
      }
      // For now, not debouncing on release
      return true;
    }
    else {
      // Glitch - return - will catch it again
      return false;
    }
    // Should not come here
    return false;
  } 
  else {
    // Not pressed
    return false;
  }
}

void toggleLED(){ //method for blinking the LED
  if (millis() - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = millis();

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      //Serial.println("hello from toggleled");
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

void switchFile()
{

  if (millis() - prevFileSwitch >= fileInterval)
    {
      prevFileSwitch = millis();
      counter = counter + 1;
      String myString = String(counter);
      fileName = "test" + myString + ".txt";
      unsigned long mi = millis();
      myFile.print("Finish: ");
      myFile.println(mi);
      myFile.close();
      myFile = SD.open(fileName, FILE_WRITE);
      //Serial.print("New File Created:");
      //Serial.println(fileName);
      mi = millis();
      myFile.print("Start: ");
      myFile.println(mi);
    }
}

int gsrGetData()
{
  long sum=0;
  for(int i=0;i<10;i++)           //Average the 10 measurements to remove the glitch
  {
    sensorValue=analogRead(GSR);
    sum += sensorValue;
    delay(5);
  }
  gsr_average = sum/10;
  return gsr_average;
}

As a test, run it off a wall power supply, see if it runs.
The problem is that the file isn't closed after each update. When the power goes down, the files are open, and therefore truncated as the length is unknown.
reopen & close after each update, OR watch for power fail (or falling) and update & close before the power fully dies.

Hello madmark2150,

Thanks for your reply. The puzzling thing in this code is, we are closing files every 30 min and creating new ones. So after 1.5 hours I expect to see test.txt, test1.txt, test2.txt and a small test3.txt. I ran at home and it does work this way. Out in the field, I just see test.txt with zero bytes. Anecdotally, running for an hour or less appears to work in the field and somewhere between 1-1.5 hours, it misbehaves.

I brought the device to work, to see how much current SD card reader is drawing and how much at batteries.

vijaykam

Are all connections to your push button secured by solder and/or screw terminals?

Hello Paul_KD7HB,

It is a starter kit push button on a breadboard. The whole circuit consists of a push button and a toggle LED.

Regards,

vijaykam

Then I will repeat the question. Are all the push button connections secure? Test with your Ohmmeter.

Thanks Paul_KD7HB, will check.

vijaykam

Hello,

Running ammeter tests, here is what I found:

  1. With just SD.begin(CS), the Hiletgo microSD card starts drawing 20 mA
  2. During normal operation, while writing to the micro-SD card, the current draw is between 1.75 mA to 3.94 mA - presumably while writing it is 3.94 mA
  3. At the 9V battery source (6xAA alkaline), the current draw is 65 mA when SD.begin() has been called, but not actively writing
  4. During normal operation, the current draw at 9V battery is 45 mA (20 mA down to 1.75 mA on the SD card reader accounts for this).

For AA batteries, the discharge curve at 100 mA is
AA-100mA

At 1.4 to 1.6 amphrs, AA cell gang will discharge to about 7 V, which I believe is the minimal acceptable voltage for the regulator? Is this potentially the issue? At around 1.4 to 1.6 hours, the regulator might start dropping voltage and cause microSD card to malfunction?

vijaykam

... further update. I ran the device for over 2 hours and monitored Arduino 5V supply - it remained at 4.987 V. I don't believe this is a power problem at this point.

One question - should I be doing an SD.end() after I am done collecting data and flushing/closing the file? In the other - more complicated - SD write example code, SD.end() is never called, so wondering.

vijaykam

Hi I didn't looked through all text above. But maybe this helps because it works on me: sometimes the close() method doesn't work properly on SD cards with large sizes... Similar problem happened to me when I was using a 32GB card, and I debugged for a long while but couldn't solve it. But the problem immediately vanished when I turned to a 8GB one :slight_smile:

This is NOT something you want to "take into the field".

Breadboards are for temporary experiments, and are inherently unreliable. When you are happy with your circuit, one much better approach is to reassemble it on something like one of these protoboards, soldering all wires and components to the PCB.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.