HIH6000 with Arduino Uno

First, log in to your Simuli account and navigate to the Simuli Virtual Lab. Here we will create a new emulated instance of the Arduino Uno. Click on the Launch button under the Arduino to get started.

This will open the configuration menu. First, provide a name for the project. Then, we will add the HIH6000 Temperature and Humidity Sensor by selecting it from the list of available components. Finally, review that the name for the project and the selected sensors are correct and click on the Launch button.

Once we have clicked on the Launch button, a new instance of the Arduino Uno will be created. It can take a few minutes, so be patient.

If you have not set up your Arduino IDE, follow the guide linked below.

Prerequisites for Arduino Development

Once you have the Arduino IDE ready to go, copy the code given below and paste it in the IDE.

#include <Wire.h>

class HIH6000 {
public:
  bool begin(int addr = 0x27) {
    i2cAddr = addr;
    Wire.begin();
    return true;
  }

  bool measure() {
    Wire.beginTransmission(i2cAddr);
    int size = Wire.write((uint8_t *)NULL, 0);
    if (Wire.endTransmission() != 0) {
      return false;
    }
    if (size != 0) {
      return false;
    }
    delay(37);  /* 36.65 nominal measurement cycle duration */
    for (int i = 0; i < 10; i++) {
      if (Wire.requestFrom(i2cAddr, 4) != 4) {
        return false;
      }
      int hum = Wire.read();
      if ((hum & STATUS_BITMASK) == STATUS_NORMAL) {
        hum = (hum << 8) | Wire.read();
        this->hum = hum * 100.0 / ((1 << 14) - 2);
        int temp = Wire.read();
        temp = (temp << 6) | (Wire.read() >> 2);
        this->temp = temp * 165.0 / ((1 << 14) - 2) - 40;
        return true;
      }
    }
    return false;
  }

  float readHumidity() {
    return hum;
  }

  float readTemperature() {
    return temp;
  }

  bool measureHumidity(float *hum) {
    if (!measure()) {
      return false;
    }
    *hum = this->hum;
    return true;
  }

  bool measureTemperature(float *temp) {
    if (!measure()) {
      return false;
    }
    *temp = this->temp;
    return true;
  }

private:
  static const byte STATUS_NORMAL = 0x00;
  static const byte STATUS_STALE = 0x40;
  static const byte STATUS_CMD = 0x80;
  static const byte STATUS_BITMASK = 0xC0;
  int i2cAddr;
  float hum, temp;
};

HIH6000 hih6000;

void setup() {
  hih6000.begin();
  Serial.begin(115200);
}

void loop() {
  float hum, temp;

  if (hih6000.measure()) {
    Serial.print("Temperature: " + String(hih6000.readTemperature()) + " C");
    Serial.println(", Humidity: " + String(hih6000.readHumidity()) + "%");

  }
  else {
    Serial.println("Cannot measure temperature/humidity!");
  }
  delay(1000);
}

Now save the sketch in a folder and keep a note of that folder. We now need to get the compiled binaries for the sketch. Go to Sketch and find Export Compiled Binary. The program will take some time to compile. Once the compilation is complete, go back to IoTIFY Virtual Lab and open the instance we created.

A new tab will open and we can interact with our Arduino. First, we need to uploadour compiled binary. Click on the Arduino, this will open a file explorer, navigate to where you had stored the Arduino sketch. In the same folder, a file called sketch.ino.standard.hex will be present (where sketch is the name of the Arduino sketch). Select this file and click on open. Now the hex file will be uploaded. Finally, reset the Arduino so that it loads the new file. To reset, just click the red power button above the top left corner of the Arduino. Now you can see the reading of the HIH6000 sensor in the terminal. If you change the values on the sensor, it will also be reflected in the terminal.

Now that you know how to get the readings from the HIH6000 sensor, you could try to make an alert system, which would turn on LEDs when the temperature goes above a certain threshold. You need an if statement to compare the current temperature value with the threshold and then switch on the LED if the condition is satisfied Keep building and have fun! 🎉

Last updated