Arduino Nano

Arduino basic Tutorial

Blynk iot Tutorial

Visit all the Links

ArduinoTutorial by Manmohan Pal

DS1307 RTC Tiny I2C Module - How to set Date and Time- by Manmohan Pal

DS1307 RTC Tiny I2C Module - How to set Date and Time- by Manmohan Pal


Arduino Tiny RTC I2C Real Time Clock

This Arduino Tiny RTC I2C module incorporates the DS1307 I2C real time clock IC and the 24C32 32K I2C EEPROM storage. What's more, it has a DS18B20 temperature sensor on board. All of this in a tiny package of 25mm x 28mm x 8.4mm. It comes with a LIR2303 rechargeable lithium battery, and a charging circuit is included in the module. When the temperature sensor is off, the RTC module can run for 1 year on a single charge.

This module is used for applications such as datalogging, timing applications ex. turning on the sprinkers at 4pm in the evenings. Since the module is self powered the time data is maintained even if the Arduino is powered off, allowing for building low power systems which can run for a long span of time without change of batteries.

Library
https://drive.google.com/open?id=1u0QFAXpzOzlIIlJBt426T1_PWYDm6d5j


Arduino Tiny RTC I2C Real Time Clock Pinout
PIN     Description Comment
BAT Battery voltage To monitor the battery voltage, or not connected
GND Ground Ground
VCC 5V supply Power the module and charge the battery
SDA I2C data I2C data for the RTC
SCL I2C clock I2C clock for the RTC
DS         DS18B20 Temp. Sensor output One wire inteface
SQ         Square wave output Normally not used

The I2C wires "SDA" and "SCL" are the data line and clock line, they should be connected to the corresponding pins depending on the Arduino board.

Board I2C / TWI pins
Uno, Ethernet A4 (SDA), A5 (SCL)
Mega2560 20 (SDA), 21 (SCL)
Leonardo 2 (SDA), 3 (SCL)
Due 20 (SDA), 21 (SCL), SDA1, SCL1

________________________________________________________

code

________________________________________________________
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

No comments:

Post a Comment