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.begin(9600);
       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());
      }
     
      delay(1000);
       
    }
    This is the section of the file "Adafruit_HTU21DF.cpp" that deals with temperature:
    
    Code: [Select]
    
    float Adafruit_HTU21DF::readTemperature(void) {
     
      // OK lets ready!
      Wire.beginTransmission(HTU21DF_I2CADDR);
      Wire.write(HTU21DF_READTEMP);
      Wire.endTransmission();
     
      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
 

Suggested Topics

  • 87
  • 5
  • 1
  • 7
  • 2
  • 7

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts