2012/05/01

Freeduino/Arduino RTC Sensor Shield To Display Date and Time In Arduino Serial Monitor Using Wire.h + RTClib.h

First, download RTClib.h from the following link.

There is a new version using Time.h library and showing day of week.

Pins Used

Analog pins 4 and 5 are used for I2C protocol implemented  by Maxim DS1307.


Serial Monitor Output


Sketch

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// 2010-02-04 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
// $Id: ds1307.pde 4773 2010-02-04 14:09:18Z jcw $
// Revised by Befun Hung <bfhorng@ms4.hinet.net> 2011-11-13
// RTClib.h download webpage http://www.ladyada.net/make/logshield/download.html
// File Name: DS1307RTClib.pde - Display Date And Time in Arduino Serial Monitor
// The sketch works on Arduino IDE 0022

#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 RTC;

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
 
    // following line sets the RTC to the date & time this sketch was compiled
    // RTC.adjust(DateTime(__DATE__, __TIME__));
}

void loop () {
    DateTime now = RTC.now();
 
    Serial.print(now.year(), DEC);
    Serial.print('/');
    if (now.month() < 10) {
      Serial.print('0');
    }
    Serial.print(now.month(), DEC);
    Serial.print('/');
    if (now.day() < 10) {
      Serial.print('0');
    }
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    if (now.hour() < 10) {
      Serial.print('0');
    }
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    if (now.minute() < 10) {
      Serial.print('0');
    }
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    if (now.second() < 10) {
      Serial.print('0');
    }
    Serial.print(now.second(), DEC);
    Serial.println();
    delay(995);
 
}

1 comment:

  1. Enthusiastic words written in this blog helped me to enhance my skills as well as helped me to know how I can help myself on my own. I am really glad to come at this platform.
    belden

    ReplyDelete