Data logging to SD micro SD not working

I altered the data logging example code for SD cards trying to log information from some temp sensors and an RTC. The temp sensors read fine and the RTC as well but I can’t seem to get the data onto the sd card. The program seems to be working completely according to my serial monitor but when I pull the sd card out I find no changes to what was already on it.

Code Below:

#include <Wire.h>

#include <DS3231.h>
DS3231 rtc(SDA, SCL);

float temp_read1()
Wire.requestFrom(0x48, 2);
byte h =;
byte l =;
return ((h << 3) | (l >> 5)) * 0.125;

float temp_read2()
byte a =;
byte b =;
return ((a << 3) | (b >> 5)) * 0.125;

float temp_read3()
byte d =;
byte c =;
return ((d << 3) | (c >> 5)) * 0.125;

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

const int chipSelect = 4;

void setup() {
// Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only

Serial.print(“Initializing SD card…”);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
while (1);
Serial.println(“card initialized.”);

void loop() {
// make a string for assembling the data to log:
String dataString = “”;

// read three sensors and append to the string:
dataString += String(" Temp1: “);
dataString += String(temp_read1());
dataString += String(” Temp2: “);
dataString += String(temp_read2());
dataString += String(” Temp3: “);
dataString += String(temp_read3());
dataString += String(” Time: “);
dataString += String(rtc.getTimeStr());
dataString += String(” Date: ");
dataString += String(rtc.getDateStr());

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile =“temp_log.txt”, FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
// print to the serial port too:
// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening datalog.txt”);

attempt_3.ino (2.51 KB)

I would start by proving out your hardware with a much simpler program: see if you can print some static text to your SD card repeatedly.

But I think what may be wrong is your use of String. You’re allocating a bunch of memory on the heap and then you open your file. IIRC, that will allocate a 512 byte buffer on the heap too which gives you a risk of fragmentation.

Which Arduino are you using?

The String is pointless here anyway, you can just print each thing directly to the file, it won’t get written until you fill up the buffer (or close the file) so there’s no performance penalty.

Im using an UNO, Im fairly new to C as a programming language but this is the last step I need to finish this data logger and I haven't quite figured it out. I managed to write to the file the string "Temperatures: "
In the setup once but not in a loop.

Edit: Found out what the problem was, problem solved. Turns out the issue was computer side. Restarted my computer and it could read all the info I was writing to the SD card.