Alternative ways to set operations at a specific time with the DS3231 module when the library is not functional

I have a DS3231 module and I need to set an operation at a specific time, for example, Time 11:30. Unfortunately, the DS3231 library is not functional and I am looking for alternative ways to achieve this goal. Can anyone please advise me on how to do this? Thank you :slightly_smiling_face:.

Example:
When is Time 11:30 Pin 5 Is High (ON).
And when is 11:35 Pin 5 is LOW (OFF)

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

#define DHT_PIN 8
#define PIN_HEATER 5

RTC_DS3231 rtc;
DHT dht(DHT_PIN, DHT21);


void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  dht.begin();
  pinMode(PIN_HEATER, OUTPUT);
}

void loop() {
  DateTime now = rtc.now();
  if (now.hour() == 11 && now.minute() == 53 && now.second() == 0) {  // this part of code
    digitalWrite(7, HIGH);
    delay(60000);
    digitalWrite(7, LOW);
  }
  float temperature = dht.readTemperature();
  if (temperature < 24) {
    digitalWrite(PIN_HEATER, HIGH);
  }  else {
    digitalWrite(PIN_HEATER, LOW);
  }
  delay(1000);
}

Use a library that is "functional"? How is the the time set in the RTC? You have to have a valid time set if you are going to check it. Depending on the library and how you are going to use it you may need more than a simple begin().

Welcome to the forum

What do you mean by this ?
Which Arduino board are you using ?

Works fine for me. What sort of problems are you having?

Post the details after reading the "How to get the best out of this forum" post.

error code when i use ds3231 library:

C:\Users\Hana\AppData\Local\Temp\arduino_modified_sketch_780230\sketch_mar17a.ino: In function 'void setup()':
sketch_mar17a:19:7: error: 'class DS3231' has no member named 'begin'
rtc.begin();
^~~~~
C:\Users\Hana\AppData\Local\Temp\arduino_modified_sketch_780230\sketch_mar17a.ino: In function 'void loop()':
sketch_mar17a:25:11: error: 'class DS3231' has no member named 'getTime'; did you mean 'getA1Time'?
t = rtc.getTime();
^~~~~~~
getA1Time
sketch_mar17a:26:18: error: 'struct tm' has no member named 'hour'; did you mean 'tm_hour'?
Serial.print(t.hour);
^~~~
tm_hour
sketch_mar17a:28:18: error: 'struct tm' has no member named 'min'
Serial.print(t.min);
^~~
sketch_mar17a:33:8: error: 'struct tm' has no member named 'hour'; did you mean 'tm_hour'?
if(t.hour == OnHour && t.min == OnMin){
^~~~
tm_hour
sketch_mar17a:33:28: error: 'struct tm' has no member named 'min'
if(t.hour == OnHour && t.min == OnMin){
^~~
sketch_mar17a:38:15: error: 'struct tm' has no member named 'hour'; did you mean 'tm_hour'?
else if(t.hour == OffHour && t.min == OffMin){
^~~~
tm_hour
sketch_mar17a:38:36: error: 'struct tm' has no member named 'min'
else if(t.hour == OffHour && t.min == OffMin){
^~~
exit status 1
'class DS3231' has no member named 'begin'

the module has registers you can access with I2C protocol.
a read in the datasheet and you can setup your own functions without the need of a library, except maybe the I2C one (usually wire.h).

simple, fancy, home made, with a hint of proud-cuz-u-did-it-yaself.

Arduino Uno

You are using the wrong library. There are many with similar names.

Post your code, using code tags, and a link to where you downloaded the library.

After downloading an Arduino library, always check its functionality by running one of the library examples. The "standard" DS3231 library available in the library manager does not have a .begin() function.

LINK: GitHub - NorthernWidget/DS3231: Communicates between Arduino-programmed AVR and Maxim DS3231 RTC: splice of Ayars' (http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html) and Jeelabs/Ladyada's (https://github.com/adafruit/RTClib) libraries

Code:

#include <DS3231.h>

int Relay = 4;

TwoWire wire;
DS3231 rtc(wire);

tm t;

const int OnHour = 22; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 48;
const int OffHour = 22; //SET TIME TO OFF RELAY
const int OffMin = 49;

void setup() {
  Serial.begin(115200);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
}

void loop() {
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);
  
  if(t.hour == OnHour && t.min == OnMin){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
}

Open the DS3231_test example in that library. You will see that it does not call a .begin() function.

The code you downloaded from somewhere else expects a completely different library.

When I use Wire library its ok. But how i schedule the time

If you have questions about code, post the code, using code tags.

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