SHT22 and sudden changes / offset

Hi,

I was using a bme280 to read exterior temperature and humidity but after a month a very humid period the sensor died.

To replace it I get a SHT20 sensor supposed to be for exterior/soil and modified the program.

It run very nicely for a little while and then after few hours the temperature goes crazy:

Then I reboot the Arduino and temperatures goes immediately right.

Humidity is fine.

It's like the temperature suddently have an offset of smth like 50 degrees.

No external factor.

Any clue why ?

Sensor model

Thanks.

Here the code I use for this sensor (an extract from the code) :

#include <Wire.h>
#include "DFRobot_SHT20.h"

DFRobot_SHT20    sht20;

float humd ;                
float temp ;

void setup()
{

    sht20.initSHT20();                                  // Init SHT20 Sensor
    delay(100);
    sht20.checkSHT20();                                 // Check SHT20 Sensor
}

void loop()
{
    humd = sht20.readHumidity();                  // Read Humidity
    temp = sht20.readTemperature();               // Read Temperature
    delay(1000);
}

If you read the device's reviews it would seem many go wrong :frowning:

@Keisar
Problems may be in the part of the code that you did not show, so always post the full code

Okay, here it is :

/************************* Library ***************************/
#include <SPI.h>
#include "Adafruit_MQTT_Client.h"
#include <Ethernet.h>
//#include <Adafruit_BME280.h>
#include <SD.h>
#include "DFRobot_SHT20.h"
#include <Wire.h>

/************************* Ethernet Client Setup *****************************/
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC};
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

/************************* SD *********************************/
const int chipSelect = 4;

/************************* MQTT brokers *********************************/
#define Local_SERVER      "192.168.1.65"
#define Local_SERVERPORT  1883
#define Local_USERNAME    "xxxxxxx"
#define Local_KEY         "xxxxxxx"

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "xxxxxxxx"
#define AIO_KEY         "xxxxxxxx"

/************************* Time global *********************************/
unsigned long action_AIO = 0;
unsigned long action_Local = 0;
unsigned long action_SD = 0;
#define intervale_publication_AIO 10000 //Interval de publication sur le réseau internet
#define intervale_publication_Local 5000 //Interval de publication sur le réseau local
#define intervale_publication_SD 1800000 //Interval de publication sur la carte SD
unsigned long temps = 0;



/************************* Array & tests *********************************/
#define interval_array 3600000         // Time between update of each value
#define interval_update_array 5000  // Time between each update of the same value of the array 
#define size_array 10                // Number of value of the array 

long time_array = 0 ;
long time_update =0 ; 
byte index = 1 ;
float temp_max_tempo ; 
float temp_min_tempo ; 
float array_max [size_array];
float array_min [size_array];
float temperature_loop ; 
float global_max_val ;
float global_min_val ;

/************ Global State ******************/

//Set up the ethernet client
EthernetClient client;
EthernetClient client2;
Adafruit_MQTT_Client mqtt_Local(&client, Local_SERVER, Local_SERVERPORT, Local_USERNAME, Local_KEY);
Adafruit_MQTT_Client mqtt_AIO(&client2, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1);  }


/************************* Input Setup *********************************/

//#define adresseI2CduBME280  0x76          
//Adafruit_BME280 bme;
//unsigned long action = 0;
DFRobot_SHT20    sht20;

/****************************** Feeds ***************************************/

Adafruit_MQTT_Publish Local_publi_temperature = Adafruit_MQTT_Publish(&mqtt_Local,  Local_USERNAME "/feeds/temperature");
Adafruit_MQTT_Publish Local_publi_humidite = Adafruit_MQTT_Publish(&mqtt_Local,  Local_USERNAME "/feeds/humidite");
Adafruit_MQTT_Publish Local_publi_max_temp = Adafruit_MQTT_Publish(&mqtt_Local,  Local_USERNAME "/feeds/max_temp");
Adafruit_MQTT_Publish Local_publi_min_temp = Adafruit_MQTT_Publish(&mqtt_Local,  Local_USERNAME "/feeds/min_temp");

Adafruit_MQTT_Publish AIO_publi_temperature = Adafruit_MQTT_Publish(&mqtt_AIO,  AIO_USERNAME "/feeds/temperature");
Adafruit_MQTT_Publish AIO_publi_humidite = Adafruit_MQTT_Publish(&mqtt_AIO,  AIO_USERNAME "/feeds/humidite");
Adafruit_MQTT_Publish AIO_publi_max_temp = Adafruit_MQTT_Publish(&mqtt_AIO,  AIO_USERNAME "/feeds/max_temp_2");
Adafruit_MQTT_Publish AIO_publi_min_temp = Adafruit_MQTT_Publish(&mqtt_AIO,  AIO_USERNAME "/feeds/min_temp");

/*------------------------------------ Setup ------------------------------------*/


void setup() 
{
  Serial.begin(9600);
  Serial.println(F("Debut programme"));
  Serial.print(F("\nInit the Client..."));
  Ethernet.begin(mac);
  delay(500); //give the ethernet time to initialize
  //bme.begin(adresseI2CduBME280);
  sht20.initSHT20();                                  
  delay(100);
  sht20.checkSHT20();    

  
  temp_min_tempo = sht20.readTemperature() ; 
  temp_max_tempo = sht20.readTemperature() ; 
  temp_min_tempo = sht20.readTemperature() ;  
  temp_max_tempo = sht20.readTemperature() ;  
  global_max_val = sht20.readTemperature() ; 
  global_min_val = sht20.readTemperature() ;
  for (int i=0; i < size_array; i++)
   {
    array_max [i] = sht20.readTemperature();
    array_min [i] = sht20.readTemperature();
   }

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    return;
  }

  Serial.println(F("Setup all good"));
  }




