DHT: Celsius to Fahrenheit Conversion

does anyone have lib or sektch to convert Fahrenheit Conversion with a dth11 sensor

For temperatures greater than zero:

Celsius * 1.8 + 32

That also works for temperatures below zero degrees C.

Pete

how do i add that to the code

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
  }
}

Change this:

    Serial.print(t);
    Serial.println(" *C");

to this:

    Serial.print((int)round(1.8*t+32));
    Serial.println(" *F");

Pete

el_supremo: Change this:

    Serial.print(t);
    Serial.println(" *C");

to this:

    Serial.print((int)round(1.8*t+32));
    Serial.println(" *F");

Pete

Not sure if the round function and casting as an int is needed... The sample code provided has the Celsius temperature stored as a float, and Serial.print() knows what to do with floats.

IIRC the dht library has a method for returning either F or C.

A quick look in the library and yes.

t = dht.readTemperature();         // will return C
t =  dht.readTemperature(true);  // returns F

The accuracy of the DHT11 is plus or minus two degrees Celsius so printing fractions of a degree (Celsius or Fahrenheit) is pointless.

Pete

el_supremo: The accuracy of the DHT11 is plus or minus two degrees Celsius so printing fractions of a degree (Celsius or Fahrenheit) is pointless.

Pete

Good point. But, I think Jimmy60 has the better solution (let the library deal with it).

Jimmy60: IIRC the dht library has a method for returning either F or C.

A quick look in the library and yes.

t = dht.readTemperature();         // will return C
t =  dht.readTemperature(true);  // returns F

Thanks just what i was looking for

I have the same problem changing C to F
This is my first sketch most of it from the internet
My code is a little different from the above example not sure what to change to get F

//I2C bus support
#include <Wire.h>
//I2C
#include <LiquidCrystal_I2C.h>
//OneWire bus suport
#include <OneWire.h>
//DS18B20 temperature sensor support
#include <DallasTemperature.h>
//DS18B20 sensor pin
#define ONE_WIRE_BUS 8

// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

LiquidCrystal_I2C lcd(0x3f, 16, 2); // set the LCD address for the LCD display

void setup()
{
  lcd.init();
  lcd.setBacklight(HIGH);; //Turns backlight on
  
  sensors.begin();
}

void loop()
{  
  sensors.requestTemperatures();

  //Temperature Sensor #1
  float temp = sensors.getTempCByIndex(0);

  //Print first sensor results
  float temp50 = sensors.getTempCByIndex(0);  // give temp a better and unique name
  temp50 = temp50 + .4;   // If Temp is out you can add or subtract here
  //Print first sensor results
  lcd.setCursor (0, 0 );
  lcd.print("OutsideTemp ");
  lcd.print(temp50);


  //Read second sensor
  temp = sensors.getTempCByIndex(1);

  //Temperature Sensor #2
  float temp100 = sensors.getTempCByIndex(1);  // give temp a better and unique name
  temp100 = temp100 + .4;   // If Temp is out you can add or subtract here
  //Print second sensor result
  lcd.setCursor (0, 1 );

  lcd.print("Pool Temp   ");
  lcd.print(temp100);

  //Wait 0.1 sec
  delay(100);
}

What about reading the library you use...

sensors.getTempFByIndex(0);

Jimmy60: IIRC the dht library has a method for returning either F or C.

A quick look in the library and yes.

t = dht.readTemperature();         // will return C
t =  dht.readTemperature(true);  // returns F

How do you read a library? Kind new and still learning

Find the library code files (library.h, and .cpp, possibly more files as well) in the libraries folder in your arduino folder. Open the code files with a text editor, such as TextEdit (mac) or NotePad (pc). Read the contents.