Skip to content

Instantly share code, notes, and snippets.

@ckxng
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckxng/dc06543e86d17620e8ae to your computer and use it in GitHub Desktop.
Save ckxng/dc06543e86d17620e8ae to your computer and use it in GitHub Desktop.
Arduino LM35 Temperature Example
/**
* 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