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

Moderator: igrr

User avatar
By PaulRB
#24168
tytower wrote: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.
Yes, I hope I have cured that issue now - see my earlier post.

tytower wrote: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.

I bought these. I don't know if they are originals or clones. Also I'm no expert on these sensors. I have used DHT11 in the past and the SHT21 seems considerably better, plus it has the i2c interface.
User avatar
By PaulRB
#24171
certeza wrote:This piece of code works reliably for me on an ESP-12 with SDA on pin 4 and SCL on pin 5.


I changed the sketch to use pins 2 (SDA) and 0 (SCL) and got this output:
Code: Select allStarting up...
Looking for SI7021 sensor...
I2C device found at address 0x40  !
Humidity: 118.99
Temperature:
and then it hangs.

I also tried un-commenring the "reset" call in your code and got:
Code: Select allStarting up...
Looking for SI7021 sensor...
I2C device found at address 0x40  !
Device 0x40 reset.
Humidity: 118.99
Temperature:
and then it hangs.

Swapping the reads of temp and humitity over:
Code: Select allStarting up...
Looking for SI7021 sensor...
I2C device found at address 0x40  !
Device 0x40 reset.
Temperature: 128.86
Humidity:
... and hangs.

I tried swapping the SCL and SDA lines over and using "Wire.begin(0,2);". Same result.

So, just to prove the sensor is working, I transfer it to a Nano 3 and change the sketch from "Wire.begin(2,0);" to "Wire.begin();" and immediately it works:
Code: Select allStarting up...
Looking for SI7021 sensor...
I2C device found at address 0x40  !
Device 0x40 reset.
Temperature: 26.24
Humidity: 41.08
done


Can someone please scrutinise my pictures above. Have I wired everything correctly?
User avatar
By PaulRB
#24173
hansg wrote: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 ..


It did!!!

In SparkFunHTU21D.cpp I changed "delay(55);" to "delay(85);" in 2 places and also changed "Wire.begin();" to "Wire.begin(0,2);":
Code: Select all/*
 HTU21D Humidity Sensor Library
 By: Nathan Seidle
 SparkFun Electronics
 Date: September 22nd, 2013
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

 This library allows an Arduino to read from the HTU21D low-cost high-precision humidity sensor.
 
 If you have feature suggestions or need support please use the github support page: https://github.com/sparkfun/HTU21D

 Hardware Setup: The HTU21D lives on the I2C bus. Attach the SDA pin to A4, SCL to A5. If you are using the SparkFun
 breakout board you *do not* need 4.7k pull-up resistors on the bus (they are built-in).
 
 Link to the breakout board product:
 
 Software:
 Call HTU21D.Begin() in setup.
 HTU21D.ReadHumidity() will return a float containing the humidity. Ex: 54.7
 HTU21D.ReadTemperature() will return a float containing the temperature in Celsius. Ex: 24.1
 HTU21D.SetResolution(byte: 0b.76543210) sets the resolution of the readings.
 HTU21D.check_crc(message, check_value) verifies the 8-bit CRC generated by the sensor
 HTU21D.read_user_register() returns the user register. Used to set resolution.
 */

#include <Wire.h>

#include "SparkFunHTU21D.h"

HTU21D::HTU21D()
{
  //Set initial values for private vars
}

//Begin
/*******************************************************************************************/
//Start I2C communication
void HTU21D::begin(void)
{
  Wire.begin(0, 2);
}

