0

I'm using RTC DS1307 and OLED on an Arduino MEGA together.

When I try to use a rtc code like rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); or DateTime now = rtc.now(); the OLED display starts not showing what i wish in "void loop".

The summary of the code is below:

#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,"EepromA");
  u8g2.setCursor(55,12);
  //u8g2.print(EepromA);
  u8g2.setCursor(100,12);
  u8g2.print("EepromA");
  u8g2.drawUTF8(0,22,"EepromB");
  u8g2.setCursor(55,22);
 // u8g2.print(EepromB);
  u8g2.setCursor(100,22);
  u8g2.print("EepromB");
  u8g2.drawUTF8(0,32,"EepromC");
  u8g2.setCursor(55,32);
//  u8g2.print(EepromC);
  u8g2.setCursor(100,32);
  u8g2.print("EepromC");
  u8g2.sendBuffer(); 
      delay(50);   

}

It seems like it is about i2c of Arduino Mega but how to solve the problem? Note: After a search on the website I found some similar problems, I have removed R2 and R3 pull-up resistors on RTC module, this did not solve it.

  • Does it display "WELCOME"? – geometrikal Apr 9 at 12:37
  • @geometrikal, yes it displays. – yenice Apr 9 at 15:36
  • This means the hardware and pullups are not an issue and the I2C clock is good. It might be that you are filling the I2C buffer too quickly. Try adding a longer delay in the loop. – geometrikal Apr 10 at 11:55
  • In addition, when i uncomment OLED codes, the RTC also works fine. – yenice Apr 10 at 12:06
  • So either RTC or OLED works, but never at the same time? – geometrikal Apr 10 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.