Using HYT 271(Temperature, Humidity reading sensor) on Arduino over I2C

Hi,

I'm pretty new to Arduino, and I am trying to read temperature and humidity using HYT 271(data sheet & Application note)and Arduino UNO.

The problem is that it gives me wrong data(56.55 for humidity, and 19.50 for temperature), and the values are not changing even though I change the testing environment.

I found the sample code on here
I also attached the sample code below.

When I pulled out one of the wires, it stopped working, so I am assuming that wiring was done well.
Any comment will be appreciated.

Thanks,

Hojae


// ********************************************
// Attention, this is unverified Code, Rev. 0.5
// Destinated only as first Information
// and to look that it works with a ARDUINO
// This code will be improved in future!
// ********************************************
//include Wire(>>I²C)-libary:
#include <Wire.h>

//set the adress of sensor:
int adress=0x28;

void setup()
{ //set arduino as master:
Wire.begin();
//set baudrate:
Serial.begin(38400);
/this code sets
databits:8 ; parity:ODD ; stopbits:2
/
UCSR0C = B00111110;
}
void loop()
{ //declaration of variables:
unsigned int valuetempH=0;
unsigned int valuetempL=0;
unsigned int valuetemp=0;
unsigned int valuecapH=0;
unsigned int valuecapL=0;
unsigned int valuecap=0;
double endvaluecap=0;
double endvaluetemp=0;

//DataFetch(4 byte):
Wire.requestFrom(adress,4);
//wait for 4 bytes available:
if(Wire.available()>3)
//receive the databytes:
{valuecapH=Wire.receive();
valuecapL=Wire.receive();
valuetempH=Wire.receive();
valuetempL=Wire.receive();
Wire.endTransmission();

//mask the statusbits
valuecapH = (valuecapH&B00111111);
valuetempL= (valuetempL&B11111100);

/put together the highbytes and lowbytes
For this, the highbyte gets moved 8 bits higher
/
valuecap = (valuecapH*256 + valuecapL);
/the temperature highbyte gets moved 6 bits
and the lowbyte 2 bits lower
/
valuetemp = ((valuetempH)*64 + valuetempL/4);

//formate the values
endvaluecap=(double(valuecap))/163.83;
endvaluetemp=((double(valuetemp))/163.83)/2;

//output the values compatible with PCLog:
Serial.print(endvaluecap);
Serial.print("\t");
Serial.println(endvaluetemp);}

//in case of no receiving possible:
else
{Serial.println("no data");}

delay(100);
}

Also, how can I make an iframe to contain the code?

IMO you should send a Measurement Request to obtain new values. On-chip registers may deserve an initialization as well.

Iframe? Use code tags </> to box your code.

Well, I found an awesome working code on Github.

Thanks anyway, Dr. Diettrich

I am having the same issue where I am receiving incorrect values and the values do not change when the test environment changes. My code:

#include <Wire.h>
void setup() {
#define HYT_ADDR 0x28 // I2C address of the HYT 221, 271, 371 and most likely the rest of the family
Wire.begin(); // Join I2c Bus as master
// I2C Pins on the Arduino Uno are A4 for SDA and A5 for SCL
Serial.begin(115200); // Start serial communication for serial console output
}

void loop() {
unsigned int data[4];
Wire.beginTransmission(HYT_ADDR);
// Select data register
Wire.write(0x00);
// Stop I2C Transmission
Wire.endTransmission();
// Request 4 bytes of data
Wire.requestFrom(HYT_ADDR, 4);
// Read 4 bytes of data
// humidity msb, humidity lsb, temp msb, temp lsb
if (Wire.available() == 4)
{
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
data[3] = Wire.read();
}

// Convert the data to 14-bits
float humidity = ((((data[0] & 0x3F) * 256) + data[1]) * 100.0) / 16383.0;
int temp = ((data[2] * 256) + (data[3] & 0xFC)) / 4;
float cTemp = (temp / 16384.0) * 165.0 - 40.0;

// Output data to serial monitor

Serial.print("Temp= ");
Serial.print(cTemp);
Serial.print(" C");
Serial.print(" ");
Serial.print("RH= ");
Serial.print(humidity);
Serial.print(" %");
Serial.print(" ");
Serial.println();
delay(1000);
}

I am using the nano arduino micro-controller.