Problem with Arduino with RTC and OLED display

I’m trying to use an OLED display with an RTC but when I enable both any of it works… If I enable just RTC, ok! or just OLED display, ok! Both are using SDA and SCL pins.

The code is here:

#include <DHT.h>
#include <U8g2lib.h>
#include <RtcDS1307.h>
#include <Wire.h>

#define portaVolt A0
#define DHTTYPE DHT11
#define DHTPIN A1

DHT dht(DHTPIN, DHTTYPE);
U8G2_SSD1306_128X32_UNIVISION_1_SW_I2C u8g2(U8G2_R0, SCL, SDA, 0);
RtcDS1307 Rtc(Wire);

void setup() {
pinMode(portaVolt,INPUT);
Serial.begin(9600);
//Wire.begin();
//Rtc.Begin();
u8g2.begin();
}

void loop() {
//RtcDateTime now = Rtc.GetDateTime();
float ler = analogRead(portaVolt);
float volts = ((ler*5)/1023)*3.5;
String sVolts = String(volts);
String msgVolts = "Volts: " + sVolts;
char msgCVolts[20];
msgVolts.toCharArray(msgCVolts,20);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0,15,msgCVolts);
}while ( u8g2.nextPage() );
delay(5000);
float h = dht.readHumidity();
float t = dht.readTemperature();
String sHumid = String(h);
String sTemp = String(t);
String msgTemp = "Temp.: " + sTemp;
String msgHumid = "Hum.: " + sHumid + “%”;
char msgSTemp[40];
char msgSHumid[40];
msgTemp.toCharArray(msgSTemp,40);
msgHumid.toCharArray(msgSHumid,40);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0,14,msgSTemp);
u8g2.drawStr(0,32,msgSHumid);
}while ( u8g2.nextPage() );
delay(5000);

}

What I’m doing wrong? Or what could I do to solve this issue?

Thanks!!!

I don't know how either of them worked. Normal practice is to define the addresses of the items on the I2C bus, typically
#define DS1307_ADDRESS 0x68 and I guess your display similarly.
You do not seem to be doing this for either of them.

Nick_Pyner:
I don't know how either of them worked. Normal practice is to define the addresses of the items on the I2C bus, typically
#define DS1307_ADDRESS 0x68 and I guess your display similarly.
You do not seem to be doing this for either of them.

The interesting thing is: in both of these libraries (U8g2lib and RtcDS1307) there is no option or a way to set those addresses...
I really don't know how to set it... I searched for the addresses and the RTC uses 0x68 and the OLED uses 0x3C.
But I have no ideia how to set it...

hawkbraz:
The interesting thing is: in both of these libraries (U8g2lib and RtcDS1307) there is no option or a way to set those addresses...

I think that is because it is not in the libraries, it is done on the setup of your programme.
Any I2C RTC example will show you, and you can be sure any OLED example will too.

Nick_Pyner:
I think that is because it is not in the libraries, it is done on the setup of your programme.
Any I2C RTC example will show you, and you can be sure any OLED example will too.

Not quite. Some RTC ICs have fixed I2C addresses, so there is no point in specifying them. An RTC object is often created for you in the class file so you don't need to specify any address.

U8G2_R0 might be the default OLED I2C address that is supplied by the library. If so, it can easily be changed. But if the module works by itself, then obviously it is not wrong. You could print it to find out if it conflicts with the RTC address.

@hawkbraz

Please use HW I2C:

U8G2_SSD1306_128X32_UNIVISION_1_HW_I2C u8g2(U8G2_R0);

This question is also discussed in the U8g2 FAQ: u8g2/faq.txt at master · olikraus/u8g2 · GitHub

The interesting thing is: in both of these libraries (U8g2lib and RtcDS1307) there is no option or a way to set those addresses...

It is possible for u8g2, see the manual:

Oliver

olikraus:
@hawkbraz

Please use HW I2C:

U8G2_SSD1306_128X32_UNIVISION_1_HW_I2C u8g2(U8G2_R0);

This question is also discussed in the U8g2 FAQ: u8g2/faq.txt at master · olikraus/u8g2 · GitHub

It is possible for u8g2, see the manual:
u8g2reference · olikraus/u8g2 Wiki · GitHub

Oliver

Problem solved!!!! Thank you... It's amazing how a simple S -> H exchange can solve a problem... LoL

I'm gonna study the FAQ you shared, thank you very much again!