HYT221 Temperatur / Humidity I2C Sensor

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
mdietinger@gmail.com
Posts: 39
Joined: 12 Aug 2016 12:08

HYT221 Temperatur / Humidity I2C Sensor

Post by mdietinger@gmail.com »

:idea: Hello,

tested the HYT221 high accuracy temperatur and humidity I2C Sensor.
The wiring:
Z-UNO PIN 10 -> PIN1
Z-UNO PIN GND -> PIN2
Z-UNO PIN 3.3V -> PIN3
Z-UNO PIN 9 -> PIN4

The code:

Code: Select all

// demo sketch for connecting HYT221 humidity and temperature sensor to Z-Uno

// add library
#include <Wire.h>   

// set up channels
ZUNO_SETUP_CHANNELS(
        ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, 
                          SENSOR_MULTILEVEL_SCALE_CELSIUS, 
                          SENSOR_MULTILEVEL_SIZE_TWO_BYTES, 
                          SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL,
                          getterTemperature),
     ZUNO_SENSOR_MULTILEVEL_HUMIDITY(getterHumidity)
);

void setup() {
  #define HYT_ADDR 0x28     // I2C address of the HYT 221, 271, 939 and most likely the rest of the family
  Wire.begin();             // Join I2c Bus as master
  Serial.begin(9600);       // Start serial communication for serial console output
  pinMode(13, OUTPUT);
}

int humidity; // here we will store the humidity
word temperature; // here we will store the temperature

void loop() {
  int iData[4];

  Wire.beginTransmission(HYT_ADDR);
  Wire.write(0);    // initiate measurement
  Wire.endTransmission(); 
  
  delay(500);
  
  Wire.beginTransmission(HYT_ADDR);   // Begin transmission with given device on I2C bus
  Wire.requestFrom(HYT_ADDR, 4);      // Request 4 bytes 
  // Read the bytes if they are available
  // The first two bytes are humidity the last two are temperature
  if(Wire.available() == 4) {                   
           iData[1] = Wire.read();
           iData[2] = Wire.read();
           iData[3] = Wire.read();
           iData[4] = Wire.read();
           Wire.endTransmission();           // End transmission and release I2C bus
              
           // combine humidity bytes and calculate humidity
           int rawHumidity = iData[1] << 8 | iData[2];
           rawHumidity =  (rawHumidity &= 0x3FFF);
           humidity = 100.0 / pow(2,14) * rawHumidity;
             
        Serial.print("Humidity: ");
        Serial.print(humidity);
             
          // combine temperature bytes and calculate temperature
          iData[4] = (iData[4] >> 2); // Mask away 2 least significant bits see HYT 221 doc
          int rawTemperature = iData[3] << 6 | iData[4];
          temperature = (165.0 / pow(2,14) * rawTemperature - 40)*10; 
        
        Serial.print("% - Temperature: ");
        Serial.print(temperature/10);
        Serial.println("C");

  }
  else {
        Serial.println("Not enough bytes available on wire.");
  }
  zunoSendReport(1);
  zunoSendReport(2);  
  blinkWait();
 delay(28000);// send every 30 second
  }

  void blinkWait() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

word getterTemperature() {
  return temperature;
}

byte getterHumidity() {
  return humidity;
}


User avatar
PoltoS
Posts: 7571
Joined: 26 Jan 2011 19:36

Re: HYT221 Temperatur / Humidity I2C Sensor

Post by PoltoS »

Works?

Can we publish it on Ecamples page with credits to you?

Could you please make a photo of schematics and may be a Fritzing (if not too painful).
mdietinger@gmail.com
Posts: 39
Joined: 12 Aug 2016 12:08

Re: HYT221 Temperatur / Humidity I2C Sensor

Post by mdietinger@gmail.com »

PoltS,

works like a charm :D
Sure you can publish on examples page, but wasn't 100% my creation.
Did copy and paste out of examples for UNO and adjust to get it working on Z-UNO.
Only thing to note, the sensor requires the updated wire library to get reported all 4 values. (BMP180 fix)
Over the weekend I will test as well HYT939 (better mechanical robustness).
Then I will provide pictures and Fritzing for both sensors.
I will post as well a code to change I2C address for HYTxxx. (Did work on HYT221)
Post Reply