@Davor Predavec (and anyone else who might be interested).
The code below is translated from the D2000 (from https://www.allaboutcircuits.com/projects/how-to-check-and-calibrate-a-humidity-sensor/) to an Arduino and will simply read and print the temperature and humidity. For use with the 5V Arduino, I modified the schematic slightly - changing the pull ups and connection names from the 3.3V D2000 (see below).
Hope this helps.
The code below is translated from the D2000 (from https://www.allaboutcircuits.com/projects/how-to-check-and-calibrate-a-humidity-sensor/) to an Arduino and will simply read and print the temperature and humidity. For use with the 5V Arduino, I modified the schematic slightly - changing the pull ups and connection names from the 3.3V D2000 (see below).
C:
/*
*****************************************************
** This software is offered strictly as is with no **
** warranties whatsoever. Use it at your own risk. **
*****************************************************
HIH8121.ino
Read an HIH8121 Hum/Temp sensor over I2C. Translated from the D2000 version from:
https://www.allaboutcircuits.com/projects/how-to-check-and-calibrate-a-humidity-sensor/
*/
/* includes */
#include <Wire.h>
/* prototypes */
void delay_minutes(unsigned int);
/* defines */
#define BYTES2READ (4)
#define HIH8121addr (0x27) /* slave I2c address */
#define EPOCH (1) /* time between reads (mins) */
void setup()
{
Serial.begin(19200);
Wire.begin();
}
void loop()
{
byte HIHdata[4]; // data read from the sensor
byte HIHstatus; // sensor status bits
unsigned int RHraw, Tempraw; // raw RH (%) and temp (c) values
float RH, TempC; // converted RH (%) and Temp (C)
byte dummydata = 0xff; // dummy data for kick write
unsigned int sample = 1; // sample counter
delay(3000); // wait while opening monitor screen
Serial.println(" HIH8121 Sensor Test");
Serial.println();
// the following delay is just a reminder that on power up you want to
// wait before talking to the sensor so you can't put it into command mode
delay(50); // wait past the command window
/* print header */
Serial.println("Sample RH Temp (C)");
/* read sensor and display result loop*/
while (1) {
/* kick the sensor */
Wire.beginTransmission(HIH8121addr);
Wire.write(dummydata);
Wire.endTransmission(); // send it
delay(50); // 50 ms delay for measurement (~36.65 ms)
// get the data
Wire.requestFrom(HIH8121addr, BYTES2READ); // get 4 bytes of data
HIHdata[0] = Wire.read();
HIHdata[1] = Wire.read();
HIHdata[2] = Wire.read();
HIHdata[3] = Wire.read();
HIHstatus = HIHdata[0] & 0b11000000; // get status bits
if (HIHstatus != 0) {
Serial.println("Status Bits Error!");
}
HIHdata[0] = HIHdata[0] & 0b00111111; //mask status
Tempraw = (HIHdata[2] * 256) + HIHdata[3];
Tempraw = Tempraw / 4; //shift temperature
RHraw = (HIHdata[0] * 256) + HIHdata[1];
RH = ((float)RHraw / 16382.0) * 100.0;
TempC = (((float)Tempraw / 16382.0) * 165.0) - 40;
Serial.print(sample);
Serial.print(" ");
Serial.print(RH);
Serial.print(" ");
Serial.print(TempC);
Serial.println();
delay_minutes(EPOCH);
sample++;
}
}
void delay_minutes(unsigned int mins) {
unsigned int count;
for (count = 0; count < mins; count++) {
delay(60000); // delay 60 seconds
//delay(1000); // use this if you want seconds epoch
}
}
Last edited: