Ciao,
here You find a library for HYT221 (derived from a library for HYT331).
The library is not well optimized, since it is blocking MCU using delay.
Delay is used to wait for measurement to take effect, otherwise You will read the previous measured value.
It works for me.
HYT221.h
#ifndef _HYT221_H
#define _HYT221_H
#define HYT_DEBUG 0
class HYT221 {
private:
int address;
int rawTemp;
int rawHum;
public:
HYT221(int I2Cadr);
uint8_t begin( void );
uint8_t read( void );
int getRawHumidity( void );
int getRawTemperature( void ) ;
double getHumidity( void ) ;
double getTemperature( void );
};
extern HYT221 HYT;
#endif
HYT221.cpp
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "HYT221.h"
#define HYT221_ADDR 0x28
// #define SCALE_MAX 0b100000000000000
#define SCALE_MAX 16384.0
#define TEMP_OFFSET 40.0
#define TEMP_SCALE 165.0
#define HUM_SCALE 100.0
//
// HYT221
//
// !!! Blocking code
//
HYT221::HYT221(int I2Cadr){
address = I2Cadr;
}
uint8_t HYT221::begin(void) {
return 1;
}
uint8_t HYT221::read( void ) {
Wire.beginTransmission(address);
Wire.write((byte)0x00);
Wire.available();
int Ack = Wire.read(); // receive a byte
// DEBUG
#if HYT_DEBUG
Serial.print("ACK: ");
Serial.println(Ack);
#endif
Wire.endTransmission();
// DEBUG ////////////////
//request 4 bytes
#if HYT_DEBUG
Wire.requestFrom(address, 4);
Wire.available();
int a1 = Wire.read(); // receive a byte
int a2 = Wire.read(); // receive a byte
int a3 = Wire.read(); // receive a byte
int a4 = Wire.read(); // receive a byte
#endif
////////////////////////////////
// delay inteval !!! blocking MCU
delay(100);
//request 4 bytes
Wire.requestFrom(address, 4);
Wire.available();
int b1 = Wire.read(); // receive a byte
int b2 = Wire.read(); // receive a byte
int b3 = Wire.read(); // receive a byte
int b4 = Wire.read(); // receive a byte
// DEBUG
#if HYT_DEBUG
Serial.print("a1: ");
Serial.println(a1, BIN);
Serial.print("b1: ");
Serial.println(b1, BIN);
Serial.print("a2: ");
Serial.println(a2, BIN);
Serial.print("b2: ");
Serial.println(b2, BIN);
Serial.print("a3: ");
Serial.println(a3, BIN);
Serial.print("b3: ");
Serial.println(b3, BIN);
Serial.print("a4: ");
Serial.println(a4, BIN);
Serial.print("b4: ");
Serial.println(b4, BIN);
#endif
// combine the bits
rawHum = ( b1 << 8 | b2 ) & 0x3FFF;
// Mask away 2 last bits see HYT 221 doc
rawTemp = b3 << 6 | ( unsigned(b4) >> 2 ) ;
return 1;
}
int HYT221::getRawHumidity( void ) {
return rawHum;
}
int HYT221::getRawTemperature( void ) {
return rawTemp;
}
double HYT221::getHumidity( void ) {
//hum = 100.0 / pow( 2, 14 ) * rawHum;
return (HUM_SCALE * rawHum) / SCALE_MAX;
}
double HYT221::getTemperature( void ) {
return ( (TEMP_SCALE * rawTemp) / SCALE_MAX ) - TEMP_OFFSET;
}
keywords.txt
#######################################
# Syntax Coloring Map For HYT221
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
HYT221 KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
read KEYWORD2
getRawHumidity KEYWORD2
getRawTemperature KEYWORD2
getHumidity KEYWORD2
getTemperature KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
Example
/*
Programm to measure and transmit Humidity and Temperature
HYT-221 via Serial Communication.
Not optimized for high speed measurement. A minimum delay
of 100 ms is currently hard coded in the getData() function
TODO:
- Add functionalty for high speed measurement. The HYT221
indicates if a new measurement is ready via the status/stall bit
see documentation of HYT221
- Do not use a global variable for temperature and humidty. Use
a function with an array to return the data.
- Think on reducing number of variables used.
*/
#include <Wire.h>
#include <HYT221.h>
HYT221 HTsens01 = HYT221(0x28);
void setup() {
Serial.begin(57600);
// initialize I2C protocoll
Wire.begin();
// initialize the sensor
HTsens01.begin();
}
void loop() {
HTsens01.read();
double h = HTsens01.getHumidity();
double t = HTsens01.getTemperature();
int hraw = HTsens01.getRawHumidity() ;
int traw = HTsens01.getRawTemperature() ;
Serial.print(t);
Serial.print(" C, ");
Serial.print(traw);
Serial.print(" , ");
Serial.print(h);
Serial.print(" %, ");
Serial.println(hraw);
delay(30000);
}
Beware that making readings at high frequency will self heat the sensor too much, giving wrong higher temperature values.
This part of code is major difference from the code posted before in this post.
rawTemp = b3 << 6 | ( unsigned(b4) >> 2 ) ;
There is a variable defined in the header file to print the pre measurement and post measurement byte values.
Ciao,
Marco.