//Read the humidity
/*******************************************************************************************/
//Calc humidity and return it to the user
//Returns 998 if I2C timed out
//Returns 999 if CRC is wrong
float HTU21D::readHumidity(void)
{
   //Request a humidity reading
   Wire.beginTransmission(HTDU21D_ADDRESS);
   Wire.write(TRIGGER_HUMD_MEASURE_NOHOLD); //Measure humidity with no bus holding
   Wire.endTransmission();

   //Hang out while measurement is taken. 50mS max, page 4 of datasheet.
   delay(85);

   //Comes back in three bytes, data(MSB) / data(LSB) / Checksum
   Wire.requestFrom(HTDU21D_ADDRESS, 3);

   //Wait for data to become available
   int counter = 0;
   while(Wire.available() < 3)
   {
      counter++;
      delay(1);
      if(counter > 100) return 998; //Error out
   }

   byte msb, lsb, checksum;
   msb = Wire.read();
   lsb = Wire.read();
   checksum = Wire.read();

   /* //Used for testing
   byte msb, lsb, checksum;
   msb = 0x4E;
   lsb = 0x85;
   checksum = 0x6B;*/
   
   unsigned int rawHumidity = ((unsigned int) msb << 8) | (unsigned int) lsb;

   if(check_crc(rawHumidity, checksum) != 0) return(999); //Error out

   //sensorStatus = rawHumidity & 0x0003; //Grab only the right two bits
   rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place
   
   //Given the raw humidity data, calculate the actual relative humidity
   float tempRH = rawHumidity / (float)65536; //2^16 = 65536
   float rh = -6 + (125 * tempRH); //From page 14
   
   return(rh);
}

//Read the temperature
/*******************************************************************************************/
//Calc temperature and return it to the user
//Returns 998 if I2C timed out
//Returns 999 if CRC is wrong
float HTU21D::readTemperature(void)
{
   //Request the temperature
   Wire.beginTransmission(HTDU21D_ADDRESS);
   Wire.write(TRIGGER_TEMP_MEASURE_NOHOLD);
   Wire.endTransmission();

   //Hang out while measurement is taken. 50mS max, page 4 of datasheet.
   delay(85);

   //Comes back in three bytes, data(MSB) / data(LSB) / Checksum
   Wire.requestFrom(HTDU21D_ADDRESS, 3);

   //Wait for data to become available
   int counter = 0;
   while(Wire.available() < 3)
   {
      counter++;
      delay(1);
      if(counter > 100) return 998; //Error out
   }

   unsigned char msb, lsb, checksum;
   msb = Wire.read();
   lsb = Wire.read();
   checksum = Wire.read();

   /* //Used for testing
   byte msb, lsb, checksum;
   msb = 0x68;
   lsb = 0x3A;
   checksum = 0x7C; */

   unsigned int rawTemperature = ((unsigned int) msb << 8) | (unsigned int) lsb;

   if(check_crc(rawTemperature, checksum) != 0) return(999); //Error out

   //sensorStatus = rawTemperature & 0x0003; //Grab only the right two bits
   rawTemperature &= 0xFFFC; //Zero out the status bits but keep them in place

   //Given the raw temperature data, calculate the actual temperature
   float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536
   float realTemperature = (float)(-46.85 + (175.72 * tempTemperature)); //From page 14

   return(realTemperature); 
}

//Set sensor resolution
/*******************************************************************************************/
//Sets the sensor resolution to one of four levels
//Page 12:
// 0/0 = 12bit RH, 14bit Temp
// 0/1 = 8bit RH, 12bit Temp
// 1/0 = 10bit RH, 13bit Temp
// 1/1 = 11bit RH, 11bit Temp
//Power on default is 0/0

void HTU21D::setResolution(byte resolution)
{
  byte userRegister = read_user_register(); //Go get the current register state
  userRegister &= B01111110; //Turn off the resolution bits
  resolution &= B10000001; //Turn off all other bits but resolution bits
  userRegister |= resolution; //Mask in the requested resolution bits
 
  //Request a write to user register
  Wire.beginTransmission(HTDU21D_ADDRESS);
  Wire.write(WRITE_USER_REG); //Write to the user register
  Wire.write(userRegister); //Write the new resolution bits
  Wire.endTransmission();
}

//Read the user register
byte HTU21D::read_user_register(void)
{
  byte userRegister;
 
  //Request the user register
  Wire.beginTransmission(HTDU21D_ADDRESS);
  Wire.write(READ_USER_REG); //Read the user register
  Wire.endTransmission();
 
  //Read result
  Wire.requestFrom(HTDU21D_ADDRESS, 1);
 
  userRegister = Wire.read();

  return(userRegister); 
}

