SD card stops writing when power is disconnected and reconnected

The purpose of this project is to log data from a gyro/accelerometer module and gyro module simultaneously and save the data to a micro SD card. I am using an arduino pro micro, and have wired everything up and have functional code when the arduino is plugged into my computer. However, after I disconnect the arduino from the computer and then reconnect it to the computer, the SD card receives no new data. This also happens when connecting to a 9v battery through the Micro usb port. The end goal is to have this project run purely on battery power, completely disconnected from the computer. I'm unsure whether this is a code or hardware issue, and I would appreciate any thoughts.


#include <Wire.h>

#include <SPI.h>
#include <SD.h>

#include <SoftwareSerial.h> 
#include <TinyGPS++.h> 


File f;
// The TinyGPS++ object
TinyGPSPlus gps;

static const int TXPin = 9, RXPin = 5;
static const uint32_t GPSBaud = 9600;

SoftwareSerial ss(TXPin, RXPin);

String data;

const int MPU_addr=0x68;  // I2C address of the MPU-6050
double AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

String filename;
char filnum = 'a';


void setup() {
 
  // put your main code here, to run repeatedly:
  Serial.begin(9600);
  
  Serial.println("");
  delay(100);

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);

  ss.begin(GPSBaud);

  filename = "RIDE"+String(filnum)+".txt";
  
  while(SD.exists(filename)){
    filnum += 1;
    filename = "RIDE"+String(filnum)+".txt";
  }
  Serial.println("Writing to"+filename);
  Serial.println(gps.date.month());
}



void SDwrite(String file, String text){

  Serial.begin(9600);
  f = SD.open(file, FILE_WRITE);
  if (f) {
    Serial.print("Writing to " + file + "...");
    f.println(text);
    // close the file:
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening " + file);
  }
  f.close();
}


void loop() {
  if (SD.begin(4)){
    Serial.begin(9600);
    ss.begin(GPSBaud);
    // put your main code here, to run repeatedly:
    Wire.beginTransmission(MPU_addr);
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
    AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
    AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
    AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
    Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
    GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
    GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
    GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L) 
  
  
    while (ss.available() > 0)
      gps.encode(ss.read());
    
    data = String(AcX)+","+String(AcY)+","+String(AcZ)+","+String(GyX)+","+String(GyY)+","+String(GyZ)+","+String(gps.location.lat())+","+String(gps.location.lng())+","+String(gps.speed.mph())+","+String(gps.time.hour())+","+String(gps.time.minute());
    Serial.println(data);
    SDwrite(filename, data);
  }else{
    while(!SD.begin(4)){
      Serial.println("SD card Removed");
    }
    filnum += 1;
    filename = "RIDE"+String(filnum)+".txt";
  }
}

I know this does not address your problem but.

If you are using a 9V battery like this.


Expect short run times and many issues involving low power.

Post an image of your project. Post a schematic.

What does that mean?

It means that everything works as expected after uploading code from the computer. However, if I unplug the arduino from the computer and plug it in again, no data is logged. Furthermore, if its unplugged from the computer and then plugged into a battery, it once again does not work.

Here is a photo, schematic is incoming soon

After you unplug the board, close the serial monitor, plug the board back in, open the serial monitor does it print now?

Nothing is showing on the serial monitor after taking those steps.

Quite odd indeed.

Which IDE version are you using?

Which operating system? Windows 10 , Mac, Linux, etc

I'm using windows 10, IDE version 1.8.13

Schematic

I have been able to come up with only one possible explanation for this seemingly impossible situation.

The controller in SD cards can take some time to boot up and be ready to communicate. If your sketch tries to intialize the card too quickly after power up, it may fail, and then your sketch may get stuck. This would not happen if you are flashing firmware because the card would have plenty of time to boot up while the uploading is going on.

I think a way to test this is to see what happens when you hit the reset button. If things work correctly after hitting reset, that would support this as the explanation. Or you could build a one-second delay after boot in your code before attempting to initialize the card.

SD card modules do NOT have a hardware reset line. The software reset sent after an update does NOT reset the SD card. Both the CPU and the SD module should be power cycled to ensure correct restart after firmware updates.
Additional weirdness happens if the CPU is reset or power cycled and the SD card is not.
The SD cards can have over 100mA draw, causing a 9V cell to sag to the point the card won't function. If you use 9V, make it from 6xAA cells and not the 9V cells warned about above.
The 32gb limit and DOS 8.3 filename requirements appear as being met since you said it does write files.
I had gone so far as to think of adding a relay to the SD card's Vcc line so the CPU could physically power cycle the card on a fail of sd.begin()

@spoontoobig This also happens when connecting to a 9v battery through the Micro usb port.
That could explain a lot

I realize there are possible issues with the battery option, but he said that if he flashes firmware everything works, but if he then disconnects from USB and reconnects to USB, it doesn't work anymore. Is that right, or did I misunderstand?

If that's right, it still seems to me that the SD card, or perhaps one of the other devices, may not have had time to properly boot up when the software attempts to initialize it. But it's a simple test to build in a one-second delay at the very beginning of his code before he attempts to do anything. If that doesn't fix the problem, then my suggestion is wrong, and it must be some other problem.

I was hoping we could get a report on this problem - fixed, and if so, how?

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