Arduino Temperature Data Logger

hi. im trying to do a temperature datalogger. im using arduino uno.LM 35 as temperature sensor, precision RTC ds3231m, and a micro Sd card reader.

connections for the DS3231 time clock
GND of DS3231 to GND on the Arduino
VCC of DS3231 to 5V pin on the Arduino
SDA of DS3231 to A4 on the Arduino
SCL of DS3231 to A5 on the Arduino

connections for the SD card module
CS of mini SD card module to digital pin 10 on the Arduino
SCK of mini SD card module to digital pin 13 on the Arduino
MOSI of mini SD card module to digital pin 11 on the Arduino
MISO of mini SD card module to digital pin 12 on the Arduino
VCC of mini SD card module to digital 5V on the Arduino
GND of mini SD card module to digital GND on the Arduino

connections for the LM35 temperature sensor
VCC pin of LM35 to 5V pin on the Arduino
OUT pin of LM35 to A0 on the Arduino
GND pin of LM35 to GND on the Arduino

#include <SD.h>

#include <SPI.h>

#include <DS3231.h>

File data_file;

DS3231  rtc(SDA, SCL);




const int lm35_pin = A0; 

int temperature;  

int chip_select_pin = 10;     //pin 53 for arduino mega 2560




void setup() {

  Serial.begin(9600);

  rtc.begin();  

  pinMode(lm35_pin, INPUT);

  pinMode(chip_select_pin, OUTPUT);

  if (SD.begin())

  {

    Serial.println("Initialization Successful. Ready to use");

  } else

  {

    Serial.println("Initialization failed. Check your pin connections or change your SD card");

    return;

  }

    

}




void loop() {

  temperature = analogRead(lm35_pin);

  temperature = (temperature*500)/1023;

  data_file = SD.open("test.txt", FILE_WRITE);

  if (data_file) {    

    Serial.print(rtc.getTimeStr());

    data_file.print(rtc.getTimeStr());

    Serial.print(",");

    data_file.print(",");    

    Serial.println(temperature);

    data_file.println(temperature);

    data_file.close();

  }

  

  else {

    Serial.println("error opening your SD card file. Try again");

  }

  delay(3000);

}

after assembling all connection. during my test, i encounter initialization failed check your pin connection or change your sd card. and error on Sd card. i have anew formatted sd card. but still have error on sd card. anyone encounter this problem? is it problem with my code?
my refercne is from this website.

any helps is highly appreciated. thanks in advance.

You are not passing in the value of your chip select pin to the SD.begin() function

  if (SD.begin(chip_select_pin))
  {
    Serial.println("Initialization Successful. Ready to use");
  } else

  {
    Serial.println("Initialization failed. Check your pin connections or change your SD card");
    return;

  }

I'm not sure what pin it defaults to if you don't pass it in.

Because I could not compile your posted sketch in my system (due to DS3231 Library), I am posting a data logger system/setup which you may practice.
lm35sd.png

1. At the elapse of every 3-second time (adjustable in the program), the temperature signal of LM35 is acquired. The value is shown on the Serial Monitor along with Date and Time. The value is also recorded on the SD Memory Card along with Date and Time.

2. While the MCU is waiting to see the exhaust of 3-second time, it checks if an external Button (K1) connected with DPin-2 has been pressed down. If so, the MCU reads the recorded data from the SD card and shows on the Serial Monitor.

The Codes:

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#define SDPIN 4
byte prSec;
File myFile;
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {
                              "Sunday", "Monday", "Tuesday",
                              "Wednesday", "Thursday", "Friday", "Saturday"
                            };

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  analogReference(INTERNAL);
  pinMode(2, INPUT_PULLUP);   //Button to read data from SD card and show on Serial Moniotr

  SD.begin(SDPIN);
  SD.remove("test.txt");   //remove file of this name
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //rtc.adjust(DateTime(2019, 4, 22, 12, 21, 45));

  myFile = SD.open("test.txt", FILE_WRITE);
}

