1

I'm new to Arduino and trying to figure out how to convert Celsius to Fahrenheit with the parameters listed below. Do I need to create a separate file for the calculations? If so, can someone help me on how I can go about doing that?

#include <DHT.h>
#include <DHT_U.h>
int SENSOR = 2;
int TEMPERATURE;
int HUMIDITY;
DHT dht(SENSOR, DHT11);
void setup() {
 Serial.begin(9600);
 dht.begin();
 pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
 TEMPERATURE = dht.readTemperature();
 HUMIDITY = dht.readHumidity();
 Serial.print("Temp: ");
 Serial.print(TEMPERATURE);
 Serial.println(" celsius ");
 Serial.println(" ");
 Serial.print("Humidity: ");
 Serial.println(HUMIDITY);
 delay(2000);
 //Built In LED
 digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(250); // wait for a second
 digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
 delay(250); 
}
dda
1,5951 gold badge12 silver badges17 bronze badges
asked Jun 18, 2018 at 7:41
1
  • If dht.readTemperature() and dht.readHumidity() return floating point numbers then you may want to change TEMPERATURE and HUMIDITY to "float" instead of "int". Commented Jun 18, 2018 at 8:00

2 Answers 2

2

If you are using the Adafruit DHT sensor library, then you can get a Fahrenheit reading by passing true as an argument to readTemperature(). C.f. the comment in the source code. For example:

Serial.print("Temp: ");
Serial.print(dht.readTemperature(true)); // true -> Fahrenheit
Serial.println(" deg. F");

Note that this library offers also the conversion functions convertCtoF() and convertFtoC().

answered Jun 18, 2018 at 8:31
1

You could add a function to your sketch.

// Celsius to Fahrenheit conversion
double Fahrenheit(double celsius){
 return 1.8 * celsius + 32;
}

Then add this code to your loop().

Serial.print("Temp: ");
Serial.print(Fahrenheit(TEMPERATURE));
Serial.println(" fahrenheit ");
answered Jun 18, 2018 at 7:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.