SD card unable to initialize when unplugged from Computer

Hi ,
I am new to Arduino, and currently working on a project that requires reading from a lux sensor and storing readings to a SD card; meanwhile displaying reading to via Bluetooth. At this point I am having issue initializing the SD card. Everything seems to work properly whenever my Arduino board is connected to my PC through USB port (able to initialize SD card, reading from lux sensor, storing readings). However, if I try to use it without connecting to my PC, the SD card will not initialize, while everything else still work. I am wondering what the issue is causing this.

Some information regarding hardware I am using: Arduino Pro mini, TSL2591 lux sensor, HC-05 bluetooth module, 6 pin micro SD card module.

currently, bluetooth rx and tx is connected to pin 7,8 using software Serial. SD card module's CS, SCK, MOSI, MISO is connected to pin 10,13,12,11, respectively.

Below is the code I am currently working on

/*This program is intended for ME498 DIP Sensor*/

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>


/* Golbal Variable and Software Serial pin setup*/
File myFile; //SD card file
// set up Rx,Tx pins 
SoftwareSerial mySerial(8,7);

// SD Card Globle Variable 
unsigned long myTime; 
int count = 0;
const int chipSelect = 10;
const byte STRING_LENGTH = 8;
char input_date[STRING_LENGTH]; 
char fileName[80];

/* lux sensor set up*/
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // Sensor identifier 

/* Below is used to configuring TSL2591 lux sensor. comment or uncomment 
 *  lines for desired gain and integration time for the sensor 
 */
 
 void configureSensor(void)
{
  // You can change the gain on the fly, to adapt to brighter/dimmer light situations
  tsl.setGain(TSL2591_GAIN_LOW);    // 1x gain (bright light)
  //tsl.setGain(TSL2591_GAIN_MED);      // 25x gain
  //tsl.setGain(TSL2591_GAIN_HIGH);   // 428x gain
  //tsl.setGain(TSL2591_GAIN_MAX);   // 9K GAIN

  // Changing the integration time gives you a longer time over which to sense light
  // longer timelines are slower, but are good in very low light situtations!
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);  // shortest integration time (bright light)
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
  // tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
  tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS);  // longest integration time (dim light)

  /* Display the gain and integration time for reference sake */
  Serial.println(F("------------------------------------"));
  Serial.print  (F("Gain:         "));
  tsl2591Gain_t gain = tsl.getGain();
  switch (gain)
  {
    case TSL2591_GAIN_LOW:
      Serial.println(F("1x (Low)"));
      break;
    case TSL2591_GAIN_MED:
      Serial.println(F("25x (Medium)"));
      break;
    case TSL2591_GAIN_HIGH:
      Serial.println(F("428x (High)"));
      break;
    case TSL2591_GAIN_MAX:
      Serial.println(F("9876x (Max)"));
      break;
  }
  Serial.print  (F("Timing:       "));
  Serial.print((tsl.getTiming() + 1) * 100, DEC);
  Serial.println(F(" ms"));
  Serial.println(F("Date: Monday, *****")); 
  Serial.println(F("------------------------------------"));
  Serial.println(F(""));
}


/**************************************************************************/
/*
    Program entry point for the Arduino sketch
*/
/**************************************************************************/

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  mySerial.begin(9600);

  // Ask the user to input the current date//
   mySerial.print(F("Please insert current date dd/mm/yyyy"));
     while(!mySerial.available()) {
     // wait for input
  }
   if (mySerial.available()){
    delay(20);
    mySerial.println(input_date);  
  
   }
  mySerial.print(input_date);
  sprintf(fileName, "Data%s.txt",input_date[8]);


//setting up TSL2591 Sensor//
mySerial.println(F("Starting Adafruit TSL2591 Test!"));
  if (tsl.begin())
  {
    mySerial.println(F("Found a TSL2591 sensor"));
  }
  else
  {
    mySerial.println(F("No sensor found ... check your wiring?"));
    while (1);
  }

  /* Display some basic information on this sensor */
  //displaySensorDetails();

  /* Configure the sensor */
  configureSensor();

  // SD Card Setup
 while (!Serial) {
  
 }
  mySerial.print(F("Initializing SD card..."));
  if (!SD.begin(10)) {
    mySerial.println("initialization failed!");
    while (1);
  }
  
  mySerial.println("initialization done.");

  myFile = SD.open(fileName, FILE_WRITE);

  //SD Card file prints
  myFile.print("No.");
  myFile.print("\t");
  myFile.print("Time (s)");
  myFile.print("\t");
  myFile.println("Lux");
  myFile.println("Date:");
}

/**************************************************************************/
/*
    Show how to read IR and Full Spectrum at once and convert to lux
*/
/**************************************************************************/
float advancedRead(void)
{
  float out_advread;
  // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
  // That way you can do whatever math and comparisons you want!
  uint32_t lum = tsl.getFullLuminosity();
  uint16_t ir, full;
  ir = lum >> 16;
  full = lum & 0xFFFF;
  out_advread = tsl.calculateLux(full, ir);
  return out_advread;
}


void loop(void)
{
  float var;
  var = advancedRead();
  //simpleRead();
  // unifiedSensorAPIRead();

  // software Serial
   mySerial.println(var, 6);
   

  
  // SD Card command
  if (myFile) {
    myTime = millis()/1000;

    count = count + 1;

    myFile.print(count);
    myFile.print("\t");
    myFile.print(myTime);
    myFile.print("\t");
    myFile.println(var, 6);
    
    Serial.print(count);
    Serial.print("\t");
    Serial.print(myTime);
    Serial.print("\t");
    Serial.println(var, 6);
    myFile.close();
    myFile = SD.open(fileName, FILE_WRITE); // Change file number for every test run
  }
  delay(2000);
}


How do you know?

Do you get this message.

Do you get this message?

For a ProMini, you can leave that out. If you're using a ProMicro, that will block the code till you connect it to the PC. So are you using a ProMini or a ProMicro?

How are you powering this when not connected to the USB?

I am using a 3.7V lipo battery to power this.

Yes, I am getting the initialization failed message when I try to run the code without connecting it to my computer's USB port.
Also, I am using a Pro Mini.

Are you using a 3.3V (8MHz) or a 5V (16MHz) ProMini
On which pin do you power the ProMini from the battery?
There are all kinds of SD card modules; which one exactly do you use?

I am using a 3.3 V Pro Mini. My battery is connected to a Power boost and to Pin RAW on the Arduino. As for SD card module, I am using a "Mic ro SD Card Module Integrated Circuit Breakout Board Mini TF Card Memory Adapter Reader Module SPI Interface with chip Level Conversion".

Below I have included a circuit diagram of my project. (The SD card module is not included in this circuit diagram because it was added later on.)

Information on how the SD card module is connected to Arduino:
CS (ChipSelect) -to Pin 10
SCK- to Pin13
MOSI (Mater out Slave In)- to Pin12
MISO (Master out Slave out)- to Pin 11

Thank you

Link please.

You don't need a chip level converter; I'm not sure if it will cause problems in this scenario.

MOSI of the module must be connected to MOSI of the Arduino, MISO to MISO.

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