Basically its an arduino project in which i am using an analog temperature sensor ky 013. The sensor is reading the temperatures correctly but what i want is to print the temperatures showed on the serial monitor to a lcd screen. I did the wiring and everything correctly but there is something wrong with the code which i couldnt figure out. Here is the code.
#include <math.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd = LiquidCrystal(12, 11, 5, 4, 3, 2);
const int sensorPin = A0;
int switchState=0;
double Thermistor(int RawADC)
{
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15;
return Temp;
}
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(sensorPin,INPUT);
}
void loop()
{
switchState=digitalRead(sensorPin);
int readVal=analogRead(sensorPin);
double temp = Thermistor(readVal);
Serial.print("Temp. : ");
Serial.print(temp);
Serial.println(" Celsius");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C");
delay(500);
}
1 Answer 1
Please format your code. I know it takes longer, but the payoff is in readability and maintainability. It's well worth the extra effort.
LiquidCrystal lcd = LiquidCrystal(12, 11, 5, 4, 3, 2);
const int sensorPin = A0;
int switchState = 0;
double Thermistor(int RawADC)
{
double Temp;
Temp = log(((10240000 / RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp)) * Temp);
Temp = Temp - 273.15;
return Temp;
}
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(sensorPin, INPUT);
}
void loop()
{
switchState = digitalRead(sensorPin);
int readVal = analogRead(sensorPin);
double temp = Thermistor(readVal);
Serial.print("Temp. : ");
Serial.print(temp);
Serial.println(" Celsius");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C");
delay(500);
}
I think the problem is the
lcd.print(temp);
Try casting it to a float from a double (and two decimal places). Maybe you just need the decimal places.
lcd.print((float)temp, 2);
Ctrl+K
to have your browser do this for you.