SD recording data Fail

On the Ethernet shield, Pin 10 is the Chip Select (CS) for the Ethernet chip! The CS for the SD Card slot is Pin 4.

As @ZoeBell said: Set Pin 10 to OUTPUT and HIGH so the Ethernet chip doesn't think you are talking to it. Having two SPI devices with their CS lines LOW will cause a collision on the SPI bus.

1 Like

Thank you A lot guys for the help. The problem appeared to be in the format of my SD. It had to be Fat32 and also on my UNO. When I switched to Mega 2560...I started seeing my numbers getting recorded!

I changed my code to this now

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header
 		Pin 4 used here for consistency with other Arduino examples
 
 created  24 Nov 2010
 modified 9 Apr 2012 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */
#include <dht.h>
#include <SPI.h>
#include <SD.h>
#include <DS3231.h>


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



String data, date, time;
int Dht= A2 ;//Dht
int TemPin= A0;  //tmp
int LDR= A1; //Ldr
long check, light, temp, humid;


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 tempo = voltage / 10; //temp

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

File dataFile;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  
  pinMode(LDR, INPUT); // LDR INPUT
  
  rtc.begin(); 
  rtc.setDOW(SUNDAY);  // set weekday
  rtc.setTime(10, 43, 40); // set the time to hh mm ss
  rtc.setDate(27, 2, 2022); // set the date to dd mm yyyy




  
  
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
  
  // 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.");
  
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
}

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



  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 4; analogPin++) {
    int Light = analogRead(LDR);
    int Temp = analogRead(tempo);
    int Humid = analogRead(Dht);
    int sensor = analogRead(analogPin);
    time = rtc.getTimeStr(); 
       dataString = String(time)+ ","+ String(Temp)+"\xC2\xB0"+","+ String(Light)+","+String(Humid)+"%";
    if (analogPin < 4) {
      dataString += ","; 
    }
  }

  dataFile.println(dataString);

  // print to the serial port too:
  Serial.println(dataString);
  
  // The following line will 'save' the file to the SD card after every
  // line of data - this will use more power and slow down how much data
  // you can read but it's safer! 
  // If you want to speed up the system, remove the call to flush() and it
  // will save the file only every 512 bytes - every time a sector on the 
  // SD card is filled with data.
  dataFile.flush();
  
  // Take 1 measurement every 500 milliseconds
  delay(500);
}

I know I'm greedy and asking for many things, but I have another question regarding DATE and TIME from my rtc. I cant record them when I set it in this part

for (int analogPin = 0; analogPin < 4; analogPin++) {
    int Light = analogRead(LDR);
    int Temp = analogRead(tempo);
    int Humid = analogRead(Dht);
    int sensor = analogRead(analogPin);
    time = rtc.getTimeStr(); 
       dataString = String(time)+ ","+ String(Temp)+"\xC2\xB0"+","+ String(Light)+","+String(Humid)+"%";
    if (analogPin < 4) {
      dataString += ","; 

I added in dataString
String(time)+","

but the compiler refuses to show me time.
fyi

I used these codes at the beginning in void setup.

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

but still I get nothing

String(time)

Have you tried printing that ?
I suspect that time does not hold what you think

By the way, what's with all of the casting to String to build dataString when you could just print the values to the file ?

Yeah, It still didn't work. Now I have doubts about the hardware after the last experience with uno and sd.

The thing is, I need them to be on the same line and to add a comma between them. So, when they're recorded like that in an SD I can easily push the data to a "cloud" google spreadsheet or an excel. I saw a few videos on YouTube of people doing that, and the data was automatically distributed on cells in replace of that comma. I was trying to do that. I am still new to an Arduino. I just started learning this board for like 2 weeks ago. I am trying to get an idea from here and there to make this project work.

If you want the data in a Comma Separated Values (CSV) file then after each data item print a comma to the file

After each set of data print a Newline character to the file

The file will contain a column value for each data item and a row for each set of data items

1 Like

This fixed a problem with being unable to open. THANX!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.