# Help Printing data to LCD

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>

// 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;
while(precision--) mult *=10;
if(val >= 0) frac = (val - int(val)) * mult; else frac = (int(val) - val) * mult;
unsigned long frac1 = frac;
Serial.print(frac,DEC) ;
}
}

void setup() {
Serial.begin(115200);

}

#define ThermistorPIN 0   // Analog Pin 0
double temp;
void loop() {
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();

}
``````

Can you tell more about the LCD used? Type?
Is it connected to the serial port?
Does the serial monitor show some output?

Rob

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.

I apologize for leaving out those details.

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 am using pins 7, 8, 9, 10, 11, 12 for the LCD.

You got it working? It looks as if your displaying data.

Give this code a go. I just edited it to print to an lcd instead of the serial monitor. Tell me how it goes, for adjustments.

``````#include <LiquidCrystal.h>
#include <math.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
long Resistance;
double Temp;
Temp = log(Resistance);
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15;
return Temp;
}

void printDouble(double val, byte precision) {

lcd.print (int(val));
if( precision > 0) {
lcd.print(".");
unsigned long frac, mult = 1;
while(precision--) mult *=10;
if(val >= 0) frac = (val - int(val)) * mult;
else frac = (int(val) - val) * mult;
unsigned long frac1 = frac;
lcd.print(frac,DEC) ;
}
}

void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
}

#define ThermistorPIN 0
double temp;
void loop() {
lcd.setCursor(0, 1);
lcd.print("Sen.1 ");
printDouble(temp,2);
lcd.print("c ");
temp = (temp * 9.0)/ 5.0 + 32.0;
lcd.print("");
printDouble(temp,2);
lcd.println("f");
delay(1000);

}
``````

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.

Working Code

``````#include <LiquidCrystal.h>
#include <math.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
long Resistance;
double Temp;
Temp = log(Resistance);
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15;
return Temp;
}

void printDouble(double val, byte precision) {

lcd.print (int(val));
if( precision > 0) {
lcd.print(".");
unsigned long frac, mult = 1;
while(precision--) mult *=10;
if(val >= 0) frac = (val - int(val)) * mult;
else frac = (int(val) - val) * mult;
unsigned long frac1 = frac;
lcd.print(frac,DEC) ;
}
}

void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
}

#define ThermistorPIN 0
#define Thermistor2PIN 1
double temp;
void loop() {
lcd.setCursor(0, 0);
lcd.print("Sensor 1 ");
printDouble(temp,2);
lcd.print((char)223);
lcd.print("C");
lcd.print((char)223);
lcd.setCursor(0, 1);
lcd.print("Sensor 2 ");
printDouble(temp,2);
lcd.print((char)223);
lcd.print("C");
lcd.print((char)223);
//uncomment the next 4 lines for temp display in F
//temp = (temp * 9.0)/ 5.0 + 32.0;
//lcd.print("");
//printDouble(temp,3);
//lcd.println("f");
delay(500);

}
``````

Thanks Again for all the help!!!!!!!!!!!!!!!