Using RTC DS1307 and OLED together in a code

Hi there,

I'm using RTC 1307 and OLED on a Arduino MEGA together.

When I try to set the time with

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

in the void.setup() then upload the code,i cannot see at OLED display what I wrote in "void loop()". On OLED display i wish something else than time.now() or etc. Just another variables.

I thought that it might be about I2C issue and set Serial.begin() with different bautrates. It didnot help.

Since the code is very long to share, i shared the logic of the problem :slight_smile:

Thanks from now,
Best regards,

jentzsch:
i shared the logic of the problem :slight_smile:

It doesn't help to do that.

jentzsch:
Since the code is very long to share

So prune it back to the bare minimum that still shows the problem.

I have a DS1307 and an oled on a strip board right here, so if you post compileable code I can run it.

I didnot gave the whole code since this part seems enough for solving the problem. Because when I uncomment the following part, the code works:

Serial1.begin(57600);
 if (! rtc.isrunning()) {
    Serial1.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
     }
     if (! rtc.begin()) {
    Serial1.println("Couldn't find RTC");
    while (1);
  }
  Serial1.end();
#include "Arduino.h"
#include "U8g2lib.h"
#include "U8x8lib.h"
#include "RTClib.h"
#include "Wire.h"
#include "SPI.h"
U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // Adafruit Feather M0 Basic Proto + FeatherWing OLED
RTC_DS1307 rtc;
void setup() {


  Serial1.begin(57600);
 if (! rtc.isrunning()) {
    Serial1.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
     }
     if (! rtc.begin()) {
    Serial1.println("Couldn't find RTC");
    while (1);
  }
  Serial1.end();
  
  Serial2.begin(9600);
  while (!Serial2) {
    ; // wait for Serial2 port to connect. Needed for native USB port only
  }
  u8g2.begin(); 
  
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_6x13_te);    
  u8g2.drawStr(30, 25, "WELCOME!");
  u8g2.sendBuffer();
  delay(2000);
  Serial2.setTimeout(1100);
}

void loop() {
 u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_6x13_te);
  u8g2.drawUTF8(0,12,"IrmsB:");
  u8g2.setCursor(55,12);
  //u8g2.print(EepromA);
  u8g2.setCursor(100,12);
  u8g2.print("Amper");
  u8g2.drawUTF8(0,22,"Hour:");
  u8g2.setCursor(55,22);
 // u8g2.print(EepromB);
  u8g2.setCursor(100,22);
  u8g2.print("Wh");
  u8g2.drawUTF8(0,32,"MALİYET:");
  u8g2.setCursor(55,32);
//  u8g2.print(EepromC);
  u8g2.setCursor(100,32);
  u8g2.print("TRY");
  u8g2.sendBuffer(); 
      delay(50);   

}

Ah sorry, different libraries from the ones I used for the OLED and the RTC.

No problem, thank you for your reply. Mega SDA SCL pins and 20 21 pins seem also electrically connected. What should i do for working these OLED and DS1307 real time clock together? Any ideas?