RTC Help!

Hello, I’m fairly new to coding but have started a project with temperature sensors on my Arduino Uno device. I am trying to take measurements on air temperature using a ds18b20 thermometer. Our Real time clock is not syncing up to our system and I am in desperate need of help coming up with a solution. The clock is 2 months behind. How can i get my device to sync up to my laptop. It runs off of windows 10. Here is my code:

// This code will output 2 temperature sensor readings along with timestamps. This data will be output to the serial port as well as an SD card.
// One Wire Digital Temp Sensor DS18B20 (Sparkfun: SEN-00245), Adafruit Datalogging Shield
// By: Spencer McDonald
// 2/19/14

// To connect the digital temp sensor: Flat facing away, pins pointing down, starting at right, GND - bus - 5V. NOTE: 4.7 kOhm between 5 V and bus
// To connect the SD/RTC shield, plug it in.

// The RTC uses the I2C bus [A4 and A5], the digital temp sensors use digital pin 2. The SD card uses digital pins 10-13.

// Libraries to include

#include “SD.h” // SD library

#include <Wire.h> // RTC library
#include “RTClib.h”

#include <OneWire.h> // Digital temperature library
#include <DallasTemperature.h>

// Initalize Variables

#define LEDpin 4

#define ONE_WIRE_BUS 2

float intempC = 0;

const int chipSelect = 10; // Sparkfun SD uses 8

File datalog;
char filename = “LOGGER00.csv”;
RTC_DS1307 RTC;

// Initalize Digital temperature sensor

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Arrays to hold device address
DeviceAddress insideThermometer;

void setup()
{
Serial.begin(115200); // Opens the communication between Arduino and computer serial port

Wire.begin(); // Begins RTC communication
RTC.begin();

sensors.begin(); // Begins digital temp sensor

// Check if RTC is running, if not fix it

if (! RTC.isrunning()) {
Serial.println(“RTC is NOT running!”);
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(DATE, TIME));
}

// Initalize SD Card

Serial.print(“Intializing SD Card…”);
pinMode(10, OUTPUT); // Necessary for microSD shield

if(!SD.begin(chipSelect))
{
Serial.println(“Card failed, or not present”);
return;
}
Serial.println(“Card initialized.”);

Serial.print(“Creating File…”);

// Make a new file each time the arduino is powered

for (uint8_t i = 0; i < 100; i++)
{
filename[6] = i/10 + ‘0’;
filename[7] = i%10 + ‘0’;
if (! SD.exists(filename))
{
// only open a new file if it doesn’t exist
datalog = SD.open(filename, FILE_WRITE);
break;
}
}

Serial.print("Logging to: ");
Serial.println(filename);

if(!datalog)
{
Serial.println(“Couldn’t Create File”);
return;
}

// Print Header

String Header = "RTC Date, RTC Time, In Temp

";   
  
  datalog = SD.open(filename, FILE_WRITE);
  datalog.println(Header);
  Serial.println(Header); 
   
// Get address of digital temp sensor 

  if (!sensors.getAddress(insideThermometer, 0)) {
    datalog.println("Unable to find address for Device 0");
    Serial.println("Unable to find address for Device 0");
  }
  

  
  
// Set the resolution to 9 bit 
  sensors.setResolution(insideThermometer, 12);    
     
  datalog.close();
  
  digitalWrite(LEDpin, OUTPUT);
 
}

//___________________________________________________________________________________

