Go Down

Topic: HYT 221 from Hygrosens Instruments (Read 7886 times) previous topic - next topic

Modular2000

Mar 06, 2011, 09:42 am Last Edit: Mar 06, 2011, 10:10 am by Modular2000 Reason: 1
Hello All,

I am quite new to Arduino and Electronics as well. However I wanted to measure Humidty and Temperature using my Arduino board. I purchased the HYT 221 from Hygrosens capable of providing Humidity and Temperature via I2C bus. I was not able to find example code on the web so I want to share my code with you (see below).  I built my code based on the documentation about the I2C protocoll available from Hygrosens Insturments. As I am a beginner no guarantee for correct functionalty.

However I have also a question: Does somebody know if there is an adapter available to connect the 1.27 mm pins to a "standard" soldering prototyping board with something about 2 mm distance per connection point?

Code: [Select]

#include <Wire.h>

/*
Programm to measure and transmit Humidity and Temperature
via HYT228 on request via Serial Communication.

Not optimized for high speed measurement. A minimum dely
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.

*/



// Standard adress: 0x28
#define HYT221_ADDR 0x28

// for Temperature and Humidity
double Humidity;
double Temperature;

// for serial initiated measurement
char msg = '0';

void getData(){
  // initiate measurement
  Wire.beginTransmission(HYT221_ADDR);
  Wire.send(0);
  Wire.available();
  int Ack = Wire.receive(); // receive a byte

    Wire.endTransmission();
  delay(100);

  // READ DATA from here on
  Wire.beginTransmission(HYT221_ADDR);
  Wire.requestFrom(HYT221_ADDR,4);  //Reade 1 byte
  Wire.available();
  int b1 = Wire.receive(); // receive a byte
  int b2 = Wire.receive(); // receive a byte
  int b3 = Wire.receive(); // receive a byte
  int b4 = Wire.receive(); // receive a byte
  Wire.send(0); //NACK

  // combine the bits
  int RawHumidBin = b1 << 8 | b2;
  // compound bitwise to get 14 bit measurement first two bits
  // are status/stall bit (see intro text)
  RawHumidBin =  (RawHumidBin &= 0x3FFF);
  Humidity = 100.0/pow(2,14)*RawHumidBin;

  b4 = (b4 &= 0x3F); //Mask away 2 least sign. bits see HYT 221 doc
  int RawTempBin = b3 << 6 | b4;
  Temperature = 165.0/pow(2,14)*RawTempBin-40;

  Wire.endTransmission();
}

//setup
void setup(){
  // Setup Serial connection
  Serial.begin(9600);
  delay(1000);
  Wire.begin();             // join I2C bus

}

void loop(){

  // Check for incoming messages
  if (Serial.available() > 0) {
    msg = Serial.read();
    //Serial.println(msg);
  }

  // Only transmit data via serial of requested.
  if (msg == '1') {
    getData();
    Serial.print(Humidity);
    Serial.print("; ");
    Serial.println(Temperature);
    msg = '0';
  }

  delay(10); 
}



edit: small bug removed on 06.03.2011, 10:10

Best Regards
Modular2000




GekoCH

Hy

zhx for the example. However why do we need the delay of 100ms since when
I do remove this the sensor does still work???
I know it is written in the manual but I see no difference.

Thx
Andy

MarkT

