SD card file won't open a second time

I recently tried to store data from a BMP280 sensor to an SD card but I seem to keep running into the same issue. My SD card initializes with(chipSelect is 10):

if (!SD.begin(chipSelect))
  {
    Serial.println("SD Card Init Failed. Womp Womp D:.");

    
    while(1);
  }

It also manages to open a file:

dataFile = SD.open("data.txt", FILE_WRITE);

  if (dataFile)
  {
    dataFile.println("File Opened:");
    Serial.println("SD card accessed(the file)!");
  }
  else
  {
    Serial.println("Error opening the sd card file :/");
  }
  dataFile.close();
  Serial.println("Init SD card closed.");

Although, when I get to my loop function in order to open a file again(to store sensor data), the file fails to open:

 dataFile = SD.open("data.txt", FILE_WRITE); // Opens the file
  //Checks if file has opened
  if (dataFile)
  {
    Serial.println("File has been opened sucessfully :D.");
    //Write to file:
    dataFile.println("---------------------------------------------------------------\n");
    //Temperature
    dataFile.print("Temperature = ");
    dataFile.print(temperature);
    dataFile.println(" C");
    //Pressure
    dataFile.print("Pressure = ");
    dataFile.print(pressure);
    dataFile.println(" hPa");
    dataFile.close(); //CLOSES THE FILEPATH
    Serial.println("Closed File(loop)");
    
  }
  else
  {
    Serial.println("Something is off... I can smell it in the air.");
  }

What could this be? I am using a WaveShare microSD card, and an Arduino Uno.

Here is my full sketch(as well as wiring at the end):

  
//BMP280 Code
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

//SD Card Reader
#include <SPI.h>
#include <SD.h>

//BMP 280
Adafruit_BMP280 bmp;
//SD card readed
const int chipSelect = 10; //Pin num connected to CS
File dataFile;


//Buzzer
const int buzzerPin = 9;


void setup() {
  Serial.begin(9600);
  
  
  while (!Serial);
  Serial.println("Serial began");
  
  //Buzzer
  pinMode(buzzerPin, OUTPUT);

  //BMP280
  
  if (!bmp.begin(0x76)) {
    
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    //while (1);
  }
 
  
  
  //SD card reader

  //pinMode(chipSelect, OUTPUT);
  //SD card init
  if (!SD.begin(chipSelect))
  {
    Serial.println("SD Card Init Failed. Womp Womp D:.");

    
    while(1);
  }
  Serial.println("SD card initialized YAYAYAYYAYAYAYA :D");
  //OPEN FIILLEEEE
  dataFile = SD.open("data.txt", FILE_WRITE);

  if (dataFile)
  {
    dataFile.println("File Opened:");
    Serial.println("SD card accessed(the file)!");
  }
  else
  {
    Serial.println("Error opening the sd card file :/");
  }
  dataFile.close();
  Serial.println("Init SD card closed.");
  
}


void loop() {
  
  
  //BMP280 start
  Serial.println("BMP280 Sensor Readings:");


  float temperature = bmp.readTemperature();
  Serial.print("Temperature = ");
  Serial.print(temperature);
  Serial.println(" °C");

  
  float pressure = bmp.readPressure() / 100.0F; // hPa
  Serial.print("Pressure = ");
  Serial.print(pressure);
  Serial.println(" hPa");


  Serial.println();
  //BMP280 END
  //SD Card Reader
  dataFile = SD.open("data.txt", FILE_WRITE); // Opens the file
  //Checks if file has opened
  if (dataFile)
  {
    Serial.println("File has been opened sucessfully :D.");
    //Write to file:
    dataFile.println("---------------------------------------------------------------\n");
    //Temperature
    dataFile.print("Temperature = ");
    dataFile.print(temperature);
    dataFile.println(" C");
    //Pressure
    dataFile.print("Pressure = ");
    dataFile.print(pressure);
    dataFile.println(" hPa");
    dataFile.close(); //CLOSES THE FILEPATH
    Serial.println("Closed File(loop)");
    
  }
  else
  {
    Serial.println("Something is off... I can smell it in the air.");
  }
  
  //Buzzer sounds every 5 seconds
  //tone(buzzerPin, 1000);
  //delay(1000);
  //noTone(buzzerPin);
  delay(5000);
  
}


(The circuit also has a BMP280 and a buzzer)
Is there something wrong with the wiring?

Update: I haven't changed anything, the SD card won't even initialize anymore.

SD cards are 3.3V devices, while the Uno R3 runs at 5V. Check whether the SD module has the required logic level shifters and is guaranteed to tolerate 5V I/O. Otherwise it will malfunction or be damaged.

Also, the SD card allocates a 512 byte buffer and you may be running out of SRAM memory.
To save SRAM, change all .print() statements like this one

    Serial.println("Closed File(loop)");

to use the "F macro", as follows. This forces the text to be stored in program memory.

    Serial.println( F("Closed File(loop)") );

I am aware that SD cards are 3.3V devices. The device is plugged into the 3.3v pin instead of the 5v pin. That was a problem I saw somewhere but I wasn't sure, thanks for the tip! I'll try it out!
Does this have anything to do with SRAM?

The I/O is 5V, and that is what kills a 3.3V chip, if not the Arduino as well.

You may not connect 5V I/O pins to 3.3V I/O pins, without using a level shifter.

The chip is connected to the 3.3v pin of the arduino though, and the chip still works, it would initialize.

Please read post #5, as many times as it takes, for the point to get across.

Sorry, I am a little new to this. The Arduino has a pin called 5V, and another called 3.3V. Doesn't the 3.3V work?

Did you not notice that there are four data and control pins (CS, MISO, MOSI, SCLK) connected between the Arduino and the SD card module?

On the Arduino side, those pins put out 5V. The SD card module can only accept 3.3V on those connections, unless it has built in logic level shifters (which interconvert the voltage levels).

Some SD card modules have those logic level shifters, and it is safe to use them with a 5V Arduino. Other SD card modules do not have logic level shifters. If not, you have to add them.

Here is an example of a bidirectional logic level shifter.

Sorry I didn't realize it worked like that. How would I be able to tell if the SD card module has level shifters or not?

Check the SD module product page, data sheet or operating manual.

Or, post a close up, focused photo of the module, sometimes it is obvious.


Here is a closeup. What could I do if there are no level shifters?

That doesn't help.

The pictured card does not have an obvious relationship to the one for which you posted the schematic. It may have series resistors, which is a cheap, dirty and unreliable solution to the level problem.

Which pins correspond to MISO, MOSI, etc. in your schematic, and how did you actually connect it up?

I recommend this SD module (the larger black chip is the level shifter): MicroSD card breakout board+ : ID 254 : $7.50 : Adafruit Industries, Unique & fun DIY electronics and kits

The pins on this board are also labeled on the back, I was told to use that labeling:


Thanks for the recommendation!

Who knows, that module might work.

I suggest to start over and test whether the module functions correctly using one or more of the example programs from the SD library or better, the SDFat library, and a couple of different SD cards.

Thanks for the help! I will try the example programs and see how it goes. If it still doesn't work I'll look into buying the sd card module you recommended.