did some further testing with HYT 939 temp/hum I2C sensor.
I think it is better to post HYT939 sensor code rather than HYT221 in the examples section.
Main reason is that HYT939 easily fits into a breadboard and is physically more robust (HYT221 Pins don't fit the 2.54mm standard)
Attached example allows two HYT939 sensors to be connected via I2C.
To allow both of them being operated, for one the default I2C address needs to be changed. (2nd script)
Every 30" temperature and humidity are reported to serial monitor.
The values are reported to Z_Wave controller based on the defined polling time. (in my case every 300 sec or 5 minutes)
I believe I managed to cover as well temperatures bellow 0° by using unsigned integer.
Was only able to simulate the temperature and that worked fine.
Installed Fritzing, but had issues to modify a TO39 component to show the correct location of notch/nose.
I am happy if this code is posted in examples, but would need some help to get the right component for Fritzing.
Code: Select all
// demo sketch for connecting HYT221 humidity and temperature sensor to Z-Uno
// add library
#include <Wire.h>
#include <math.h>
unsigned int humidity; // here we will store the humidity
unsigned int temperature; // here we will store the temperature
unsigned int humidity2; // here we will store the humidity
unsigned int temperature2; // here we will store the temperature
// 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),
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE,
SENSOR_MULTILEVEL_SCALE_CELSIUS,
SENSOR_MULTILEVEL_SIZE_TWO_BYTES,
SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL,
getterTemperature2),
ZUNO_SENSOR_MULTILEVEL_HUMIDITY(getterHumidity2)
);
void setup() {
#define HYT_ADDR 0x28 // I2C address of the HYT 221, 271, 939 and most likely the rest of the family
#define HYT_ADDR2 0x50 // 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);
}
void loop() {
readHYT();
readHYT2();
delay(30000);
}
void blinkWait() {
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
}
void readHYT () {
int iData[4];
Wire.beginTransmission(HYT_ADDR);
Wire.write(0); // initiate measurement
Wire.endTransmission();
blinkWait();
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(165.0 / pow(2,14) * rawTemperature - 40);
Serial.println("C");
}
else {
Serial.println("Not enough bytes available on wire.");
}
}
void readHYT2 () {
int iData[4];
Wire.beginTransmission(HYT_ADDR2);
Wire.write(0); // initiate measurement
Wire.endTransmission();
blinkWait();
Wire.beginTransmission(HYT_ADDR2); // Begin transmission with given device on I2C bus
Wire.requestFrom(HYT_ADDR2, 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);
humidity2 = 100.0 / pow(2,14) * rawHumidity;
Serial.print("Humidity2: ");
Serial.print(humidity2);
// 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];
temperature2 = (165.0 / pow(2,14) * rawTemperature - 40)*10;
Serial.print("% - Temperature2: ");
Serial.print((165.0 / pow(2,14) * rawTemperature - 40));
Serial.println("C");
}
else {
Serial.println("Not enough bytes available on wire.");
}
}
word getterTemperature() {
return temperature;
}
byte getterHumidity() {
return humidity;
}
word getterTemperature2() {
return temperature2;
}
byte getterHumidity2() {
return humidity2;
}
To change I2C address only connect one sensor and connect Z-UNO-PIN10 to Sensor-PIN-VDD(3.3V) instead of the power cable.
Most likely you don't get anything out of the serial monitor as it takes longer to open the monitor as the code takes to execute.
Code: Select all
// Sketch to change the address of a HYT humidity sensor
#include "Wire.h"
#define OLDADDRESS 0x28
#define NEWADDRESS 0x50
int powerPin = 11; //connect sensor VDD to pin 11
void setup()
{
pinMode(powerPin, OUTPUT);
Wire.begin();
Serial.begin(9600);
}
void loop()
{
delay(5000);
Serial.println("Changing the address of HYT sensor");
Serial.print("Old address (HEX): ");
Serial.println(OLDADDRESS, HEX);
Serial.print("New address (HEX): ");
Serial.println(NEWADDRESS, HEX);
Serial.println("Power-on the sensor");
digitalWrite(powerPin, HIGH);
Serial.println("Trying to put the sensor to Command mode");
Wire.beginTransmission(OLDADDRESS);
Wire.write(0xA0); //start-command-mode
Wire.write(0x00); //highbyte
Wire.write(0x00); //lowbyte
Wire.endTransmission(); //transmitting bytes
Serial.println("Sensor should be now in Command mode");
Wire.beginTransmission(OLDADDRESS);
Wire.write(0x5C); //change i2c address
Wire.write(0x00); //highbyte
Wire.write(NEWADDRESS); //lowbyte, new address
Wire.endTransmission();
Serial.println("Address should be now changed");
Wire.beginTransmission(OLDADDRESS);
Wire.write(0x80); //normal mode
Wire.write(0x00); //highbyte
Wire.write(0x00); //lowbyte
Wire.endTransmission();
Serial.println("Sensor should be now in normal mode");
for (uint8_t add = 0X0; add < 0X80; add++) {
Wire.requestFrom(add, (uint8_t)1);
if (Wire.available()) {
Serial.print("Found I2C device from address: ");
Serial.println(add, HEX);
}
}
Serial.println("Power-off the sensor");
digitalWrite(powerPin, LOW);
Serial.println("Done!");
while (1){} //infinite loop
}