Website in the process of migration. Original site located at https://sites.google.com/site/ryanpatricksnyder0
The first step towards automation that I wanted to incorporate was a temperature sensor. I bought some 10k ohm resistors and thermistors. Using a simple wiring schematic one can take an analog measurement through a micro-controller and determine the temperature in kelvin. Once you have that data value you can just convert it to your preferred unit, which I just converted to Celsius and then Fahrenheit for simplicity.
At first I wanted to use fancy display, or many LEDs to display the temperature but I realized that it would quickly use up too many pins on an Arduino. So instead I took the temperature and divided it by an integer of 10, which the number gets truncated after the decimal so you end up with the first (10's place) of a temperature. Then by subtracting the difference of the original by the new truncated number will return 1-9, or the second digit. It is probably easier to understand in code versus writing.
And here is the complete code, which displays the schematic of the Arduino circuit with the thermistor.
#include <math.h>
//Schematic:
double Thermistor(int RawADC) {
// Inputs ADC Value from Thermistor and outputs Temperature in Celsius
// requires: include <math.h>
// Utilizes the Steinhart-Hart Thermistor Equation:
// Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}
// where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08
long Resistance;
double Temp; // Dual-Purpose variable to save space.
Resistance=((10240000/RawADC) - 10000);
// Assuming a 10k Thermistor. Calculation is actually:
// Resistance = (1024 * BalanceResistor/ADC) - BalanceResistor
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later.
// "Temp" means "Temporary" on this line.
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
// Now it means both "Temporary" and "Temperature"
Temp = Temp - 273.15; // Convert Kelvin to Celsius
// Now it only means "Temperature"
// BEGIN- Remove these lines for the function not to display anything
Serial.print("ADC: ");
Serial.print(RawADC);
Serial.print("/1024"); // Print out RAW ADC Number
Serial.print(", Volts: ");
printDouble(((RawADC*4.860)/1024.0),3); // 4.860 volts is what my USB Port outputs.
Serial.print(", Resistance: ");
Serial.print(Resistance);
Serial.print("ohms");
// END- Remove these lines for the function not to display anything
// [Ground] ---- [10k-Resister] -------|------- [Thermistor] ---- [+5v]
// |
// Analog Pin 0
// Uncomment this line for the function to return Fahrenheit instead.
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit
return Temp; // Return the Temperature
}
void printDouble(double val, byte precision) {
// prints val with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimal places
// example: printDouble(3.1415, 2); // prints 3.14 (two decimal places)
Serial.print (int(val)); //prints the int part
if( precision > 0) {
Serial.print("."); // print the decimal point
unsigned long frac, mult = 1;
byte padding = precision -1;
while(precision--) mult *=10;
if(val >= 0) frac = (val - int(val)) * mult;
else frac = (int(val) - val) * mult;
unsigned long frac1 = frac;
while(frac1 /= 10) padding--;
while(padding--) Serial.print("0");
Serial.print(frac,DEC) ;
}
}
#define ThermistorPIN 0 // Analog Pin 0
//These pins are used for indicating the temperature, one counts how many times each
// color blinks with the blue one blinking first
//Ex: if blue blinks 6 times and UV twice, the temp=62
int led10 = 13; //blue led for giving first digit of water temperature by blinking x amount of times
int led1 = 12; //UV or purple LED for blinking x amount of times to display 2nd digit
void setup() {
Serial.begin(115200);
pinMode(led10, OUTPUT);
pinMode(led1, OUTPUT);
}
double temp;
int temp10; // Value used to first digit of temperature (10s place, 10,20,30,etc.)
int temp1; // Value used for second digit of temperature (values 1-9)
void loop() {
temp=Thermistor(analogRead(ThermistorPIN)); // read ADC and convert it to Celsius
//Serial.print(", Celsius: "); printDouble(temp,3); // display Celsius
temp = (temp * 9.0)/ 5.0 + 32.0; // converts to Fahrenheit
//Serial.print(", Fahrenheit: "); printDouble(temp,3); // display Fahrenheit
//*By dividing the temp by an integer it will truncate the remaining numbers after a decimal point
//** so 62/10 would return 6
temp10 = temp/10;
//*Then to find the second digit we can subtract the difference
//** of the two so 62-60 = 2
temp1 = temp-temp10;
//*This method was useful to make a temperature display that does not
//** use up too many pins and it still easy to understand
//Now time to display the temperature through blinking LEDs
//First is the blue light for the first digit
for (int i=1; i<=temp10; i++) //Blink once for every int value, could have started at 0 and just used a
//less than (<)
{
digitalWrite(led10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250); // wait for a 250ms, more than enough time to recognize a flash
digitalWrite(led10, LOW); // turn the LED off by making the voltage LOW
delay(800); // wait 800ms, can be adjusted however too slow is tedious and
//too fast is easy to miss a blink
}
//A delay here can be helpful for a gap between the two colors
//Second is the UV light for the second digit
for (int i=1; i<=temp1; i++) //Blink once for every int value, could have started at 0 and just
//used a less than (<)
{
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250); // wait for a 250ms, more than enough time to recognize a flash
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(800); // wait 800ms, can be adjusted however too slow is tedious and too
//fast is easy to miss a blink
}
delay(2000);
}
A video showing how the sensor works running the two colors:
I found that because the unit was outdoors the temperature of the tank was almost always the same temperature as the air. It did have a bit of lag time but it wasn't too much. So I didn't keep that one until I stared heating the water.