Celsius to Fahrenheit
Part of the Displays and Sensors section of Coddy's Arduino journey. Lesson 21 of 24.
Fahrenheit is another temperature scale: water freezes at 32 F and boils at 212 F. To convert, multiply the degrees C by 9, divide by 5 and add 32:
float fahrenheit = celsius * 9 / 5 + 32;The order matters. The board works left to right through * and /, before +. Starting with celsius, a float, keeps every step in decimals.
Written 9 / 5 * celsius + 32, the first step divides two whole numbers: 9 / 5 is 1, the fraction is dropped, and the answer is celsius + 32. Writing 9.0 / 5 would fix it too.
Challenge
EasyThe sketch works out celsius. Print Fahrenheit instead:
- convert with
celsius * 9 / 5 + 32 - print the Fahrenheit value instead of the Celsius one
Press Run, then Check.
Try it yourself
const float BETA = 3950; // the thermistor's beta value
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(A0);
float celsius = 1 / (log(1 / (1023.0 / reading - 1)) / BETA + 1.0 / 298.15) - 273.15;
// turn celsius into fahrenheit and print that instead
Serial.println(celsius);
delay(500);
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Displays and Sensors
1Seven-Segment Displays
Seven-Segment DisplayDigit Patterns in BytesCount on the DisplayCountdown to ZeroDice Roller4Sensors
Ultrasonic Echo TimeEcho Time to CentimetresThermistor TemperatureCelsius to FahrenheitLight Level BarDistance on the LCDLCD ThermometerPractice on your own: Arduino playground