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);
}
-
If dht.readTemperature() and dht.readHumidity() return floating point numbers then you may want to change TEMPERATURE and HUMIDITY to "float" instead of "int".VE7JRO– VE7JRO2018年06月18日 08:00:34 +00:00Commented Jun 18, 2018 at 8:00
2 Answers 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()
.
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 ");