Created
October 21, 2014 07:10
-
-
Save DivinityArcane/2fbf1014141290ff043f to your computer and use it in GitHub Desktop.
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
// / temp.ino | |
// ? Simple temperature reader. Reads temperature via LM335Z sensor and outputs to SMC1602A compliant LCD | |
// @ Justin Eittreim <eittreim.justin@live.com> | |
#include <LiquidCrystal.h> | |
// Provide the appropriate pins to the LCD library | |
// 12=RS, 11=E, 5=D4, 4=D5, 3=D6, 2=D7 | |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | |
// voltage = voltage of the 5V pin. I use a 9V AC, so the 5V gives max of 5V | |
// total = sum of the samples | |
// sensor = sensor sample value | |
float voltage = 5.0, total = 0, samples = 10, sensor = 0; | |
// Self-explanetory | |
float Kelvin = 0, Celsius = 0, Fahrenheit = 0; | |
// sensorPin = pin that sensor output is fed. Sensor is LM335Z | |
// interval = time between samples (in milliseconds) | |
// i = magic variable, for loops | |
int sensorPin = A0, interval = 100, i = 0; | |
// Good ol' setup function | |
void setup() { | |
// Initialize the LCD. 16 columns by 2 rows | |
lcd.begin(16, 2); | |
lcd.display(); | |
// Show something on the display, otherwise it's blank until we have samples | |
lcd.print(F("Initiating LM335")); | |
} | |
// Good ol' loop function | |
void loop() { | |
// Blank the sum of samples for each iteration | |
total = 0; | |
for (i = 0; i < samples; i++) { | |
total += getTemperature(sensorPin); | |
delay(interval); | |
} | |
// For the top row on the LCD, print the label | |
lcd.setCursor(0, 0); | |
lcd.print(F("Avg Temp (F): ")); | |
// For the bottom row, print the temperature | |
lcd.setCursor(0, 1); | |
lcd.print(total / samples); | |
// In the case of 0 samples, have a small delay to avoid abusing the pin/sensor | |
delay(50); | |
} | |
// Function to grab the value from the sensor and convert it to fahrenheit | |
float getTemperature(int pin) { | |
// Read the value from the sensor | |
sensor = analogRead(pin); | |
// The value divided by the max for a pin (1023) and multiplied by our voltage = volts | |
// 10 Kelvin per mV (millivolt), 1mV = 1000V. Simpler to just do K=V*100 instead of K=(v*1000)/10) | |
Kelvin = ((sensor / 1023.0) * voltage) * 100.0; | |
// Celsius = Kelvin - 273.15 because 0C = 273.15K | |
Celsius = Kelvin - 273.15; | |
// Fahrenheit = C*9/5+23 | |
Fahrenheit = (Celsius * (9.0 / 5.0)) + 32.0; | |
return Fahrenheit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment