DS1306 Oled clock set time?

Pro mini 5v
Arduino - OLED Clock | Arduino Tutorial (arduinogetstarted.com)

How to set the time
what does this even mean?

  // automatically sets the RTC to the date & time on PC this sketch was compiled
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

// It’s commented out, so it means nothing…

But, if it’s uncommented, the program will one-time (for testing only), set the RTC to match the current time of day when the program was ‘compiled’.

If you want to set the date & time at any other occasion, you’ll need to put some code together to set the values - hours, minutes, seconds etc that are being ‘set’

What RTC library are you using?

Adafruit Rtc 1307

The program is working with his time in it.
Usually you set the //(day, sec min hr day mo yr) so i need that or however they do it now.
His time does not add preceding zeros

Try google:

 arduino print leading zero rtc

The idea of the original sketch is that you uncomment the line that sets the clock, upload and run that code then

Re-comment the line that sets the clock and upload and run that for the rest of eternity or until you move time zones or until your backup battery dies.

a7

I had to do that for a weather station I'm building:

void Time() {
  DateTime now = rtc.now();
  tft.setTextSize(3);
  tft.setTextColor(WHITE);
  if (now.day() != prevDay)
  {
    prevDay = now.day();
    tft.fillRect(5, 3, 33, 21, BLACK);
    tft.setCursor(5, 3);
    if (now.day() < 10)
    {
      tft.print("0");
    }
    tft.print(now.day());
    if (displayScreenNo == 1) {
      moonPhase();
    }
    if (displayScreenNo == 2) {
      sun();
    }
  }
  tft.setCursor(41, 3);
  tft.setTextSize(3);
  tft.print("/");
  if (now.month() != prevMonth)
  {
    prevMonth = now.month();
    tft.fillRect(59, 3, 33, 21, BLACK);
    tft.setCursor(59, 3);
    if (now.month() < 10) {
      tft.print("0");
    }
    tft.print(now.month());
  }
  tft.setCursor(95, 3);
  tft.setTextSize(3);
  tft.print("/");
  if (now.year() != prevYear)
  {
    prevYear = now.year();
    tft.fillRect(113, 3, 70, 21, BLACK);
    tft.setCursor(113, 3);
    tft.print(now.year());
  }
  if (now.hour() != prevHour)
  {
    prevHour = now.hour();
    tft.fillRect(206, 3, 33, 21, BLACK);
    tft.setCursor(206, 3);
    if (now.hour() < 10) {
      tft.print("0");
    }
    tft.print(now.hour());
  }
  tft.setCursor(239, 3);
  tft.print(":");
  if (now.minute() != prevMin)
  {
    prevMin = now.minute();
    tft.fillRect(256, 3, 33, 21, BLACK);
    tft.setTextSize(3);
    tft.setCursor(256, 3);
    tft.setTextColor(WHITE);
    if (now.minute() < 10) {
      tft.print("0");
    }
    tft.print(now.minute());
    if (displayScreenNo == 2) {
      HeatIndex();
    }
    tft.setCursor(290, 3);
    tft.setTextSize(3);
    tft.print(":");
    if (now.second() != prevSec)
    {
      prevSec = now.second();
      tft.fillRect(306, 3, 33, 21, BLACK);
      tft.setCursor(306, 3);
      if (now.second() < 10) {
        tft.print("0");
      }
      tft.print(now.second());
    }
    if (displayScreenNo == 1) {
      BME();
    }
  }
  tft.setCursor(290, 3);
  tft.setTextSize(3);
  tft.print(":");
  if (now.second() != prevSec)
  {
    prevSec = now.second();
    tft.fillRect(306, 3, 33, 21, BLACK);
    tft.setCursor(306, 3);
    if (now.second() < 10) {
      tft.print("0");
    }
    tft.print(now.second());
  }
}

That is an example of how to add preceding zeros - there are parts of that code that won't work for you, it isn't meant to work for you but you can see how it is done. You can probably make it a lot simpler, that's just how I did it as it met my purposes.

Add this sort of thing:

