DS3231 used without library

Because of the use of the DS3231 in other than Arduino projects (ESP8266) I needed to find a way to use this module without a library; no need for alarms, and the function I need it for calls for action on specific times of day, or even specific days of the week. For that no library would be needed after I found this site: https://tronixlabs.com.au/news/tutorial-using-ds1307-and-ds3231-realtime-clock-modules-with-arduino/
Here is a shortened version, no serial monitor output, just blinking a LED on DS3231 based time.

Please let me know your comments about the use of the various DS3231 related code/statements?

/*
// https://tronixlabs.com.au/news/tutorial-using-ds1307-and-ds3231-realtime-clock-modules-with-arduino/
// uses no library
// test with builtin LED blink: every 30 seconds HIGH, then 30 seconds LOW
// SCL = AD5, SDA = AD4
// Uses state function: execute LED ON or LED OFF command only once

v2: shortened version, no serial output
*/


#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(30,17,15,7,18,11,17);

  pinMode(LED_BUILTIN, OUTPUT);

}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  static byte trigger = 1;

  if (second >= 30) //use second for once a minute, use minute for once an hour
  {
    if (trigger)
    {
    digitalWrite(LED_BUILTIN, HIGH);
    trigger = 0;
    }
    else
    {
      trigger = 1;
    }
    
  }
  else
  {
    if (trigger)
    {
    digitalWrite(LED_BUILTIN, LOW);
    trigger = 0;
    }
    else
    {trigger = 1;
    }
  }


}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second
}

Well, the Wire library doesn't exist outside of the Arduino runtime environment. So just about anything you can compile in it for the ESP is supported. Considering that, I am not sure why you would rule out DS3231 libraries that also compile under it.

So

// uses no library

is kind of a lie.

There are many ESP8266 boards supported in the IDE. I have quickly tried to compile the DS3232.h library sketches for a few of them, and all looks well.

cattledog:
There are many ESP8266 boards supported in the IDE. I have quickly tried to compile the DS3232.h library sketches for a few of them, and all looks well.

Hi cattledog, I know ds3231.h runs with esp8266. It is just that for this specific application I want to avoid the use of any rtc library because I do not want to depend on library-specific commands and maybe too to reduce memory usage.

brice3010:
Hi cattledog, I know ds3231.h runs with esp8266. It is just that for this specific application I want to avoid the use of any rtc library because I do not want to depend on library-specific commands and maybe too to reduce memory usage.

The ESP has gobs of memory.

As a learning exercise or attempt to produce a "light weight" solution, it's not an unreasonable idea. I'd go at it from two directions:

First, read the device datasheet cover to cover. Learn the register set. If it has multiple operating modes, figure out which one you want to use. Understand not only nominal operation, but initialization and exceptions.

Second, look at existing libraries and completely understand how they handle the device. Maybe you could use one as a starting point and chop away unneeded features and remove support for processors that you're not using.

Of course, how far are you going to take this "no library" idea? Are you going to write your own I2C drivers?

The ESP8266 has wifi so you don't even need an RTC. You can simply use NTP.

--- bill

brice3010:
Because of the use of the DS3231 in other than Arduino projects (ESP8266) I needed to find a way to use this module without a library;

I'm not sure what you are saying makes much sense, particularly with an ESP8266 in play. Having said that, I never use a library WITH Arduino projects. I found the following the easiest to implement, any library method seems to manage to waste memory doing the same job, and there has never been any motivation to change.