//Give this function the 2 byte message (measurement) and the check_value byte from the HTU21D
//If it returns 0, then the transmission was good
//If it returns something other than 0, then the communication was corrupted
//From: http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html
//POLYNOMIAL = 0x0131 = x^8 + x^5 + x^4 + 1 : http://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks
#define SHIFTED_DIVISOR 0x988000 //This is the 0x0131 polynomial shifted to farthest left of three bytes

byte HTU21D::check_crc(uint16_t message_from_sensor, uint8_t check_value_from_sensor)
{
  //Test cases from datasheet:
  //message = 0xDC, checkvalue is 0x79
  //message = 0x683A, checkvalue is 0x7C
  //message = 0x4E85, checkvalue is 0x6B

  uint32_t remainder = (uint32_t)message_from_sensor << 8; //Pad with 8 bits because we have to add in the check value
  remainder |= check_value_from_sensor; //Add on the check value

  uint32_t divsor = (uint32_t)SHIFTED_DIVISOR;

  for (int i = 0 ; i < 16 ; i++) //Operate on only 16 positions of max 24. The remaining 8 are our remainder and should be zero when we're done.
  {
    //Serial.print("remainder: ");
    //Serial.println(remainder, BIN);
    //Serial.print("divsor:    ");
    //Serial.println(divsor, BIN);
    //Serial.println();

    if( remainder & (uint32_t)1<<(23 - i) ) //Check if there is a one in the left position
      remainder ^= divsor;

    divsor >>= 1; //Rotate the divsor max 16 times so that we have 8 bits left of a remainder
  }

  return (byte)remainder;
}


And loaded the example sketch that came with that library:
Code: Select all/*
 HTU21D Humidity Sensor Example Code
 By: Nathan Seidle
 SparkFun Electronics
 Date: September 15th, 2013
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
 
 Uses the HTU21D library to display the current humidity and temperature
 
 Open serial monitor at 9600 baud to see readings. Errors 998 if not sensor is detected. Error 999 if CRC is bad.
 
 Hardware Connections (Breakoutboard to Arduino):
 -VCC = 3.3V
 -GND = GND
 -SDA = A4 (use inline 330 ohm resistor if your board is 5V)
 -SCL = A5 (use inline 330 ohm resistor if your board is 5V)

 */

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

//Create an instance of the object
HTU21D myHumidity;

void setup()
{
  Serial.begin(115200);
  Serial.println("HTU21D Example!");

  myHumidity.begin();
}

void loop()
{
  float humd = myHumidity.readHumidity();
  float temp = myHumidity.readTemperature();

  Serial.print("Time:");
  Serial.print(millis());
  Serial.print(" Temperature:");
  Serial.print(temp, 1);
  Serial.print("C");
  Serial.print(" Humidity:");
  Serial.print(humd, 1);
  Serial.print("%");

  Serial.println();
  delay(1000);
}


And now the output is:
Code: Select allTime:22896 Temperature:25.1C Humidity:44.6%
Time:24068 Temperature:25.1C Humidity:44.7%
Time:25239 Temperature:25.1C Humidity:44.7%
Time:26411 Temperature:25.1C Humidity:44.8%
Time:27582 Temperature:25.1C Humidity:44.9%
Time:28753 Temperature:25.1C Humidity:44.9%
Time:29930 Temperature:25.1C Humidity:45.0%
Time:31101 Temperature:25.1C Humidity:45.1%
Time:32273 Temperature:25.1C Humidity:45.2%
Time:33444 Temperature:25.1C Humidity:45.2%
Time:34616 Temperature:25.1C Humidity:45.2%
Time:35787 Temperature:25.0C Humidity:45.2%


Thankyou hansg!

So... what makes that work when the other attempts did not?
User avatar
By networx
#37787 Is there anybody else who got this working?

No chance for me, HTU21D board from SparkFun and NodeMCU with ESP12e and all i got is:

Time:460996 Temperature:998.0C Humidity:998.0%

I also have tried the demo sketch from cactus.io, an different GPIOs, all the same. "Can't find sensor" or esp restarts. So i cross checked with an Arduino Nano and it works.

Anybody here who has ESP12e or Nodemcu and HTU21D working with one of the demo sketches?