How to use rtc module DS1302

Hello fellow guys,

I’ve been looking around and looked if i could find something on Google about the rtc. But i don’t know where to start. Which library do i need to use, and which commands shall i need?

this is my code so far with a DS1302.h library (i think from the library manager)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#include <DS1302.h>

LiquidCrystal_I2C lcd(0x3F,20,4);
dht DHT;
DS1302 rtc(9,10,11);

#define DHT11_PIN  12
unsigned long prevMillis = 0;
unsigned long interval = 2000;

int ButtonUp    = 2;
int button_up   = 0;
int button_count= 0;
int lastButton  = 0;

void setup() {
  pinMode(ButtonUp,  INPUT);
  lcd.init();
  lcd.backlight();
  lcd.begin(16,2);
}

void loop() {
  
  button_up = digitalRead(ButtonUp)  ;

  unsigned long currentMillis = millis();

  if (button_up != lastButton){
    if (button_up == HIGH){
      button_count=button_count+1;
    }
  }
  lastButton = button_up;
  
  if (button_count==1){
    if (currentMillis - prevMillis > interval){
      int chk = DHT.read11(DHT11_PIN);
      prevMillis = currentMillis;
      int temp = DHT.temperature;
      int humi = DHT.humidity;
      lcd.setCursor(0,0);
      lcd.print("temp:");
      lcd.print(temp);
      lcd.print(char(223));
      lcd.setCursor(0,1);
      lcd.print("humidity:");
      lcd.print(humi);
      lcd.print("%");
    }
  }
  
  else if (button_count==2){
  lcd.print("Nothing");
  }
  
  else if (button_count==3){
    lcd.clear();
    button_count=0;
  }
}

this piece should be the clock time

  else if (button_count==2){
  lcd.print("Nothing");
  }

i want on the first line the date in dd/mm/yy and on the second line the time in hh/mm/ss.

if you have a handy link or a proper piece of code i’ll be grateful!

No part of your code has anything to do with RTCs, except the #include. Certainly not the part you claim is related to RTCs. That's totally bogus. Are you a troll?

That is the point PaulRB, i don't know what funtions to use, i've also highligthed the part where the "actual" code must be placed. I'm a newbie if it comes to rtc and time funtions. So please don't call me a troll, just having a hard time to find the propper code

Have you tried any of the examples that come with the library that you are using? Which library, post a link. Get the RTC working by itself before trying to use it with the LCD.

@groundFungus ive used this example, and this works. but i dont know how to work with the switch and case. And from the void printTime section i dont know anything about it

// 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);
}

Time to learn :wink:

The switch takes a number and based on that number executes one of the cases. The number is one of the values kSunday .. kSaturday (probably defined in the DS1302 library). So if the number is kSunday, case Time::kSunday is executed and the dayAsString function returns the String (capital S) "Sunday".

If you add a Serial.print to the dayAsString function, you can see that number.

String dayAsString(const Time::Day day) {

  Serial.print("day is ");
  Serial.println(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)";
}

dayAsString takes one argument (day) and that argument is used in the switch.

The printTime function reads the RTC and creates a String based on the day using dayAsString. Next it uses a character array (buf), places time related information in that buffer (day, t.yr,..) using a specified format (%s %04d...) and next 'prints' the character array on the serial port; this is not strictly necessary but the format specifier allows you to format to your needs; e.g. %02d indicates to print a number with a leading zero. It might be simpler to understand if you use

Serial.print(day);
Serial.print(" ");
Serial.print(t.yr);
Seral.print("-");
Serial.print(t.mon)
...
...

This will not print leading zeroes; you can check if a number is smaller than 10 (e.g. t.min) and print a zeror first before printing the actual number.

switch explained. Note that the code presented is for a PC.
sprintf example. snprintf only differs in the one additional argument to limit the amount of characters stored in the string (lower case s).

Thanks sterretje for the short explanation, that helps a lot.

I'm going to check it out and let you hear somethin asap.