SHT temperature + SSd1306 OLED not working

Hi

When using only SHT or only OLED it works, but when i use code as below i get no sensor reading. Not on display and not on in serial monitor...

#include <Arduino.h>
#include "Adafruit_SHT31.h"
#include <Wire.h>  
#include "SSD1306.h" 

SSD1306  display(0x3c, D3, D5);

Adafruit_SHT31 sht31 = Adafruit_SHT31();

void setup(){

  Serial.begin(9600);

  while (!Serial)
    delay(10);     

  Serial.println("SHT31 test");
  if (! sht31.begin(0x44)) {   // Set to 0x45 for alternate i2c addr
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }
  // Initialising the UI will init the display too.
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_16);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  
}

void displayTempHumid(){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = sht31.readHumidity();
  // Read temperature as Celsius
  float t = sht31.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)){
    display.clear(); // clearing the display
    display.drawString(5,0, "Failed SHT31");
    return;
  }
  display.clear();
  display.drawString(0, 0, "Humidity: " + String(h) + "%\t"); 
  display.drawString(0, 16, "Temp: " + String(t) + "C");  
}

void loop() {
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();

  if (! isnan(t)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.println(t);
  } else { 
    Serial.println("Failed to read temperature");
  }
  
  if (! isnan(h)) {  // check if 'is not a number'
    Serial.print("Hum. % = "); Serial.println(h);
  } else { 
    Serial.println("Failed to read humidity");
  }
  Serial.println();
  displayTempHumid();
  display.display();
  
  delay(1000);
}

why do you include Wire.h twice? (it's not your bug but to get you thinking)

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

J-M-L:
why do you include Wire.h twice? (it's not your bug but to get you thinking)

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Thanks, point taken :slight_smile:

Adafruit_SHT31 sht31 = Adafruit_SHT31();

Another example of crappy code from Adafruit.

Adafruit_SHT31 sht31;

is all that is needed.

  while (!Serial)
    delay(10);

Which Arduino are you using? Do you NEED this? You most certainly not need the stupid delay().

    while (1) delay(1);

Another stupid delay().

  display.setTextAlignment(TEXT_ALIGN_LEFT);
 
}

Personally, I'd actually print something to the display, so that I could KNOW that it is working, with the other hardware disconnected, and then again with it connected.

void displayTempHumid(){

That is a stupid name for a function that does far more than what that name says.

Why did you replicate the code from that function in loop()? You COULD have just added Serial.print() calls to that function.

Hi, same problem here, did you solved your problem ?

It seems to be a compatibility problem between "Adafruit_SHT31.h" and "SSD1306.h" . I tried another SHT31 libraries and tried to change the address of the SHT to 0x45 but without any success.

It is possible to read values from the SHT until the "display.init();" ....
After this function something is broken for reading the SHT... Probably about I2C address but I not able to understand what...

If someone want to help :
The SHT library here :

The SSD1306 library :

Not really solved but a working workaround by using I2C with brzo_i2c instead of I2C with Wire.h for the OLED screen.
So SHT31D use wire.h and the OLED use brzo_i2c.h.

PaulS:

  while (!Serial)

delay(10);



Which Arduino are you using? Do you NEED this? You most certainly not need the stupid delay().



while (1) delay(1);



Another stupid delay().

On an ESP8266 those delays are actually not stupid. they'll ensure the dog does not bark (calling yield()).