void loop()
{  
datalog = SD.open(filename, FILE_WRITE);
 
// Real Time Clock

  DateTime now = RTC.now();

  datalog.print(now.month(), DEC);
  datalog.print("/");
  datalog.print(now.day(), DEC);
  datalog.print("/");
  datalog.print(now.year(), DEC);
  datalog.print(",");
  datalog.print(now.hour(), DEC);
  datalog.print(":");
  datalog.print(now.minute(), DEC);
  datalog.print(":");
  datalog.print(now.second(), DEC);
  datalog.print(",");
 
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print("/");
  Serial.print(now.year(), DEC);
  Serial.print(", ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print(", ");
  
  
// Digital Temperature

  sensors.requestTemperatures(); // There might be a better place for this function

  intempC = sensors.getTempC(insideThermometer);

  datalog.print(intempC);


  Serial.print(intempC);

  


// Close the file and flash LED to show data was recorded

datalog.println("");
Serial.println("");

datalog.close(); 

//digitalWrite(LEDpin, HIGH);  //Flash a LED to know that it is logging data
delay(1000);
//digitalWrite(LEDpin, LOW);
}

Please edit your post and enclose the code in code tags ("</> button). See “How to use this forum” if questions.

Sorry about that!

// This code will output 2 temperature sensor readings along with timestamps.  This data will be output to the serial port as well as an SD card.
// One Wire Digital Temp Sensor DS18B20 (Sparkfun: SEN-00245), Adafruit Datalogging Shield
// By: Spencer McDonald
// 2/19/14

// To connect the digital temp sensor:  Flat facing away, pins pointing down, starting at right, GND - bus - 5V.  NOTE: 4.7 kOhm between 5 V and bus
// To connect the SD/RTC shield, plug it in.

// The RTC uses the I2C bus [A4 and A5], the digital temp sensors use digital pin 2.  The SD card uses digital pins 10-13.

// Libraries to include

#include "SD.h"                    // SD library

#include <Wire.h>                  // RTC library
#include "RTClib.h"

#include <OneWire.h>               // Digital temperature library
#include <DallasTemperature.h>


// Initalize Variables

#define LEDpin 4

#define ONE_WIRE_BUS 2

float intempC = 0;

const int chipSelect = 10; // Sparkfun SD uses 8
                         
                         
File datalog;
char filename[] = "LOGGER00.csv";
RTC_DS1307 RTC;


// Initalize Digital temperature sensor

  // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
     OneWire oneWire(ONE_WIRE_BUS);

  // Pass our oneWire reference to Dallas Temperature. 
     DallasTemperature sensors(&oneWire);

  // Arrays to hold device address
     DeviceAddress insideThermometer;

void setup()
{
  Serial.begin(115200); // Opens the communication between Arduino and computer serial port
  
  Wire.begin();         // Begins RTC communication
  RTC.begin();
  
  sensors.begin();      // Begins digital temp sensor
  
// Check if RTC is running, if not fix it

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }


// Initalize SD Card  
  
  Serial.print("Intializing SD Card...");
  pinMode(10, OUTPUT); // Necessary for microSD shield
  
  if(!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("Card initialized.");  
  
  Serial.print("Creating File...");
  

// Make a new file each time the arduino is powered

  for (uint8_t i = 0; i < 100; i++) 
  {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) 
    {
      // only open a new file if it doesn't exist
      datalog = SD.open(filename, FILE_WRITE); 
      break;
    }
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
  
  if(!datalog)
  {
  Serial.println("Couldn't Create File"); 
  return;
  }
  
// Print Header
  
  String Header = "RTC Date, RTC Time, In Temp [C]";   
  
  datalog = SD.open(filename, FILE_WRITE);
  datalog.println(Header);
  Serial.println(Header); 
   
// Get address of digital temp sensor 

  if (!sensors.getAddress(insideThermometer, 0)) {
    datalog.println("Unable to find address for Device 0");
    Serial.println("Unable to find address for Device 0");
  }
  

  
  
// Set the resolution to 9 bit 
  sensors.setResolution(insideThermometer, 12);    
     
  datalog.close();
  
  digitalWrite(LEDpin, OUTPUT);
 
}

//___________________________________________________________________________________

void loop()
{  
datalog = SD.open(filename, FILE_WRITE);
 
// Real Time Clock

  DateTime now = RTC.now();

  datalog.print(now.month(), DEC);
  datalog.print("/");
  datalog.print(now.day(), DEC);
  datalog.print("/");
  datalog.print(now.year(), DEC);
  datalog.print(",");
  datalog.print(now.hour(), DEC);
  datalog.print(":");
  datalog.print(now.minute(), DEC);
  datalog.print(":");
  datalog.print(now.second(), DEC);
  datalog.print(",");
 
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print("/");
  Serial.print(now.year(), DEC);
  Serial.print(", ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print(", ");
  
  
// Digital Temperature

  sensors.requestTemperatures(); // There might be a better place for this function

  intempC = sensors.getTempC(insideThermometer);

  datalog.print(intempC);


  Serial.print(intempC);

  


// Close the file and flash LED to show data was recorded

datalog.println("");
Serial.println("");

datalog.close(); 

//digitalWrite(LEDpin, HIGH);  //Flash a LED to know that it is logging data
delay(1000);
//digitalWrite(LEDpin, LOW);
}

The clock is 2 months behind. How can i get my device to sync up to my laptop.

Your RTC setting routine is inside an conditional which is not met. since the RTC is running, but not at the correct time. Modify those lines to unconditionally set the clock to the compile time. You can then take the lines out of the code completely.

EDIT: I was not clear enough on what to do. Set the clock by running the code without the conditional. Then comment out the setting line, and compile/upload the code again. If you don't do that, the rtc will revert to the original compile time if the Arduino is reset or power is removed.

// Check if RTC is running, if not fix it

  //if (! RTC.isrunning()) {
  //  Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
//  }