Solved - Celsius to Fahrenheit Conversion with HTU21DF

I am building a humidity controller using the HTU21DF humidity and temperature sensor from Adafruit. 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:

#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:

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;
}

This is the section of the file "Adafruit_HTU21DF.cpp" that deals with temperature:

But where do you call it? If that function returns temperature in Celsius, converting the returned value to Fahrenheit is trivial.

    Serial.print("\t\tTemp: "); Serial.print(htu.readTemperature());

Aha. You need to do more than just print the value returned by the function.

Whoever wrote this should be trussed up like a Thanksgiving turkey. Storing the return value in a variable and then print the variable's value makes it so much easier to add a line of code to alter the value in the variable.

That was what I was looking for. A friend of mine helped me write the code as this is my first Arduino project.

This is the new code:

#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() {

  int Temp = htu.readTemperature();
  int Humidity = htu.readHumidity();
  
  if (Humidity>=75) {
    Serial.print("Humidifier Off");
    digitalWrite(ledPin, LOW);
    digitalWrite(relayPin, LOW);
    Serial.print("\t\tTemp: "); Serial.print(1.8*Temp+32); Serial.print(" F");
    Serial.print("\t\tHum: "); Serial.print(Humidity); Serial.println(" %");
  }
  
  else {
    Serial.print("Humidifier On");
    digitalWrite(ledPin, HIGH);
    digitalWrite(relayPin, HIGH);
    Serial.print("\t\tTemp: "); Serial.print(1.8*Temp+32); Serial.print(" F");
    Serial.print("\t\tHum: "); Serial.print(Humidity); Serial.println(" %");
  }
  
  delay(150000);
    
}

My only remaining question is where should I declare the variables? I've seen code where the variables are declared before the setup loop but my Humidity and Temperature variables didn't work unless I declared them in the void loop.

My only remaining question is where should I declare the variables?

Variables that need global scope should be declared outside of any function. Variables that need local scope should be declared in the function/block where they are needed.

I've seen code where the variables are declared before the setup loop but my Humidity and Temperature variables didn't work unless I declared them in the void loop.

First, the readTemperature() function returns an int. But, no one refers to it as "the int readTemperature". So, it is grating on the ears (and eyes) to hear "the void loop". It is "the loop function".

Second, you have not shown where you declared the variables at global scope, nor have you defined what "didn't work" means. Therefore, we can't help with that problem.

I apologize for my ignorance on some terminology. This is my first time working with Arduino so I'm still learning the basics in some areas.

By "didn't work" I meant that I got nothing in the serial monitor. When I declare the variables inside the loop function the serial monitor shows the correct information (Humidifier On/Off, Temperature, Humidity).

My first attempt to declare the variables looked like this:

#include <Wire.h>
#include "Adafruit_HTU21DF.h"

Adafruit_HTU21DF htu = Adafruit_HTU21DF ();

int ledPin = 13;
int relayPin = 9;
int Temp = htu.readTemperature();
int Humidity = htu.readHumidity();
  
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 (Humidity>=75) {
    Serial.print("Humidifier Off");
    digitalWrite(ledPin, LOW);
    digitalWrite(relayPin, LOW);
    Serial.print("\t\tTemp: ");
    Serial.print(1.8*Temp+32);
    Serial.print(" F");
    Serial.print("\t\tHum: ");
    Serial.print(min(Humidity, 100));
    Serial.println(" %");
  }
  
  else {
    Serial.print("Humidifier On");
    digitalWrite(ledPin, HIGH);
    digitalWrite(relayPin, HIGH);
    Serial.print("\t\tTemp: ");
    Serial.print(1.8*Temp+32);
    Serial.print(" F");
    Serial.print("\t\tHum: ");
    Serial.print(min(Humidity, 100));
    Serial.println(" %");
  }
  
  delay(150000);
    
}

The above code led to getting nothing in the serial monitor.

After moving the humidity and temperature variable declarations to the loop function I was getting correct information int the serial monitor. That code is the code I posted in my previous post.

Prior to setup() and loop() declare the two variables like this:

int Temp = 0;
int Humidity = 0;

Inside loop(), read the values like this:

Temp = htu.readTemperature();
Humidity = htu.readHumidity();

Then print.

Thanks guys! It's working perfectly now.