/*------------------------------------ Loop ------------------------------------*/

void loop() 
{

  temps = millis();
  temperature_loop = sht20.readTemperature();
  global_max_val = -1000;
  global_min_val = 1000;
  MQTT_AIO_connect() ;
  MQTT_Local_connect() ;



    if((millis() - time_update) > interval_update_array) // Update of the current array position if temperature > / <
    {
      //Serial.print("Check value #");Serial.println(index);

      if (temp_max_tempo < temperature_loop)
        {
          array_max[index] = temperature_loop;
          temp_max_tempo = temperature_loop; 
          //Serial.println("UPDATE");
        }
        
        if (temp_min_tempo > temperature_loop)
        {
          array_min[index] = temperature_loop;
          temp_min_tempo = temperature_loop; 
        }
        for (int i=0; i < size_array; i++)  // Check what is the biggest vallue in both array    
        {
          if (array_max[i] > global_max_val)
          {
            global_max_val=array_max[i];
          }
          if (array_min[i] < global_min_val)
          {
            global_min_val=array_min[i];
          }
        }

        time_update = millis(); 
    }   
    
  if((millis() - time_array) > interval_array) //Switch of array position and reset of old values
    {
      index = index + 1; 
      if(index >= size_array) 
       {
         index = 0 ;
       }

      temp_max_tempo = temperature_loop; ;
      temp_min_tempo = temperature_loop; ;
      array_max[index] = temperature_loop;
      array_min[index] = temperature_loop;

      time_array = millis(); 


                /*Serial.println("");
                Serial.println("Array maximum : ");
                for (int i=0; i < size_array; i++)
                {
                  Serial.print(" - ");          
                  Serial.print("Value :");
                  Serial.print(i);
                  Serial.print(" : ");
                  Serial.print(array_max[i]);
                  if (i == size_array-1){Serial.println("");}
                }
                  Serial.print("Maximum value is : ");Serial.println(global_max_val);

                Serial.println("");
                Serial.println("Array minimum : ");
                for (int i=0; i < size_array; i++)
                {
                  Serial.print(" - ");          
                  Serial.print("Value :");
                  Serial.print(i);
                  Serial.print(" : ");
                  Serial.print(array_min[i]);
                  if (i == size_array-1){Serial.println("");}
                }
                  Serial.print("Minimum value is : ");Serial.println(global_min_val);
                Serial.println("");*/
    }   

    if ( ( temps - action_Local ) > intervale_publication_Local ) //Publication toutes les x secondes sur le MQTT Local
    {
 
        action_Local = temps ;
        Local_publi_temperature.publish(sht20.readTemperature()) ;
        Local_publi_humidite.publish(sht20.readHumidity()) ;
        Local_publi_max_temp.publish((float)global_max_val);
        Local_publi_min_temp.publish((float)global_min_val);

    }

    if ( ( temps - action_AIO ) > intervale_publication_AIO ) //Publication toutes les x secondes sur le MQTT Internet
    {
 
        action_AIO = temps;
        AIO_publi_temperature.publish(sht20.readTemperature()) ;
        AIO_publi_humidite.publish(sht20.readHumidity()) ;
        AIO_publi_max_temp.publish((float)global_max_val);
        AIO_publi_min_temp.publish((float)global_min_val);
 
    }

    if ( ( temps - action_SD ) > intervale_publication_SD ) //Publication toutes les x secondes sur la carte SD
    {

      File dataFile = SD.open("Temp_log.txt", FILE_WRITE);
      if (dataFile) 
      {
        dataFile.println(temperature_loop);
        dataFile.close();
        Serial.println("Ecriture SD");

      }
      else 
      {
        Serial.println("error opening datalog.txt");
      }
    }

  if(! mqtt_AIO.ping()) // ping the server to keep the mqtt connection alive
  {
    mqtt_AIO.disconnect();
  }

  if(! mqtt_Local.ping()) 
  {
    mqtt_Local.disconnect();
  }

}

/************************* Voids *****************************/


void MQTT_AIO_connect() 
{
  int8_t ret;

  // Stop if already connected.
  if (mqtt_AIO.connected()) 
  {
    return;
  }

  Serial.print("Connecting to MQTT AIO... ");

  while ((ret = mqtt_AIO.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt_AIO.connectErrorString(ret));
       Serial.println("Retrying MQTT AIO connection in 5 seconds...");
       mqtt_AIO.disconnect();
       delay(5000);  // wait 5 seconds
  }
  Serial.println("MQTT AIO Connected!");
}


void MQTT_Local_connect() 
{
  int8_t ret2;

  // Stop if already connected.
  if (mqtt_Local.connected()) 
  {
    return;
  }

  Serial.print("Connecting to MQTT Local... ");

  while ((ret2 = mqtt_Local.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt_Local.connectErrorString(ret2));
       Serial.println("Retrying MQTT Local connection in 5 seconds...");
       mqtt_Local.disconnect();
       delay(5000);  // wait 5 seconds
  }
  Serial.println("MQTT Local Connected!");
}

This morning again the temperatures went cray, I unplug the sensor itself from the cable and put i back 30 secs and all good.

Is there a way the sensor itself overheat ?

I don't see anything incorrect in your code...

Does resetting also make it working fine?

When I reset or unscrew that bit and unplug / plug the sensor it work fine for a while.

may be a simple way to solve it is to use resetFunc(); function ever hour

perhaps just clean the contacts?

It appears to me like you might have a faulty sensor. I've used sht20 in similar packages but I've never seen them do that.

Actually, I have switched from 5v to 3.3v and it runs like a charm since 2 days without any trouble.

Even if the specification mentioned 5v was fine...