0

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);
}
4
  • 1
    Please edit your post and format the code using the code formatting markdown (four leading spaces). For help see Markdown help. You should be able to do this by selecting the code and pressing Ctrl+K to have your browser do this for you. Commented Jan 28, 2016 at 22:25
  • You haven't actually said what's wrong. If we don't know what's happening, we can't tell you why happening. Commented Jan 29, 2016 at 11:54
  • Have you checked if the pin defining is right for the LCD? Commented Jan 30, 2016 at 16:35
  • Does the lcd show "Temp: " as expected? Commented Feb 2, 2016 at 19:53

1 Answer 1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.