OLED SSD1306 and DS1307 Problem and Fix it

Posted on

OLED and RTC I2C Problem

I2C SSD1306 OLED and DS1307 use the same communication as I2C. When both of these run, there is an error on the RTC reading but not on the OLED. Therefore, in this article, I will share how to fix this problem in the communication between SSD1306 OLED and DS1307 RTC via I2C.

The SSD1306 is a controller for OLED displays that can communicate with a microcontroller using the I2C protocol. Some features of the SSD1306 OLED display include:

  • High contrast and wide viewing angle
  • Low power consumption
  • Small form factor
  • Easy to interface with microcontrollers using I2C
  • Can display text and graphics
  • Available in various sizes

The DS1307 is a real-time clock (RTC) IC (integrated circuit) that provides a clock and calendar with a built-in battery backup. Some features of the DS1307 include:

  • Real-time clock (RTC) and calendar function with a built-in quartz crystal
  • 56 bytes of non-volatile RAM for data storage
  • I2C interface for communication with a microcontroller
  • Provides date and time information (seconds, minutes, hours, day, date, month, and year)
  • 24-hour or 12-hour format with AM/PM indicator
  • Leap year compensation valid up to 2100
  • Automatic power-fail detect and switch circuit
  • Operating voltage range of 3V to 5.5V
  • Easy to interface with microcontroller

Why did the Error occur?

From what I understand, based on the explanation from the u8g2 forum, it is explained that by default, the SSD1306 uses an I2C bus clock speed of 400Khz, while the RTC DS1307 based on the datasheet only has a maximum of 100KHz. So with this condition, there are some corrupt values on the RTC DS1307.


How to fix it

One way to fix this is to equalize the I2C Bus Clock frequency used by these two components at 100KHz. In the u8g2 library, there is a command that we can use to set the I2C bus clock speed is setBusClock(), so that it can be set at 100KHz to 300KHz with the command:

  u8g2.setBusClock(100000);

The command is placed inside setup() and before u8g2.begin(). You can see more details in the following line of code:

void setup()
{
  u8g2.setBusClock(100000);
  u8g2.begin();  
  rtc.begin();
}

The result

Below are two images that are the result of the two-component program code. The first is before fixing the bus clock speed. You can see the data obtained is random. Whereas the second one after adding the command to set the I2C bus clock speed is the correct value.


Done. Hopefully this u8g2 SSD1306 OLED and DS1307 Problem and Fix it article can be useful.

Read more:

Leave a Reply

Your email address will not be published. Required fields are marked *