Go Down

Topic: Help Printing data to LCD (Read 1 time) previous topic - next topic

TheMakersWorkbench

Jun 11, 2010, 08:21 am Last Edit: Jun 11, 2010, 08:23 am by TheMakersWorkbench Reason: 1
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.
Code: [Select]
#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();

}

robtillaart


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

Rob
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Casey Gross

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.

TheMakersWorkbench

#3
Jun 11, 2010, 09:14 pm Last Edit: Jun 11, 2010, 09:20 pm by TheMakersWorkbench Reason: 1
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.


Casey Gross

#4
Jun 11, 2010, 09:40 pm Last Edit: Jun 11, 2010, 09:40 pm by caseygross Reason: 1
You got it working? It looks as if your displaying data.

Casey Gross

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.
Code: [Select]
#include <LiquidCrystal.h>
#include <math.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);              
double Thermistor(int RawADC) {
 long Resistance;  
 double Temp;
 Resistance=((10240000/RawADC) - 10000);
 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;
   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");
   lcd.print(frac,DEC) ;
 }
}

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

#define ThermistorPIN 0  
double temp;
void loop() {
 lcd.setCursor(0, 1);
 temp=Thermistor(analogRead(ThermistorPIN));          
 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);                                    

}


TheMakersWorkbench

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
Code: [Select]
#include <LiquidCrystal.h>
#include <math.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);              
double Thermistor(int RawADC) {
 long Resistance;  
 double Temp;
 Resistance=((10240000/RawADC) - 10000);
 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;
   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");
   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);
 temp=Thermistor(analogRead(ThermistorPIN));        
 lcd.print("Sensor 1 ");
 printDouble(temp,2);
 lcd.print((char)223);  
 lcd.print("C");
 lcd.print((char)223);
 lcd.setCursor(0, 1);
 temp=Thermistor(analogRead(Thermistor2PIN));
 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!!!!!!!!!!!!!!! :) :) :) :) :)

Go Up