SHT31 temp/humidity sensor

Hi, I have a SHT31 temp/humidity sensor and want to connect it thru IIC to Lora TTGO ESP32, Somehow the ESP32 fails to read temp and humidity once the OLED is initialised. I am attaching the code which works with SDA/SCL at 21/22 pins and the OLED initialisation commented. Initializing OLED seems to put off the sensor. I guess its about the IIC already used by the OLED. Any leads or examples where SHT31 Lora ESP32 are used or any hint?

type or paste code here #include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SHT31 sht31 = Adafruit_SHT31();

//OLED pins
#define OLED_SDA 4
#define OLED_SCL 15 
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);

void setup()
{
Serial.begin(9600);
if (! sht31.begin(0x44))
{
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}

//reset OLED display via software
//  pinMode(OLED_RST, OUTPUT);
//  digitalWrite(OLED_RST, LOW);
//  delay(20);
//  digitalWrite(OLED_RST, HIGH);
  
  //initialize OLED
//  Wire.begin(OLED_SDA, OLED_SCL);
//  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
//    Serial.println(F("SSD1306 allocation failed"));
//    for(;;); // Don't proceed, loop forever
//  }
}

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

if (! isnan(t))
{
Serial.print("Temp *C = "); Serial.println(t);
}
else
{
Serial.println("Failed to read temperature");
}

if (! isnan(h))
{
Serial.print("Hum. % = "); Serial.println(h);
}
else
{
Serial.println("Failed to read humidity");
}
Serial.println();
delay(1000);
}

No, the entire point of i2c bus is that many devices share the same 2 Arduino pins. The devices must each have a different address on the bus, which in your circuit I'm pretty sure they will.

Please clarify, which pins is the sht31 connected to?

Try moving Wire.begin() before sht31.begin().

The SHT31 is connected : 21/SDA 22/SCL.

when I move the wire.begin() before sht31.begin() then i get this error: "Couldn't find SHT31"

Hi
The SHT31 has 2 I2C addresses.
You select which address to use by its ADDR pin.
I2C address A 0x44 (default) ADDR (pin 2) connected to VSS
I2C address B 0x45 ADDR (pin 2) connected to VDD.

I suggest you run I2CScanner.ino in your project and see what address it will find.

1 Like

Moving that line should not have caused that error. Please post your updated code.

type or paste code heretype or paste code here #include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SHT31 sht31 = Adafruit_SHT31();

//OLED pins
#define OLED_SDA 4
#define OLED_SCL 15 
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);

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

  //initialize OLED
  Wire.begin(OLED_SDA, OLED_SCL);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
 Serial.println(F("SSD1306 allocation failed"));
  for(;;); // Don't proceed, loop forever
}

if (! sht31.begin(0x44))
{
Serial.println("Couldn't find SHT31");
while (1) delay(1);
}

//reset OLED display via software
//  pinMode(OLED_RST, OUTPUT);
//  digitalWrite(OLED_RST, LOW);
//  delay(20);
//  digitalWrite(OLED_RST, HIGH);
  

}

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

if (! isnan(t))
{
Serial.print("Temp *C = "); Serial.println(t);
}
else
{
Serial.println("Failed to read temperature");
}

if (! isnan(h))
{
Serial.print("Hum. % = "); Serial.println(h);
}
else
{
Serial.println("Failed to read humidity");
}
Serial.println();
delay(1000);
}

Ah, I see. The error is coming from the esp, it is not a compile error.

Try i2c scanner sketch as suggested by @ruilviana

Have you tried any of the example sketches that were installed with the Adafruit_SSD1306 library? If they do not work, disconnect the sht31 and try again.

The above code with i2C address 0x44 works with the wire.begin() commented. Why would that conflict with Wire.begin()?

This might be due to I2C conflict, you can check the detailed tutorial on displaying the data on its own OLED display here: Interfacing SHT31 with ESP32 TTGO LoRa32 and display on its OLED display

Thanks. I will try and get back to you soon.

I tried but it shows a hazy screen on oled and on the serial it shows blank. when i rst the ttgo it gives "Couldn't find SHT31". The same ttgo and i run a short Adafruit program that does not involve oled and with gpio 21 and 22 and i get temperature and humidity on the serial.


Which SHT31 is a direct connect for the Davis Instruments Advantage Pro 2 transmitter. Mine was manufactured in 2020. The humidity and temp sensor went haywire. I purchased an SHT31 board on Amazon. (Not the Davis one) Does not work. It is connected correctly. My older one was the SHT10 of which I just purchased the IC and replaced it. It worked for a few years till transmitter went haywire also.

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