DS1302 showing fixed time i.e. 00:00:00

this is the code i have used it was taken from an example ,i have 2 ic's code works fine for 1 ic but for other same ic it doesn't work at all and is showing fixed time 00:00:00

// Example sketch for interfacing with the DS1302 timekeeping chip.
//
// Copyright (c) 2009, Matt Sparks
// All rights reserved.
//
// http://quadpoint.org/projects/arduino-ds1302
#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:
//
//   http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
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(t.day);

  // 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",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
}

}  // namespace

void setup() {
  Serial.begin(9600);

  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  rtc.writeProtect(false);
  rtc.halt(false);

  // Make a new time object to set the date and time.
  // Sunday, September 22, 2013 at 01:38:50.
  Time t(2013, 9, 22, 1, 38, 50, Time::kSunday);

  // Set the time and date on the chip.
  rtc.time(t);
}

// Loop and print the time every second.
void loop() {
  printTime();
  delay(1000);
}

Post your circuit diagram also

i only have connected the esp pins (5,18,19) to the respective pinouts of DS1302. that's the only circuit !!

Esp 32 or esp8266?
Can you post a pic of it

actually i have disintegrated the circuit for trying another circuit, module is

Try this

#include <DS1302RTC.h> // include the DS1302RTC library
#include <Time.h> // include the Time library

// Define the DS1302 connections
const int RTC_CE_PIN = 5; // RTC chip enable
const int RTC_IO_PIN = 18; // RTC data line
const int RTC_CLK_PIN = 19; // RTC clock line

DS1302RTC rtc(RTC_CE_PIN, RTC_IO_PIN, RTC_CLK_PIN); // create an instance of the DS1302RTC class

void setup() {
  // Set the serial communication baud rate
  Serial.begin(9600);
  // Start the DS1302
  rtc.begin();
  // Set the time to 10:00 AM on Monday, April 28th, 2023
  tmElements_t tm;
  tm.Year = 123; // year 2023 - 1970 = 53, so 2023 is year 53 in the tmElements_t structure
  tm.Month = 4;
  tm.Day = 24;
  tm.Hour = 10;
  tm.Minute = 0;
  tm.Second = 0;
  tm.Wday = 1; // Monday
  rtc.write(tm); // write the time to the DS1302
}

void loop() {
  // Read the current time from the DS1302 and print it to the serial monitor
  tmElements_t tm = rtc.get();
  Serial.print("Time: ");
  Serial.print(tm.Hour);
  Serial.print(":");
  Serial.print(tm.Minute);
  Serial.print(":");
  Serial.print(tm.Second);
  Serial.print(" ");
  Serial.print(tm.Day);
  Serial.print("/");
  Serial.print(tm.Month);
  Serial.print("/");
  Serial.println(tmYear + 1970); // year 53 + 1970 = 2023
  delay(1000); // wait for 1 second before reading the time again
}

Can't you show us those "respective" pinout connection

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.