Last active
August 29, 2015 14:20
-
-
Save ckxng/dc06543e86d17620e8ae to your computer and use it in GitHub Desktop.
Arduino LM35 Temperature Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* GLOBAL CONFIGURATION VARIABLES | |
*/ | |
int LM35PIN = 0; | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
print3Temperatures(LM35PIN); | |
delay(1000); | |
} | |
/** | |
* Read an LM35 temperature sensor and print the ambient | |
* temperature. Temperatures are measured in Celcius, Farenheit, | |
* and Kelvin. | |
* \param pin the analog pin leading to the LM35 Vout | |
*/ | |
void print3Temperatures(int pin) { | |
Serial.print("Temp: "); | |
Serial.print(readTemperatureC(pin)); | |
Serial.print("*C, "); | |
Serial.print(readTemperatureF(pin)); | |
Serial.print("*F, "); | |
Serial.print(readTemperatureK(pin)); | |
Serial.println("*K"); | |
} | |
/** | |
* Read an LM35 temperature sensor and print the ambient | |
* temperature. Temperatures are measured in Celcius. | |
* \param pin the analog pin leading to the LM35 Vout | |
* \return float the temperature measured in Celcius | |
*/ | |
float readTemperatureC(int pin) { | |
return float(analogRead(pin)) / 1023 * 500; | |
} | |
/** | |
* Read an LM35 temperature sensor and print the ambient | |
* temperature. Temperatures are measured in Farenheit. | |
* \param pin the analog pin leading to the LM35 Vout | |
* \return float the temperature measured in Farenheit | |
*/ | |
float readTemperatureF(int pin) { | |
return readTemperatureC(pin) * 9.0/5.0 + 32; | |
} | |
/** | |
* Read an LM35 temperature sensor and print the ambient | |
* temperature. Temperatures are measured in Kelvin. | |
* \param pin the analog pin leading to the LM35 Vout | |
* \return float the temperature measured in Kelvin | |
*/ | |
float readTemperatureK(int pin) { | |
return readTemperatureC(pin) + 273.15; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment