Arduino: DS1307 RTC I2C Setup

From OnnoWiki
Jump to navigation Jump to search

Sumber: http://www.labelektronika.com/2018/02/cara-program-rtc-ds1307-menggunakan-Arduino.html

Wiring-RTC-DS1307-Dengan-Arduino.png
RTC DS1307	   Arduino     
SCL	           A5
SDA	           A4
VCC	           +5V
GND	           Gnd

Library

https://github.com/adafruit/RTClib


Code

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

RTC_DS1307 rtc;

char namaHari[7][12] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};

void setup () {
  Serial.begin(9600);
  if (! rtc.begin()) {
    Serial.println("RTC TIDAK TERBACA");
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//update rtc dari waktu komputer
  }
}

void loop () {
    DateTime now = rtc.now();
    Serial.print(namaHari[now.dayOfTheWeek()]);  
    Serial.print(',');    
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), 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.println();
   
    delay(3000);
}


Referensi