If you go to http://www.hygrochip.com/index.php?id=8&L=1 and click on documentation you'll find an original Arduino example - this only has a delay at the end of loop() to prevent it printing too fast to read...  Can safely ignore delays AFAICT.
[ I won't respond to messages, use the forum please ]

GekoCH

I saw this code but it is not working, it is printing always the same values messured at the first time.
So the Code from above does work better.

Andy

Marco Benini

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

Code: [Select]

#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

Code: [Select]

#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

Code: [Select]

#######################################
# 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

Code: [Select]

/*
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.
Code: [Select]
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.


GekoCH

thx but hmm for which version of the Arduino is this since for my Arduino IDE 0022 it doesn't work...

HYT221.cpp: In member function 'uint8_t HYT221::read()':
HYT221.cpp:33: error: 'class TwoWire' has no member named 'write'

andy

GekoCH

ok found the problem. It does work with Arduino v1.
In 0022 you have to cahnge wire.read to wire.receive and wire.write to wire.send and then it does work :)

However I got another question.
Does someone know how to change the Adress of one sensor?

Thx
Andy

copachino


thx but hmm for which version of the Arduino is this since for my Arduino IDE 0022 it doesn't work...

HYT221.cpp: In member function 'uint8_t HYT221::read()':
HYT221.cpp:33: error: 'class TwoWire' has no member named 'write'

andy


hi i want to know your expiriens with this sensor.... i just bought an SHT/! and can no get it work so maybe you could give me a recommendation for a sensor for temperature and humidity messure........ i´ve treid to seach this HYT 221 on ebay and mouser and can´t find it

GekoCH

just take any HYT they 221 is an old one so use the 271.
I'm currently using this one too.


copachino


GekoCH

pls google since I don't know where you're from in europe you get it from every electronic store...

copachino

ok i will try to search im not in Europe, but in my country can not buy one so i will searh on USA... thx

Marco Benini

Ciao,

I have both Sensirion and IST (Hygrosens) sensors.

IST one has better accuracy and is I2C (easier to use), Sensirion has better control over data sent (CRC).

Since the products are in IST AG portfolio these sensors are easier to find (IST is the Swiss company that produce thin film sensors).
Thery also produce great digital temperature sensors with better accuracy than Dallas 18B20.
http://www.ist-ag.com/

In USA You can find IST prodcuts at www.servoflo.com .

In Europe from

Schukat.com
http://www.schukat.com/schukat/schukat_cms_en.nsf/index/CMSDF15D356B046D53BC1256D550038A9E0?OpenDocument&wg=A2645&refDoc=CMS83E9AF7D7863AC9FC125711E004F71BE

Beware that prices variation in very high.

conrad.com
http://www.conrad.com/DIGITAL-HUMIDITY-SENSOR-HYT-221.htm?websale7=conrad-int&pi=505671&ci=SHOP_AREA_14741_0231310

I had my I2C address changed directly by the manufacturer last year (I purchased 3 for a project), anyway I have been said that there is a procedure to change it.
Maybe You can contact IST.

Ciao,
Marco.


GekoCH

I got a document where it is described how to change the address however I can't managed it to get
work....
Maybee you can help me since you are somehow familiar with the I2C interface and the HYR Sensors.

here is the link:
www.ibrutech.ch/Change_Address.pdf

Andy

copachino


Ciao,

I have both Sensirion and IST (Hygrosens) sensors.

IST one has better accuracy and is I2C (easier to use), Sensirion has better control over data sent (CRC).

Since the products are in IST AG portfolio these sensors are easier to find (IST is the Swiss company that produce thin film sensors).
Thery also produce great digital temperature sensors with better accuracy than Dallas 18B20.
http://www.ist-ag.com/

In USA You can find IST prodcuts at www.servoflo.com .

In Europe from

Schukat.com
http://www.schukat.com/schukat/schukat_cms_en.nsf/index/CMSDF15D356B046D53BC1256D550038A9E0?OpenDocument&wg=A2645&refDoc=CMS83E9AF7D7863AC9FC125711E004F71BE

Beware that prices variation in very high.

conrad.com
http://www.conrad.com/DIGITAL-HUMIDITY-SENSOR-HYT-221.htm?websale7=conrad-int&pi=505671&ci=SHOP_AREA_14741_0231310

I had my I2C address changed directly by the manufacturer last year (I purchased 3 for a project), anyway I have been said that there is a procedure to change it.
Maybe You can contact IST.

Ciao,
Marco.




do you have a code and also a sckematic of hoe did you get STH to work??? this could be my last test beforore buy another, if with this donesnt work well i have to change the sensor cuase probably its bad

Go Up