Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By PaulRB
#24048 certeza, your suggestion did not help, same result.

I added delay(1); into the empty while loop again. This enabled me to get an incorrect temp and an incorrect humid reading without the watchdog timer resetting. Here's my sketch:
Code: Select all/****************************************************************
 * ReadSHT2x
 *  An example sketch that reads the sensor and prints the
 *  relative humidity to the PC's serial port
 *
 *  Tested with:
 *    - SHT21-Breakout Humidity sensor from Modern Device
 *    - SHT2x-Breakout Humidity sensor from MisensO Electronics
 ***************************************************************/

#include <Wire.h>
#include <SHT2x.h>


void setup()
{
  Serial.begin(115200);
  //delay(5000);
  Serial.println();
  Serial.println("Starting Wire...");
  Wire.begin(2, 0);
  Serial.println("Wire Started");
}

void loop()
{
  Serial.println();
  Serial.print("Humidity(%RH): ");
  Serial.print(SHT2x.GetHumidity());
  Serial.print("     Temperature(C): ");
  Serial.println(SHT2x.GetTemperature());
 
  delay(1000);
}


and here's the modified library code:
Code: Select all/*
  SHT2x - A Humidity Library for Arduino.

  Supported Sensor modules:
    SHT21-Breakout Module - http://www.moderndevice.com/products/sht21-humidity-sensor
   SHT2x-Breakout Module - http://www.misenso.com/products/001
   
  Created by Christopher Ladden at Modern Device on December 2009.
  Modified by Paul Badger March 2010
 
  Modified by www.misenso.com on October 2011:
   - code optimisation
   - compatibility with Arduino 1.0

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <inttypes.h>
#include <Wire.h>
#include "Arduino.h"
#include "SHT2x.h"



/******************************************************************************
 * Global Functions
 ******************************************************************************/

/**********************************************************
 * GetHumidity
 *  Gets the current humidity from the sensor.
 *
 * @return float - The relative humidity in %RH
 **********************************************************/
float SHT2xClass::GetHumidity(void)
{
   return (-6.0 + 125.0 / 65536.0 * (float)(readSensor(eRHumidityHoldCmd)));
}

/**********************************************************
 * GetTemperature
 *  Gets the current temperature from the sensor.
 *
 * @return float - The temperature in Deg C
 **********************************************************/
float SHT2xClass::GetTemperature(void)
{
   return (-46.85 + 175.72 / 65536.0 * (float)(readSensor(eTempHoldCmd)));
}


/******************************************************************************
 * Private Functions
 ******************************************************************************/

uint16_t SHT2xClass::readSensor(uint8_t command)
{
    uint16_t result;

    Wire.beginTransmission(eSHT2xAddress);   //begin
    Wire.write(command);               //send the pointer location
    delay(100);
    Wire.endTransmission();                  //end
    delay(100);

    Wire.requestFrom(eSHT2xAddress, 3);
    while(Wire.available() < 3) {
      delay(1);; //wait
    }

    //Store the result
    result = ((Wire.read()) << 8);
    result += Wire.read();
   result &= ~0x0003;   // clear two low bits (status bits)
    return result;
}

SHT2xClass SHT2x;


Output now shows the following and then hangs:
Code: Select allStarting Wire...
Wire Started

Humidity(%RH): 118.99     Temperature(C): 128.86

User avatar
By hansg
#24052 Here is my solution to get SDT21 running width ESP8266-12

I had the same issus as you .. WDT reset

Now i use the HTU21D lib from SparkFun.
I had to change the the Timing for temperature convert.
The sensirion datasheet : temperatur conversation takes max 85ms at 14bit resolution (default).

change the delay(55) line inside the readTemperature to delay(85)

this did the trick for me , now the Humidity and Temperature readings are OK

hope that helps ..
User avatar
By certeza
#24148 This piece of code works reliably for me on an ESP-12 with SDA on pin 4 and SCL on pin 5.

Looking for SI7021 sensor...
I2C device found at address 0x40 !
Humidity: 58.50
Temperature: 27.30
done

Code: Select all#include <Wire.h>

#define I2C_ADDR 0x40

typedef enum {
    eTempPostHumidityCmd = 0xE0,
    eTempHoldCmd         = 0xE3,
    eRHumidityHoldCmd    = 0xE5,
    eTempNoHoldCmd       = 0xF3,
    eRHumidityNoHoldCmd  = 0xF5,
} HUM_MEASUREMENT_CMD_T;

uint16_t readSensor(byte command)
{
    uint16_t result;

    Wire.beginTransmission(0x40); //begin
    Wire.write(command);          //send the pointer location
    Wire.endTransmission();       //end
   
    delay(100);
   
    Wire.requestFrom(0x40, 3);
    while( Wire.available() < 3 ) {
      delay(10); //wait
    }

    //Store the result
    result = ((Wire.read()) << 8);
    result += Wire.read();
    result &= ~0x0003;   // clear two low bits (status bits)
    return result;
}

float GetHumidity(void)
{
  return (-6.0 + 125.0 / 65536.0 * (float)(readSensor(eRHumidityHoldCmd)));
}

/**********************************************************
 * GetTemperature
 *  Gets the current temperature from the sensor.
 *
 * @return float - The temperature in Deg C
 **********************************************************/
float GetTemperature(void)
{
  return (-46.85 + 175.72 / 65536.0 * (float)(readSensor(eTempHoldCmd)));
}


bool reset(byte address)
{
  Wire.beginTransmission(address);
  Wire.write(0xFE);
  byte error = Wire.endTransmission();
  if (error == 0) {
    return true;
  }
  return false; 
}

void setup()
{
  Serial.begin(115200);
  delay(500);
  Serial.println("\nStarting up...");
 
  Wire.begin(4,5);
  delay(100);
}

void loop()
{
  byte error;

  Serial.println("Looking for SI7021 sensor...");

  Wire.beginTransmission(I2C_ADDR);
  error = Wire.endTransmission();

  if (error == 0)
  {
    Serial.print("I2C device found at address 0x");
    Serial.print(I2C_ADDR,HEX);
    Serial.println("  !"); 
   
//    if (reset(I2C_ADDR)) {
//       Serial.println("Device 0x40 reset.");;
       delay(85);
       Serial.print( "Humidity: " );
       Serial.println( GetHumidity() );
       delay(85);
       Serial.print( "Temperature: " );
       Serial.println( GetTemperature() );
//    }
  }
  else if (error==4)
  {
    Serial.print("Unknow error at address 0x");
    Serial.println(I2C_ADDR,HEX);
  }   
  Serial.println("done\n");

  delay(5000);   // wait 5 seconds for next request
}
User avatar
By tytower
#24152 Get your power right first is what I suggest . Power hungry little buggers they be , they are drawing 250 mA plus in pulses every 100ms I'm told that's why the FTDI cables cant support them properly and that's also a likely cause of resets.

How are you finding the SHT21 ? The first site I visited sold them for $40 . Ebay has one to replace it and the DHT22 for about $7 now . I'm having an issue with the DHT22 that's why I ask.