Project shuts down when SD card is in its slot??

Hi guys, Im having big trouble with my project. Its a multisensor that takes data and shows it on a lcd, plus saves it on a microSD card. My code seems to work, cause the project has worked ok for a while, and data is saved on The microsd card as expected. However, after a couple tries, the project (with the same code) shuts down right away after I turn it on. It only does so when I put the sd card in the sd module. When the module is empty, the project works every time. This made me think the SD card created a voltage drop and made the project shut down. So i connected the microSD module to another 5v source, but it doesn’t solve the problem at all. I also tried adding delay(1000); before the first file write the project does on the microSD, because the file saves this first file write (which is simply the name of my variables I’ll be monitoring: Temperature, etc) on the microSD before the system shuts down when I turn it on. No matter how long the delay(), it still shuts down instantly however.


//DHT libs
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
//water temp libs
#include <OneWire.h>
#include <DallasTemperature.h>
//LCD libs
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>  // F Malpartida's NewLiquidCrystal library
//SD libs
#include <SD.h>
#include <SPI.h>


//LCD variables
#define I2C_ADDR    0x3F  // Define I2C Address for the PCF8574T 
//Following are the PCF8574 pin assignments to LCD connections
// This are different than earlier/different I2C LCD displays
#define BACKLIGHT_PIN  3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
//variables pour allumer et fermer le LCD
#define  LED_OFF  1
#define  LED_ON  0
//LCD object
LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

//DS18B20 variables
int waterTempPin = 7;           //water temp sensor on pin 7
// Setup a oneWire instance                                                                                                                                                                                                           to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(waterTempPin);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
float waterSensorValue;
int waterError;

//DHT variables
#define dhtType           DHT11     //valeurs possibles: DHT11, DHT22 ou DHT21 
int dhtPin =  8;         // Pin which is connected to the DHT sensor.
DHT_Unified dht(dhtPin, dhtType);
float tempValue;
int tempError;
float RHValue;
int RHError;

//SD variables
int chipSelect = 4; //la pin 4, la pin CS du adapteur microSD
File mySensorData; //var for working with our file data

//light variables
int lightPin = A0; // select the input pin for LDR
float lightValue;

//interrupt variables
int buttonPin = 2;
int flag = 0;

//timer variables
int timeOn;
uint32_t delayMS;
int fileNb;

//other variables
int doneSampling; //compte le nombre de samples qui a fonctionné


void setup() {
  //save power

  //SD setup
  pinMode(10, OUTPUT); //cest la var pour work avec notre file, ne rien plugger dessus meme si elle est non utilisée
  SD.begin(chipSelect); //chipselect cest la pin 4, la pin CS du adapteur microSD
  // start mySensorData port

  //interrupt setup
  pinMode(buttonPin, INPUT_PULLUP);
  attachInterrupt(0, fonctionDuBouton, FALLING);

  //LCD setup
  lcd.begin (20, 4); // initialize the lcd
  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);

  //DS18B20 setup

  //DHT setup
  // Print dht11 temperature sensor details.
  sensor_t sensor;

  //Timer setup
  delayMS = sensor.min_delay / 1000;

  //SD: save column names
  mySensorData ="TRHS.txt", FILE_WRITE);
  mySensorData.println("File; Time of Measurement; Air Temperature; Air Humidity; Water Temperature; Light Percentage");


void loop() {
  if (flag == 1) {
    //Indicate measures are starting
    lcd.setCursor(2, 1);

    // Delay between measurements.

    // set the display
    lcd.backlight();  //Backlight ON

    //open SD file
    mySensorData ="TRHS.txt", FILE_WRITE);

    //0: set time of measurements
    timeOn = floor(millis() / 1000);
    mySensorData.print("; ");
    mySensorData.print("; ");

    //1: Get temperature event and print its value.
    tempError = 0;
    RHError = 0;

    sensors_event_t event;
    if (isnan(event.temperature)) {
      tempError = 1;
      mySensorData.print("; ");
    else {
      tempValue = event.temperature;
      mySensorData.print("; ");

    //2: Get humidity event and print its value.
    //lcd.setCursor(0, 1); //Start at character 0 on line 1
    if (isnan(event.relative_humidity)) {
      RHError = 1;
      mySensorData.print("; ");
    else {
      RHValue = event.relative_humidity;
      mySensorData.print("; ");

    //3: Get WATER temperature
    sensors.requestTemperatures(); // Send the command to get temperature readings
    mySensorData.print(sensors.getTempCByIndex(0)); //0 refers to the first IC on the wire
    mySensorData.print("; ");

    //4: Get light lvl
    lightValue = (1023 - analogRead(lightPin)) / 10.23;
    mySensorData.println(lightValue); //donne le voltage miex que div par 1023



    //How data on LCD-------------------------------------------------------------------------
    //Show DHT data
    lcd.setCursor(0, 0); //Start at character 0 on line 0
    if (tempError = 1) {
      lcd.println("Error T*");
    else {
      lcd.print("T*: ");
      lcd.print(" *C");
    if (RHError = 1) {
      lcd.println("Error RH");
    else {
      lcd.print("RH: ");
    //Show water T
    lcd.setCursor(0, 1); //Start at character 0 on line 1
    lcd.print("Water T*: ");
    lcd.print(" *C");

    //Show Light Sensor
    lcd.setCursor(0, 2); //Start at character 0 on line 2
    lcd.print("Light level: ");

    //Show time since ON (already calculated at begining of loop)
    lcd.setCursor(0, 3); //Start at character 0 on line 3
    lcd.print(" saved at ");
    lcd.print(" sec");

    lcd.noBacklight();  //Backlight OFF (save battery)

    flag = 0; //must be last to prevent overwriting
  // __asm__  __volatile__("sleep");//in line assembler to go to sleep


ISR(WDT_vect) {
  // This is called when the interrupt occurs, but I don't need to do anything in it

void fonctionDuBouton() {
  if (flag == 0 && millis() > 2000) {
    flag = 1;

void unusedAsOutput() {
  //Save Power by writing all Digital IO LOW - note that pins just need to be tied one way or another, do not damage devices!
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);

Maybe it’s a piece of hardware you’re supposed to use with the microSD module? my schematics is attached to this thread. Thanks!

Guys, i still havent found any solution. Someone might have an idea?

This made me think the SD card created a voltage drop and made the project shut down.

I think this is correct. SD modules are not famous for their low power consumption, and an alternative supply does nothing for the signal lines. My Etherten has on-board SD and the regulaors got noticably hotter when I started to use it. If your project worked before and doesn't now, something has changed, and it's not the code. So what is it? Power supply maybe?

Have you tried more than one memory card in there? - I had one go bad on me lately and it seems to have failed short circuit as it gets very hot in whatever device I plug it into. Just a thought.