SD recording data Fail

Hello,

I have a project of making a weather station recording data of temp, humidity, light by time, and date in an SD. However, when I tried to implement the code it keeps failing on me.

Can anyone please on what I did wrong on that code?

I am using Arduino UNO + Arduino Ethernet Shield.
my micro SD is ScanDisk Ultra 64 GB XC I

This is my code

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


File file;  //  associate file with SD library
dht DHT;
DS3231  rtc(SDA, SCL); //  associate rtc with DS3231 library

int CSpin = 10; // chip select pin for SD card
#define Dht A2 //Dht
#define TemPin A0  //tmp
int LDR= A1; //Ldr
int i = 0;  // data record counter
long check, light, temp, humid;
String data, date, time;
String filename = "data.csv";  // filename


void setup() {

   Serial.begin(9600);  // define Serial output baud rate
 
  pinMode(LDR, INPUT); // LDR INPUT
    // Begin serial communication at a baud rate of 9600:

    
 

  
  rtc.begin(); 
  //rtc.setDOW(SUNDAY);  // set weekday
  rtc.setTime(10, 43, 40); // set the time to hh mm ss
  rtc.setDate(01, 3, 2022); // set the date to dd mm yyyy


  Serial.println("checking SD card"); // check for presence of SD card
  if(SD.begin(CSpin) == 0)
  {
  Serial.println("Card fail"); //  return to void setup() if SD card not found
  return;
  }

  Serial.println("Card OK");
  if(SD.exists(filename)>0) SD.remove(filename);  // delete old file
  file = SD.open(filename, FILE_WRITE);  // create new file
  if(file == 1)
  {  // column headers
  String header = "record, time, light, temp, humid, ON_Date >>";
  header = header + String(rtc.getDateStr());  // date
  file.println(header); //  write column headers to file
  file.close();  //  close file after writing to SD card
  }

  else Serial.println("Couldn't access file"); //  file not opened
  }

void loop() {

  i++;  //  increase data record counter
  Serial.print("record ");Serial.println(i); //  print record number
  light= analogRead(LDR); //  light intensity reading
  check = DHT.read11(Dht);


int Tempreading = analogRead(TemPin);
              // Convert the reading into voltage:           
  float voltage = Tempreading * (5000 / 1024.0); //temp
  // Convert the voltage into the temperature in degree Celsius:
  float temperature = voltage / 10; //temp
  
temp =  temperature ;//  temperature reading




humid = DHT.humidity;  //  humidity reading
time = rtc.getTimeStr();  // time stamp


// combine measurements into a string
data = String(i) + "," + String(time) + "," + String(light);
data = data + "," + String(temp) + "," + String(humid);
file = SD.open(filename, FILE_WRITE); //  open data file before writing
file.println(data);  //  write data string to file 
file.close();  //  close file after writing to SD card
delay(1000);  //  delay 1s before next reading 

}


This on my serial COM check

"checking SD card
Card fail
record 1
"

Perhaps you could tell us what is failing and how you know this. Then we might know what to look for.

1 Like

Hi Paul, what's failing is the data doesn't get created on a text file in the SD. when I try to check the SD card on my phone it doesn't display any text files containing the results of my sensors readings.

1 Like

Oh, I see that you are checking the pin number rather than the SD card file.

I’ve had issues getting my SD card module to work
Presuming you are using an uno, here is what ended up working for me
I have no idea why it works or even if all the steps below are needed

  1. Switch the CS to pin 4
    Add in the void setupvoid setup the following 2 lines

PinMode (10, OUPUT);
digitalWrite(10,HIGH);

1 Like

This is a very common beginner mistake. Opening and closing the file just to write one data point VASTLY increases the SD card error rate, and amount of time required to write data. Open the file once in setup() and close it when you are all done collecting data. Issue a file.flush() command once an hour or once a day to keep the file pointers up to date.

file = SD.open(filename, FILE_WRITE); //  open data file before writing
file.println(data);  //  write data string to file 
file.close();  //  close file after writing to SD card

Also, on AVR based Arduinos like the Uno, use of Strings causes memory fragmentation, and program failures. They are never necessary, so get rid of them.

Welcome to the forum

Failing in what way ?

Sorry man, I'm confused. can you clarify more?
My CSpin is pin 10 and I was saying if it wasn't responding then type "Card fail".
I read on UNO its pin 10 ,right ?

I am not sure if I can switch CS to pin 4. I am using an Arduino Ethernet shield on top of my UNO. I heard it is set to 10 by default. But I will give it try and will tell you if it worked.

Unfortunately, it doesn't work

'PinMode' was not declared in this scope"

I wasn’t using a shield so that may not work for you

No such function. Use pinMode() instead.

Also you may want to check the capitalization on pinmode . I never get it right and rely on ide to change Color when it’s right

Ah, sadly it still didn't work. It compiled fine this time but it gave me the same message.

checking SD card
Card fail
record 1

The issue is that it doesn't even need to complete an hour. I am testing it out within a minute. However, it complies but it gives me this annoying message that is failing to create a file.

Well, this is a problem, because the filename is NOT a String.

String filename = "data.csv";  // filename

You should instead be using a zero terminated character array, as required.

char filename[] = "data.csv";

DON'T USE Strings.

1 Like

the code should make the sensors readings written in a file on the SD card. but when I run the code it compiles fine but it gives me a message after the SD checking that it is failing.

I am lost, It is still failing dude.

this is the only way I learned from youtube so far and it worked in proteus simulation.
however, when I went to try it out on the hardware it just didn't work.

Have you changed any of the code, after considering the problems pointed out to you?

If so, post the revised code, using code tags, along with the relevant error messages.

Please read and follow the instructions in the "How to get the best out of the forum" post, linked at the head of every forum topic.

yeah, I tried them. I will keep trying and see how it goes.