User Tools

Site Tools


Arduino Sketch - Read time on RTC DS1302

// Example sketch for interfacing with the DS1302 timekeeping chip.
// Copyright (c) 2009, Matt Sparks
// All rights reserved.
#include <stdio.h>
#include <DS1302.h>
namespace {
// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip. See the DS1302
// datasheet:
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock
// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);
String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  return "(unknown day)";
void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();
  // Name the day of the week.
  const String day = dayAsString(;
  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           t.yr, t.mon,,
 , t.min, t.sec);
  // Print the formatted string to serial so we can see the time.
}  // namespace
void setup() {
// Loop and print the time every second.
void loop() {
read_time_sketch.txt · Last modified: 2017/12/10 06:09 by adam