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;
}