Sunday, June 14, 2015

RTC with Arduino



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

// LCD
// LCD D4 to Arduino 9
// LCD D5 to Arduino 10
// LCD D6 to Arduino 11
// LCD D7 to Arduino 12
// RS 4 to Arduino 7
// EN 6 to Arduino 8
// RH 5 to GND
// pin 3 to center pot
LiquidCrystal lcd(23, 22, 21, 20, 19, 18);  

// DS1307 RTC
// SDA pin to Arduino A4
// SCL pin to Arduino A5
RTC_DS1307 RTC;

void setup() {
  Serial.begin(57600);
  Wire.begin();
  RTC.begin();

  /*
  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(10/23/2012, 10:30:00));
   }
   */

  lcd.begin(16,2); // 16x2 LCD
  lcd.clear(); // blank the display
}

void loop() {
  DateTime now = RTC.now();
  lcd.clear();
  lcd.setCursor(0,0);
  if (now.day() < 10) {
    lcd.print("0");
  }

  lcd.print(now.month(), DEC);
  lcd.print("/");
  if (now.month() < 10) {
    lcd.print("0");
  }

  lcd.print(now.day(), DEC);
  lcd.print("/");
  lcd.print(now.year(), DEC);
  lcd.setCursor(0,1);
  if (now.hour() < 10) {
    lcd.print("0");
  }

  lcd.print(now.hour(), DEC);
  lcd.print(":");
  if (now.minute() < 10) {
    lcd.print("0");
  }

  lcd.print(now.minute(), DEC);
  lcd.print(":");
  if (now.second() < 10) {
    lcd.print("0");
  }

  lcd.print(now.second(), DEC);
  delay(1000);
}

No comments:

Post a Comment