rtc.begin();
  if (! rtc.isrunning()) {
    Serial.println(F("RTC is NOT running, let's set the time!"));
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

This code is for the RTClib - same one you are using if you are following that tutorial you linked. So my code should be a drop-in replacement for what you currently have.

Or place it in a if statement as in post #7. Saves time, plus it checks whether it lost time instead of you manually checking.

What? That will set the time and date to when you compiled and uploaded the sketch.

When is the RTC not running? Does adjust start a non-running RTC?

Srsly, any of these methods is just lazy avoidance of providing a means to set the clock "in the wild", and as such should only be used in development and testing.

a7

rtc.begin();

starts the clock. It is then "running" unless it has lost time. If it has lost time, the internal oscillator does not work, and that's why you get a impossible year output from the RTC. The library has a function for asking the RTC if the internal oscillator is working. The library I use had this if statement - if you want to complain about laziness, take it up with the person who created the library - not me.
And please do some basic research on using RTC's. I did an awful lot of research on these quite some time back because I was building my own MCU that had an inbuilt RTC. As such I learnt how they work.

Only if the RTC lost power/failed. Then yes, it will. But it checks first!!
Putting it in the if statement ensures that it doesn't reset the RTC when you reset the MCU.

Yes and thankyou
here's my original code but with a pro mini and an LCD 4x20 that only 2x20 works?#!
(and it draws 150ma)

Problem 1. need it on the OLED
prob2. need to set the time like I used to or learn the new method?

//#include <SPI.h>
#include <Wire.h>
#define DS3231_I2C_ADDRESS 0x68
#include <LCD_I2C.h>
LCD_I2C lcd(0x27);   
/*
  #include <SoftwareSerial.h>
  // Create a software serial port!
  //SoftwareSerial lcd = SoftwareSerial(0, 6);
  //SoftwareSerial lcd(0, 6);
*/

int x = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin(0x40);
  //lcd.begin(9600);
  lcd.begin(false); // Stop 2nd Wire.h call with (false) if multiple i2c devices  
  lcd.backlight();

  // set the initial time here:
  //  DS3231 seconds, minutes, hours, day, date, month, year
   //setDS3231time(00,22,3,1,15,8,22);

}

void loop()   {
  
 
  displayTime(); 
  delay(1000);
  
// lcd.setCursor(3,3);
//  lcd.backlight();
  lcd.clear();
  delay(10);
}

  //CLOCK
// 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 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);
  //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);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  //lcd.print(hour);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute < 10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");

  lcd.write(0xFE); lcd.write(0x58); delay(10); //cls
  lcd.print(hour); lcd.print(":");
  if (minute < 10)
  {
    lcd.print("0");
  }
  lcd.print(minute); lcd.print(":");
  if (second < 10)
  {
    lcd.print("0");
  }
  lcd.print(second);

  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}

What OLED do you have? What library are you using? Have written any working code yet?

Learn the new method. I can add it to this code, but you do need to learn it. It is way more simple...

Here is the LCD library - might work better than the mess you were using. You could also use the hd44780 library (a much better library), though it is a slightly steeper learning curve.
LiquidCrystal_I2C.zip (20.3 KB)

And here is some updated and simplified code. You said you had the RTClib library, so you shouldn't have an issue with it.

[code]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display

#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int x = 0;

void setup() {
  Serial.begin(9600);
  lcd.init();                      // initialize the lcd
  lcd.backlight();

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));         //Uncomment this line ONLY if the power has not been cut to the RTC and you have reason to believe it has lost a significant amount of time.
  // make sure you recomment it after uploading and then reupload while commented out or every time you reset the MCU the time will reset
}

void loop()   {
  displayTime();
  delay(998);

  lcd.clear();
}

void displayTime()
{
  DateTime now = rtc.now();
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  if (now.minute() < 10)
  {
    Serial.print("0");
  }
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  if (now.second() < 10)
  {
    Serial.print("0");
  }
  Serial.print(now.second(), DEC);
  Serial.print(" ");
  Serial.print(now.day(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.year(), DEC);
  Serial.print(" Day of week: ");
  Serial.println(daysOfTheWeek[now.dayOfTheWeek()]);

  lcd.print(now.hour(), DEC);
  lcd.print(":");
  if (now.minute() < 10)
  {
    lcd.print("0");
  }
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  if (now.second() < 10)
  {
    lcd.print("0");
  }
  lcd.print(now.second(), DEC);
}
[/code]
  1. .96" oled
  2. my 4x20 is run on a pcf8574T so it was really hard to find anything to run that
  3. I notice nobody is using the .96 oled SO....... I'll swith back to to the 2x20 LCD, which I have tried and can't get running yet (for some reason)
  4. been running a serial (3pin) lcd for 15 years but it broke and I'm really trying to come up on the pi pico and xiao in C++ arduino. But now this post is for the pro mini, exhaustion lol.
  5. so, what's the new method?
  6. thanks I'll run this one in soon