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