Cannot get SD card to write timestamp and temperature

I am using an Arduino UNO with a Micro SD Card Adapter, a DS3231 RTC, an analog temperature amplifier, and a 16X2 LCD.
The problem I am having is writing data to the SD card.
The SC card Initalizes and the timestamp and temp show on the serial monitor.
But I cannot get any data written to the card.
What I want to do is when Pin7 goes HIGH, to record one line of the timestamp and temp to the SD card.
I have run the SD examples of ReadWrite and Datalogger and they write to the card just fine.
But when I run my program attached, nothing gets recorded even though the date and time and temperatures show on the serial monitor. What is wrong with my program? Thank you for your help.

include.txt (11.5 KB)


        int digitalRead(Pin7); // OOOPPPSSSS
        if (val == HIGH) // WHAT IS val?
          logfile ="TEMPBP.txt", FILE_WRITE);

          logfile.flush(); // write to file
#endif // ECHO_TO_FILE

also why don’t you have all your logfile.print() in between the and logfile.close() ?

Again, what is Val? (Hint ... Where is it defined?)

Share the modified code... or we can’t help as we don’t know what you really did

I see that code now (correctly checking the pin)

 if (digitalRead(Pin7) == HIGH) {
logfile ="TEMPBP.txt", FILE_WRITE);


logfile.flush(); // write to file
#endif // ECHO_TO_FILE <<== suspicious location of the #endif

but you write NOTHING in between the open and close statement besides a CRLF… so I’m not surprised you see no data

Double Check also that every open() as a matching close() for your file.

Double Check that every time you perform a logfile.print the file is actually open.

You also have a suspicious location of the #endif before the closing bracket

Please press ctrl-T in the IDE to correctly indent the code before sharing. It’s painful to read on my smartphone

What "stuff" should I write between logfile.printly(); and logfile.fllush();

all the prints for what you want to see in the file...

On a mac it’s cmd-T to indent (it’s also available in the menu)


The else you added is for testing the pin, isn’t it ?

Why do you do this ?

 while (digitalRead(Pin7) == HIGH) {
      //wait until Pin7 is HIGH again

the pin was high when you logged and it’s likely still HIGH

Seasons greetings !

Track your


there is very likely an extra one that is not needed.
The lazy way to do this is to replace all of them by a logfile.println("xxx");with a different xxx for each println(). (Like aaa,bbb,ccc,ddd,...)
Then run your code and you’ll see what’s on the unwanted line. If it’s bbb then get rid of the logfile.println("bbb");and put back the println() in the other places

This is something you can do. It's called a structured walk through. Pretend you are the processor and single step through the program lines, keeping track of the state of important variables, and what characters are sent to the output.

The empty println() are creating the white space.
You have multiple of those in your code (including a suspicious one before closing the file)
What I suggested was to change each of these similarly looking output into something different by printing a different text and the new line.

For example if your text looks like this today





And after you change the prints it looks like this


Then you know that the line which printed bbb is the culprit.

That’s a lazy way to perform the code walkthrough @aarg suggested - which is a good practice.