Celsius to Fahrenheit Conversion with HTU21DF

  • I am building a humidity controller using the humidity and temperature sensor. So far everything is working correctly but I was wondering how I could get the temperature to read back in Fahrenheit rather than Celsius. I know the formula is 9/5*C+32=F but I don't know exactly how to get this to work with my code.

    Here is the code I have written:
    Code: [Select]

    #include <Wire.h>
    #include "Adafruit_HTU21DF.h"
    Adafruit_HTU21DF htu = Adafruit_HTU21DF ();
    int ledPin = 13;
    int relayPin = 9;
    void setup() {
       pinMode (ledPin, OUTPUT);
       pinMode (relayPin, OUTPUT);
       Serial.println("HTU21DF Humidity Controller");
       if (!htu.begin()) {
         Serial.println("Couldn't find sensor!");
    void loop() {
      if (htu.readHumidity()>=75) {
        Serial.print("Humidifier Off");
        digitalWrite(ledPin, LOW);
        digitalWrite(relayPin, LOW);
        Serial.print("\t\tTemp: "); Serial.print(htu.readTemperature());
        Serial.print("\t\tHum: "); Serial.println(htu.readHumidity());
      else {
        Serial.print("Humidifier On");
        digitalWrite(ledPin, HIGH);
        digitalWrite(relayPin, HIGH);
        Serial.print("\t\tTemp: "); Serial.print(htu.readTemperature());
        Serial.print("\t\tHum: "); Serial.println(htu.readHumidity());
    This is the section of the file "Adafruit_HTU21DF.cpp" that deals with temperature:
    Code: [Select]
    float Adafruit_HTU21DF::readTemperature(void) {
      // OK lets ready!
      delay(50); // add delay between request and actual read!
      Wire.requestFrom(HTU21DF_I2CADDR, 3);
      while (!Wire.available()) {}
      uint16_t t = Wire.read();
      t <<= 8;
      t |= Wire.read();
      uint8_t crc = Wire.read();
      float temp = t;
      temp *= 175.72;
      temp /= 65536;
      temp -= 46.85;
      return temp;

  • @irenla
    You have probably already figured this out but try this.

    return temp;
    float tempF = (temp * 9.0) / 5.0 + 32.0;

    you get the idea now.

Log in to reply

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.