I am having a bit of trouble printing the Temp Data to the LCD. Can anyone help me out. LCD is hooked up correctly and thermistor is reading correctly. Serial output is within .01 deg C of my laser thermometer.
The code below is the code I am using to output the data to the serial console.
#include <math.h>
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"
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) ;
}
}
void setup() {
Serial.begin(115200);
}
#define ThermistorPIN 0 // Analog Pin 0
double temp;
void loop() {
temp=Thermistor(analogRead(ThermistorPIN)); // read ADC and convert it to Celsius
Serial.print("Sen.1 "); printDouble(temp,2); // display Celsius
Serial.print("c ");
temp = (temp * 9.0)/ 5.0 + 32.0; // converts to Fahrenheit
Serial.print(""); printDouble(temp,2); // display Fahrenheit
Serial.println("f"); // End of Line
delay(1000); // Delay a bit... for fun, and to not Serial.print faster than the serial connection can output
//lcd.clear();
}
Are you using a 16 pin hitachi config, serial, etc? I dont see that your printing any info to the lcd using lcd.print, so its a serial lcd? make shure that your lcds baud rate is at 115200 if it is serial.
The LCD is a standard 16x2 parallel LCD. I did not include any LCD code in the above code because all of my attempts failed. Every time I tried to output the data to the LCD weird things would happen that led me to believe that the math.h and LiquidCrystal.h libraries were not playing well together. I have attached a photo of the output problems at the bottom of this post.
First I tried using lcd.write to print the serial data to the LCD but realized the completed project will have more serial data than just the temperature info.
So I tried lcd.print(); printdouble(temp,2); and that failed to work also.
I had to modify the pin order in your revision but it works now. Thanks!!!!!!!!!!!!!!!!!!!!!!!!!
I also refined the code to have the data fit on my LCD and to include a second sensor. The only thing is that the LCD will not display a 0 if the second decimal point is a 0. It will instead drop that space and move the C into it. I also added the degree symbol for a more professional look.