RTC and OLED interfering with each other

I am currently trying to get a SSD1315 oled screen to display the time from an RTC however when the oled is initialized and set to begin the RTC starts showing an incorrect date.

My setup is a Seeed studio sensor kit shield on top of a data-logger shield on an Arduino Uno R3.
The data-logger shield looks like this one Logging Recorder Data Logger Module Shield XD-204 for Arduino UNO SD Card,Shield Board but not branded Keyes. It has an SD card reader and an RTC (I believe it is a DS1307). I am using the U8x8 library for the screen and the RTClib for the RTC though I have tried with a different RTC library with the same result.

#include <Arduino.h>
#include <Wire.h>
#include <U8x8lib.h>  //Screen
#include <RTClib.h> //RTC

U8X8_SSD1306_128X64_NONAME_HW_I2C Oled(U8X8_PIN_NONE); //As used by Grove
RTC_DS1307 rtc;

bool oled = true; //When true screen is on but time is bugged. When False time is correct.

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

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

  if (oled) {
    Oled.begin();
    Oled.setFlipMode(true);
  }
}

void loop() {
  DateTime now = rtc.now();
  Serial.println(now.timestamp());
  if (oled) {
    Oled.setFont(u8x8_font_chroma48medium8_r);
    Oled.setCursor(0, 0);
    Oled.println(now.timestamp(2));  //Date
    Oled.println(now.timestamp(1));  //Time
  }
  delay(1000);
}

Googleing has shown other people with the same issue but there is either no fix or the fix is something I have tried and has not worked.

Did you go through the instructions on how to set the RTC initially and then remove /comment out that reference to reload?

Yes, it shows the correct date as long as oled.begin() is not run.

Try adding the following line that lowers the I2C bus clock to 100KHz from the normal 400KHz. The DS1307 does not like the higher speed.

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

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

  if (oled) {
    Oled.setBusClock(100000UL); //add this line
    Oled.begin();
    Oled.setFlipMode(true);
  }
}

< edit > moved setBusClock() inside of "if (oled)"

That's done it!
Thank you very much!

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