void loop()
{
  byte prSec = bcdSeconds();
  Serial.println(prSec);
  while (bcdSeconds() - prSec < 3)   //adjustable ; now 5 minutes interval
  {
    if (digitalRead(2) == LOW)
    {
      myFile.close();
      Serial.println("Reading data from SD Card...!");
      readFromSDCard();
HERE: goto HERE;    //wait for ever
    }
  }
  prSec = bcdSeconds();
  dataLog();
  Serial.println();
}

byte bcdSeconds()
{
  DateTime nowTime = rtc.now();
  if (nowTime.second() == 0)
  {
    prSec = 0;
    return 0;
  }
  else
  {
    DateTime nowTime = rtc.now();
    return nowTime.second();
  }
}

void dataLog()
{
  DateTime nowTime = rtc.now();  //get copy of Time/Calendar onto nowTime
  //---------------------------------------------------------------
  float temperature = (float)100 * (1.1 / 1023.0) * analogRead(A0);
  //---------------------------------------------------------------
  Serial.print(" ");
  Serial.print("Temp: ");
  Serial.print(temperature, 2);
  Serial.print(" degC");
  myFile.print(" ");
  myFile.print("Temp: ");
  myFile.print(temperature, 2);
  myFile.print(" degC");
  //----------------------------

  Serial.print(" ");
  Serial.print(nowTime.year());
  Serial.print('/');
  Serial.print(nowTime.month());
  Serial.print('/');
  Serial.print(nowTime.day());
  Serial.print(" ");
  Serial.print(daysOfTheWeek[nowTime.dayOfTheWeek()]);

  Serial.print(" ");
  Serial.print(nowTime.hour());                     // hour, minute, and second
  Serial.print(':');
  Serial.print(nowTime.minute());
  Serial.print(':');
  Serial.print(nowTime.second());
  Serial.println(' ');

  myFile.print(' ');
  myFile.print(nowTime.year(), DEC);
  myFile.print('/');
  myFile.print(nowTime.month(), DEC);                     //  month
  myFile.print('/');
  myFile.print(nowTime.day(), DEC);                      //  date
  myFile.print(" ");
  myFile.print(daysOfTheWeek[nowTime.dayOfTheWeek()]);   // day of the week

  myFile.print(" ");
  myFile.print(nowTime.hour(), DEC);                     // hour, minute, and second
  myFile.print(':');
  myFile.print(nowTime.minute(), DEC);
  myFile.print(':');
  myFile.print(nowTime.second(), DEC);
  myFile.println(' ');
  //-----------------------------------------------
}

void readFromSDCard()
{
  myFile = SD.open("test.txt", FILE_READ);
  if (myFile)
  {
    while (myFile.available())
    {
      char x = (char)myFile.read();
      Serial.print(x);
    }
    myFile.close();
  }
  else
  {
    Serial.println("File can't be opened...!");
  }
}

lm35sd.png

blh64:
You are not passing in the value of your chip select pin to the SD.begin() function

  if (SD.begin(chip_select_pin))

{
    Serial.println("Initialization Successful. Ready to use");
  } else

{
    Serial.println("Initialization failed. Check your pin connections or change your SD card");
    return;

}



I'm not sure what pin it defaults to if you don't pass it in.

yes sir. thank you very much. as soon as i add chip select pin. it works like charm. anyway do you have any idea how to to insert lcd in this project. thank again

Paindrayden96:
yes sir. thank you very much. as soon as i add chip select pin. it works like charm. anyway do you have any idea how to to insert lcd in this project. thank again

Yes, I do. Do you? It really depends on what type of lcd you insert. It may take several pins if you drive it directly, or it could be an i2c device that communicates with the Wire library.

Whatever you have (or will get), test it out by itself and get it working. Only after that, try to integrate it into your current project. If you get stuck, start a new thread and people will help.