Smart Thermostat Arduino - part sensors
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
//code snippet from: https://github.com/niektemme/smarttherm-arduino/blob/master/arduino-sketch/smarttherm.ino | |
//ref voltage | |
#define aref_voltage 3.3 | |
//variables for settemp variable resistor | |
int sensorPin = 0; //sensor = settemp | |
int sensorValue = 0; | |
int sensorOldValue = 0; | |
//variables for TMP36 | |
int temperaturePin = 1; //act temp | |
int tempValue = 0; | |
int tempOldValue = 0; | |
int tempDisValue = 0; | |
int tempDisOldValue = 0; | |
//variables for boiler on off sensor (photo resistor) | |
int boilerPin = 2; | |
int boilerValue = 0; | |
//variables for thermistor | |
int thermistorPin= 3; | |
int thermistorValue =0; | |
int thermistor2Pin= 4; | |
int thermistor2Value =0; | |
... | |
void setup() { | |
analogReference(EXTERNAL); //analogue refference | |
... | |
} | |
void loop() { | |
... | |
float temperature = tempReading(temperaturePin); //getting the voltage reading from the temperature sensor | |
temperature = (temperature - .5) * 100; //converting from 10 mv per degree wit 500 mV offset | |
//to degrees ((volatge - 500mV) times 100) | |
tempValue = (int) (temperature*100); | |
//reading boiler on of state | |
float boiler = getVoltage(boilerPin); | |
boilerValue = (int) (boiler*100); | |
//avaraging set temperature to .5 degrees celcius | |
float sensorCalcValue = (round(((analogRead(sensorPin)/5.0)+100.0)/10.0*2.0))/2.0*10.0; | |
sensorValue = (int) sensorCalcValue; | |
//Reading thermistor 1 value using thermistorRead() function, sending nominal value and bcofficient (steinhart) to function | |
float thermistor = thermistorRead(thermistorPin,10000,10000,25,3950); | |
thermistorValue = (int) (thermistor*100); | |
//Reading thermistor 2 value using thermistorRead() function, sending nominal value and bcofficient (steinhart) to function | |
float thermistor2 = thermistorRead(thermistor2Pin,100000,100000,25,4261); | |
thermistor2Value = (int) (thermistor2*100); | |
... | |
} | |
//function to get analoge value to digital range | |
float getVoltage(int pin){ | |
return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range | |
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts | |
} | |
//tmp36 sensor fucntion | |
float tempReading(int pin){ | |
int tempRead = analogRead(pin); | |
float voltage = tempRead * aref_voltage; | |
voltage /= 1024.0; | |
return voltage; | |
} | |
//function for read thermistor value given nominal value and bcofficient (steinhart) | |
float thermistorRead(int pin, int seriesr, int thermistnominal,int tempnom, int bcof) { | |
float tempRead = analogRead(pin); | |
// convert the value to resistance | |
tempRead = 1023 / tempRead - 1; | |
tempRead = seriesr / tempRead; | |
float steinhart; | |
steinhart = tempRead / thermistnominal; // (R/Ro) | |
steinhart = log(steinhart); // ln(R/Ro) | |
steinhart /= bcof; // 1/B * ln(R/Ro) | |
steinhart += 1.0 / (tempnom + 273.15); // + (1/To) | |
steinhart = 1.0 / steinhart; // Invert | |
steinhart -= 273.15; // convert to C | |
return steinhart; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment