Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. Celsius to Fahrenheit Conversion with HTU21DF

Celsius to Fahrenheit Conversion with HTU21DF

Scheduled Pinned Locked Moved Hardware
2 Posts 2 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    irenla
    wrote on last edited by gohan
    #1

    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;
    }
    
    S 1 Reply Last reply
    0
    • I irenla

      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;
      }
      
      S Offline
      S Offline
      Scott Wilson
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      12

      Online

      12.1k

      Users

      11.2k

      Topics

      113.4k

      Posts


      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • MySensors
      • OpenHardware.io
      • Categories
      • Recent
      • Tags
      • Popular