How to Initialize RTC Communications for Shed Lighting

Hi,

Can you please show us how to initialize communication with a DS3231M RTC Module? My son and I built a shed recently, and we assembled a circuit to control the lights, which consist of 2-12V LED bulbs.

The goal is to turn on the lights at night and turn them off in the morning. Also, to check the battery voltage first, before turning on the lights.

The good news is that the battery-checking part of the program works.

The bad news is that the time-checking part of the program doesn’t. We can’t even get past the Setup.

The Serial Monitor reports, “Unable to find DS3231MM. Checking again in 3s.”

I think the trouble is this line of code: while (!DS3231M.begin())

The complete code is included here:

[code]
/* 
 * 2020 Shed Ligting
 * 
 * This skecth controls the lights in the shed at 10W
 * - turns them on at night and off in the morning.
 * - checks the the battery voltage first. 
 * 
 * Battery:
 * "You should never discharge a "12V" lead acid battery below 10.5V"
 * Reference Post #6: https://forum.arduino.cc/index.php?topic=698935.0 
 * 
 * The circuit:
 * See 10W Shed Wiring Diagram Rev3 for details.
 * 
 * The clock:
 * DS3231M RTC Module.
 * Need libraries to set and check the time.
 * 
 * Attributions:
 * Examples > 01.Basics > ReadAnalogVoltage
 * Examples > 05.Control > IfStatementConditional
 * 
 * created Nov 7, 2020
 * by Leo Di Croce
 * modified Nov 9, 2020
 * by Leo Di Croce
 * modified Nov 21, 2020
 * by Leo Di Croce
 * modified Nov 25, 2020 - Rev 1 uses functions that do the work
 * by Leo Di Croce
 * 
*/

//========================================

// ----------LIBRARIES--------------

#include <DS3231M.h> // Include the DS3231M RTC library

// --------CONSTANTS (won't change)---------------

const int analogPin = A5;    // pin that the sensor is attached to
const int ledPin = 3;       // pin that the LED is attached to
const int threshold = 815;
/*
The threshold is the value below which  battery voltage should not drop.
  It was calculated twice:
  1.  when I measured voltage @ battery = 11.3V
      Read analogValue = 858
      If voltage threshold = 10.5V
      Then analogValue threshold = (10.5 / 11.3) * 858 = 797; say 800
  2.  when I measured volatge @ light = 11.0 V
      Read analogValue = 854
      If voltage threshold = 10.5V
      Then analogValue threshold = (10.5 / 11.0) * 854 = 815
*/

/*******************************************************************************************************************
** For DS3231M RTC Module: Declare all program constants                                                          **
*******************************************************************************************************************/
const uint32_t SERIAL_SPEED        = 9600; ///< Set the baud rate for Serial I/O
const uint8_t  SPRINTF_BUFFER_SIZE =   32; ///< Buffer size for sprintf()
//const uint8_t  LED_PIN             =   13; ///< Arduino built-in LED pin number

//------------ VARIABLES (will change)---------------------

int analogValue = 0;

/*******************************************************************************************************************
** For DS3231M RTC Module: Declare global variables and instantiate classes                                       **
*******************************************************************************************************************/
DS3231M_Class DS3231M;                          ///< Create an instance of the DS3231M Class

//========================================

void setup() {
  Serial.begin(SERIAL_SPEED); // initialize serial communication at 9600 bits per second
  pinMode(ledPin, OUTPUT); // initialize the LED pin as an output

  Serial.print(F("\nStarting Set program\n"));
  Serial.print(F("- Compiled with c++ version "));
  Serial.print(F(__VERSION__));
  Serial.print(F("\n- On "));
  Serial.print(F(__DATE__));
  Serial.print(F(" at "));
  Serial.print(F(__TIME__));
  Serial.print(F("\n"));
  while (!DS3231M.begin())  // Initialize RTC communications
  {
    Serial.println(F("Unable to find DS3231MM. Checking again in 3s."));
    delay(3000);
  } // of loop until device is located
  DS3231M.pinSquareWave();  // Make INT/SQW pin toggle at 1Hz
  Serial.println(F("DS3231M initialized."));
  DS3231M.adjust();  // Set to library compile Date/Time
  Serial.print(F("DS3231M chip temperature is "));
  Serial.print(DS3231M.temperature() / 100.0, 1);  // Value is in 100ths of a degree
  Serial.println(
      "\xC2\xB0"
      "C");
  Serial.println(F("\nEnter the following serial command:"));
  Serial.println(F("SETDATE yyyy-mm-dd hh:mm:ss"));
  
}

//========================================

void loop() {
  analogValue = analogRead(analogPin); // read the value of the battery voltage

  checkVoltage(); // call the functions that do the work
  printValue() ;

  delay(2000);        // delay in between reads for stability
  // PLACEHOLDER - Look at replacing delay() with millis() eventually. Reference: https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
}

//========================================

void checkVoltage() {
  // BEWRARE: The light flickers as the analogValue (i.e. votltage) approaches the threshold value:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH); // if the analog value is high enough, turn on the LED
  }  else {
    digitalWrite(ledPin, LOW);
  }
}

//========================================

void printValue() {
  Serial.println(analogValue);
  /*
  float voltage = analogValue * (12.0 / 1023.0); // convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 12V)
  Serial.println(voltage);
  */  
}

//========================================

void setTheTime() {
  
}

//========================================

void checkTheTime() {
  
}

[/code]

Also, here’s the wiring diagram:

And finally, the original post—and replies—that helped us build the circuit: https://forum.arduino.cc/index.php?topic=698935.msg4697091#msg4697091

Thank you in advance for your help in this regard,
=L=

You need to provide links to the datasheets or product pages of the hardware you mention...

For instance the DS3231M is 3.3V, the Uno is 5V, so unless the module you have does
level shifting on the I2C bus you have possibly fried something.

What happens when you run the DS3231 library example sketches?

MarkT:
You need to provide links to the datasheets or product pages of the hardware you mention...

For instance the DS3231M is 3.3V, the Uno is 5V, so unless the module you have does
level shifting on the I2C bus you have possibly fried something.

Thank you MarkT. Here's a link to the RTC's product info: [DS3231M MEMS Precise RTC -DFRObot](DS3231M-DFRobot MEMS Precise RTC SKU: DFR0641)
Looks like it can handle 5V.

aarg:
What happens when you run the DS3231 library example sketches?

Thank you aarg. Serial Monitor reports, "Couldn't find RTC," when I run the DS3231 library example sketches. I tried both, the ds3231.ino sketch and the DS3231_alarm.ino sketch.

A4 and A5 are the i2c bus pins SDA/SCL.

Try moving the battery reading to a different analog pin.

You should be able to see the device on the bus at 0x68 with this scanner program

// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011

#include <Wire.h>

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

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 8; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

cattledog:
A4 and A5 are the i2c bus pins SDA/SCL.

Try moving the battery reading to a different analog pin.

You should be able to see the device on the bus at 0x68 with this scanner program…

Thank you cattledog. Looks good! I moved the battery reading to a different analog pin and ran the I2C Scanner sketch that you provided…
Serial Monitor now reports:
I2C scanner. Scanning …
Found address: 104 (0x68)
Done.
Found 1 device(s).
Also ran the DS3231M example sketches and they appear to be working fine.
Next step is to continue coding my sketch. Wish me luck!