Sketch19:
To prepare for another data logging sketch, only the date and time should be written line by line to the SD card. As soon as the SPI.h library is included, SD.begin() reports 'SD initialization failed'.
This RTC3231-LCD-SD data logger shows current time on LCD at 1-sec interval; acquires RTC Temperature at 2-sec interval, and shows it on Serial Monitor with time stamp; the temperature is recorded on SD Card at 2-sec interval along with time stamp. This tutorial may help you trouble shooting your own data logger. (Codes could be added to show on Serial Monitor the recorded contents of SD Card when button K1 is pressed.)
![rtc-lcd-sd.png](https://europe1.discourse-cdn.com/arduino/original/4X/9/8/2/982b50f310d63f3bd3c6a0b416a6f9e143aed1c1.png)
![rtc-lcd-sdPIC.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/4/5/3/4537201b46b4be8ac6b5313672a51b5d31ed434f.jpeg)
#include<SPI.h> //sd cARD USES spi bUS
#include<SD.h> //contains library functions
#define CSPIN 53 //if using DPin-4, it does not require to set direction
File myFile; //file pointer variable declaration
#include <Wire.h> //needed because DS3231 uses I2C Bus
#include <RTClib.h> //needed becuase we have ready-made functions of this librray
RTC_DS3231 rtc; //the object rtc is created from the class RTC_DS3231
char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
#define deviceAddress 0x68
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte x, x5, x6;
float rtcTemp;
void setup()
{
Serial.begin(9600);
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
//rtc.adjust(DateTime(2018, 12, 23, 9, 15, 17));//set date-time manualy:yr,mo,dy,hr,mn,sec
lcd.init();
lcd.backlight();
//---------------------
Serial.begin(9600);
pinMode(CSPIN, OUTPUT);
SD.begin(CSPIN); //SD Card is initialized
delay(2000);
// SD.remove("Imp21.txt"); //remove any existing file
}
void loop()
{
DateTime nowTime = rtc.now();
x5 = nowTime.second(); //present second 23
// Serial.println(x5, DEC);
do
{
DateTime nowTime = rtc.now();
x6 = nowTime.second();
if (x6 == 0)
{
x5 = x6;
}
showTimeOnLCD();
}
while ((x6 - x5) != 2);
rtcTemp = rtcTemperature();
//nowTime = rtc.now();
Serial.print(nowTime.hour());
Serial.print(':');
Serial.print(nowTime.minute());
Serial.print(':');
Serial.print(nowTime.second());
Serial.print(" ");
Serial.print("RTC Temp: ");
Serial.print(rtcTemp, 2);
Serial.println(" degC");
rtcTempOnSD(); //store RTC temp on SD Card
}
void showTimeOnLCD()
{
DateTime nowTime = rtc.now();
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[nowTime.dayOfTheWeek()]);
lcd.print(':');
lcd.print(" ");
lcd.print(nowTime.day(), DEC);
lcd.print('/');
lcd.print(nowTime.month(), DEC);
lcd.print('/');
lcd.print(nowTime.year(), DEC);
//-------------------------
lcd.setCursor(0, 1);
lcd.print("Time: ");
byte x2 = nowTime.hour();
if (x2 < 10)
{
lcd.print('0');
}
lcd.print(nowTime.hour(), DEC);
lcd.print(':');
//---------------------
byte x1 = nowTime.minute();
if (x1 < 10)
{
lcd.print('0');
}
lcd.print(nowTime.minute(), DEC);
lcd.print(':');
//------------------------
byte x = nowTime.second();
if (x < 10)
{
lcd.print('0');
}
lcd.print(nowTime.second(), DEC);
//---------------------------
}
float rtcTemperature()
{
Wire.beginTransmission(0x68); //START, deviceAdr, data direction (write) are queued
Wire.write(0x0E);//(0x0E); //Control Register Address is queued
Wire.write(0x20);//(0x20); //Start Temperature Conversion command byte is queued
Wire.endTransmission(); //queued information are transferred on ACK
do
{
Wire.requestFrom(0x68, 1); //command to the slave to send 1-byte data
x = Wire.read(); //data is read from FIFO buffer
}
while (bitRead(x, 5) != LOW); //bit-5 of Control Register is LOW indicates end-of-conversion
//--- read Temperature Signal----------------
Wire.beginTransmission(0x68); //
Wire.write(0x11); //point higher byte of Temperature Register; queued
Wire.endTransmission(); //transfer the above queued information
Wire.requestFrom(0x68, 2); //request to send 2-byte data of Temperature Register
float tempUpper = (float)Wire.read(); //integer part is saved as: XX.00000…….
byte tempLower = Wire.read(); //fractional part as: XX000000
float tempLowerx = (float) ((tempLower >> 7) * 0.5 + ((tempLower >> 6) & 0x01) * 0.250);
float temp = tempLowerx + tempUpper;
return temp;
}
void rtcTempOnSD()
{
myFile = SD.open("Imp21.txt", FILE_WRITE); //file created/opened for writing
if (myFile) //file has really been opened
{
DateTime nowTime = rtc.now();
myFile.print(nowTime.hour());
myFile.print(':');
myFile.print(nowTime.minute());
myFile.print(':');
myFile.print(nowTime.second());
myFile.print(" ");
myFile.print("RTC Temp: ");
myFile.println(rtcTemp);
}
else
{
Serial.println("File can't be opened");
while (1);
}
myFile.close();
// myFile = SD.open("Imp21.txt", FILE_READ); //file opened for reading
// if (myFile) //file has really been opened
// {
// while (myFile.available())
// {
// Serial.print((char)myFile.read());
// }
// myFile.close();
//}
}
![rtc-lcd-sdPIC.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/4/5/3/4537201b46b4be8ac6b5313672a51b5d31ed434f.jpeg)
![rtc-lcd-sd.png](https://europe1.discourse-cdn.com/arduino/original/4X/9/8/2/982b50f310d63f3bd3c6a0b416a6f9e143aed1c1.png)