# Temperature different on LCD and Serial printouts

**URL:** <https://forum.arduino.cc/t/temperature-different-on-lcd-and-serial-printouts/1268997>\
**Category:** Science and Measurement\
**Created:** [June 7, 2024, 2:38am UTC](https://forum.arduino.cc/t/temperature-different-on-lcd-and-serial-printouts/1268997 "2024-06-07T02:38:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![disco\_spidah](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/disco_spidah/32/1077817_2.png) [@disco\_spidah](https://forum.arduino.cc/u/disco_spidah)\
**Post date:** [June 7, 2024, 2:38am UTC](https://forum.arduino.cc/t/temperature-different-on-lcd-and-serial-printouts/1268997/1 "2024-06-07T02:38:54Z")

</div>

Hello everyone,

I am building a water flow (DN80) and water temperature sensor (LM34) as a project. I am running into an issue where when I run my temperature sensor through the serial monitor, it gives me an accurate temperature (around 70 F), but when I load it to my code to display on my ILI9341(2.4" LCD) display it gives me a number that is around ~495 F. Below is the code for the temperature sensor, via serial monitor, followed by the LCD code I am using.

Any and all help/advice is greatly appreciated!

```arduino
float temp;

void setup() {
  Serial.begin(9600);
}

void loop() {
  temp = float(analogRead(A0)) * 500 / 1024;
  if (temp > 68) {
    LCD.print("Too Hot for Fishing!");
    Serial.print(temp);
    Serial.println(" F");
    delay(1000);
  } else if (temp < 48) {
    Serial.print("Too Cold for Fishing!");
    Serial.print(temp);
    Serial.println(" F");
    delay(1000);
  } else {
    Serial.print("Good for Fishing!");
    Serial.print(temp);
    Serial.println(" F");
    delay(1000);
  }
}

```

```arduino
//Settings for YF-DN80
const int input = A5; // Pin the sensor is connected to
float pipe_diameter = 62.5; // internal pipe diameter in mm80 = default
int sample_time = 10; // number of seconds to average for the reading
float unit_frequency = .55; //frequency from sensor data sheet

// touch screen settings
// copy-paste results from TouchScreen_Calibr_native.ino
#define MINPRESSURE 100 //min press down pressure
#define MAXPRESSURE 1000 // max press down pressure

const int XP = 8, XM = A2, YP = A3, YM = 9; //ID=0x9341
const int TS_LEFT = 100, TS_RT = 903, TS_TOP = 78, TS_BOT = 888;

//global variables
unsigned long X; //used for measuring the signal from sensor
unsigned long Y; //used for measuring the signal from sensor
unsigned long Z; //temp probe
float WATER = 0; //L/M
float V = 0; // KM/h
float temperature; 
unsigned long startTime = 0; // when did the current reading start
unsigned long samples_taken = 0; // how many samples have been taken so far during the current time span
int pulse = 0;  
int lastPulse = 0;  
int rotations = 0;
unsigned long readTime = 0;

//libraries
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>

// Touchscreen global variables
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button settings_btn, main_btn, pipe_diameter_p_btn,pipe_diameter_n_btn, sample_time_p_btn, sample_time_n_btn, sensor_f_p_btn, sensor_f_n_btn;
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

int pixel_x, pixel_y; //Touch_getXY() updates global vars
int program_mode = 0; //0 is main screen without initial things drawn //is the main screen once initial stuff is drawn
bool initial = true;

bool Touch_getXY(void)
{

    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT); //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH); //because TFT control pins
    digitalWrite(XM, HIGH);
    
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
    }

    return pressed;
}

void setup(void)
{
    uint16_t ID = tft.readID();
    tft.begin(ID);
    tft.setRotation(0); //PORTRAIT
    tft.fillScreen(BLACK);
    pinMode(input, INPUT);
}

void loop(void)
{

    //use analog pin A5 to count rotations
    unsigned long startReadTime = millis();
    pulse = digitalRead(input);
    while(startReadTime - millis() <= 1000)
    {
      if (pulse != lastPulse) {
       lastPulse = pulse;
       if (pulse == HIGH) rotations++;
      } 
    }
    readTime = readTime + (millis()-startReadTime); // keep track of how long we have been taking 1 second readings for final calculations
    
   // test();
 
   bool down = Touch_getXY();
   settings_btn.press(down && settings_btn.contains(pixel_x, pixel_y));
   main_btn.press(down && main_btn.contains(pixel_x, pixel_y));
 
    if (settings_btn.justPressed()) 
    {
      program_mode = 1;
      initial=true;   
      settings_btn.drawButton(true);
    }
    if (main_btn.justPressed()) 
    {
      program_mode = 0;
      initial=true;
      main_btn.drawButton(true);
    }   
    switch (program_mode)
    {
      case 0:
          drawMainScreen();
      break;
      case 1:
          drawSettingsScreen();
      break;      
      default:
        drawMainScreen();
      break;
    }
    
}

void drawMainScreen(void)
{
    if(initial)
    {
      tft.fillScreen(BLACK);
      showmsgXY(20, 10, 3, NULL, "Temperature:");
      showmsgXY(20, 160, 3, NULL, "Flow Rate:");
      settings_btn.initButton(&tft, 60, 275, 70, 40, WHITE, CYAN, BLACK, "CFG_HOLD", 1);
      settings_btn.drawButton(false);
      main_btn.initButton(&tft, 185, 275, 70, 40, WHITE, CYAN, BLACK, "Main", 1);
      main_btn.drawButton(false);      
      rotations = 0;
    }
    if(startTime == 0)
      startTime = millis();

    unsigned long currentTime = millis();

    if( (currentTime - startTime)/1000 > sample_time-1)//sample time -1 due to the fact that the main loop waits a full second to take readings
    {

      WATER = (rotations/(readTime/1000))/unit_frequency; //L/M
      float conversion = (1000000 / ((pipe_diameter/2)*(pipe_diameter/2) * 3.14) * 60)/1000000;
      V = conversion*WATER;
      temperature = float(analogRead(A0)) * 500 / 1024;
      String temp = String(temperature);
      temp = temp + " F";
      String val = String(V);
      val = val + " KP/h";
      tft.fillRect(0,40,400,40,BLACK);
      showmsgXY(20,40,3,NULL,temp);

      float conversion2 = (1000000 / ((pipe_diameter/2)*(pipe_diameter/2) * 3.14) * 60)/1000000 *0.27778;
      V = conversion2*WATER;
      val = String(V);
      val = val + " MP/s";
      tft.fillRect(0,80,400,40,BLACK);
      showmsgXY(20,80,3,NULL,val);
 
      val = String(WATER);
      val = val + " L/m";
      tft.fillRect(0,200,400,40,BLACK);
      showmsgXY(20,200,3,NULL,val);
      startTime = millis();
      
      
      readTime = 0;
      rotations = 0;
    } 
   
    initial = false;
}

void drawSettingsScreen(void)
{
    String val;

    if(initial == true)
    {
      tft.fillScreen(BLACK);
      showmsgXY(20, 10, 2, NULL, "Pipe Dia (mm):");
      showmsgXY(20, 80, 2, NULL, "Sample Time:");      
      showmsgXY(20, 155, 2, NULL, "Sensor Frequency:");  
      
      settings_btn.initButton(&tft, 60, 275, 70, 40, WHITE, CYAN, BLACK, "CFG_HOLD", 1);
      settings_btn.drawButton(false);
      main_btn.initButton(&tft, 185, 275, 70, 40, WHITE, CYAN, BLACK, "Main", 1);
      main_btn.drawButton(false);    

      //pipe_diameter buttons
      pipe_diameter_p_btn.initButton(&tft, 30, 50, 30, 40, WHITE, CYAN, BLACK, "+", 4);
      pipe_diameter_p_btn.drawButton(false);
      pipe_diameter_n_btn.initButton(&tft, 200, 50, 30, 40, WHITE, CYAN, BLACK, "-", 4);
      pipe_diameter_n_btn.drawButton(false);    
      
      //sample_time buttons
      sample_time_p_btn.initButton(&tft, 30, 130, 30, 40, WHITE, CYAN, BLACK, "+", 4);
      sample_time_p_btn.drawButton(false);
      sample_time_n_btn.initButton(&tft, 200, 130, 30, 40, WHITE, CYAN, BLACK, "-", 4);
      sample_time_n_btn.drawButton(false);    

      //sensor frequency buttons
      sensor_f_p_btn.initButton(&tft, 30, 200, 30, 40, WHITE, CYAN, BLACK, "+", 4);
      sensor_f_p_btn.drawButton(false);
      sensor_f_n_btn.initButton(&tft, 200, 200, 30, 40, WHITE, CYAN, BLACK, "-", 4);
      sensor_f_n_btn.drawButton(false);    
    
      val = String(pipe_diameter,1);
      tft.fillRect(60,50,100,30,BLACK);
      showmsgXY(60,40,3,NULL,val);
      val = String(sample_time);
      tft.fillRect(60,120,100,30,BLACK);
      showmsgXY(60,120,3,NULL,val);
      val = String(unit_frequency);
      tft.fillRect(60,190,100,30,BLACK);
      showmsgXY(60,190,3,NULL,val);
      
      initial = false;
      
  }
  bool down = Touch_getXY();
  pipe_diameter_p_btn.press(down && pipe_diameter_p_btn.contains(pixel_x, pixel_y));
  pipe_diameter_n_btn.press(down && pipe_diameter_n_btn.contains(pixel_x, pixel_y));
  sample_time_p_btn.press(down && sample_time_p_btn.contains(pixel_x, pixel_y));
  sample_time_n_btn.press(down && sample_time_n_btn.contains(pixel_x, pixel_y));    
  sensor_f_p_btn.press(down && sensor_f_p_btn.contains(pixel_x, pixel_y));
  sensor_f_n_btn.press(down && sensor_f_n_btn.contains(pixel_x, pixel_y));   
       
  if (pipe_diameter_p_btn.justPressed()) 
  {
  
    pipe_diameter = pipe_diameter + .1;
    val = String(pipe_diameter,1);
      tft.fillRect(60,40,100,30,BLACK);
      showmsgXY(60,40,3,NULL,val);
      delay(50);
  
  }
  if (pipe_diameter_n_btn.justPressed()) 
  {
    if(pipe_diameter > 0 )
      pipe_diameter = pipe_diameter - .1;
      val = String(pipe_diameter,1);
      tft.fillRect(60,40,100,30,BLACK);
      showmsgXY(60,40,3,NULL,val);
      delay(50);
  }  
  
  if (sample_time_p_btn.justPressed()) 
  {
    sample_time++;
    val = String(sample_time);
    tft.fillRect(60,120,100,30,BLACK);
    showmsgXY(60,120,3,NULL,val);
     delay(50);
  }
  if (sample_time_n_btn.justPressed()) 
  {
    if(sample_time > 1)
      sample_time--;
    val = String(sample_time);
     tft.fillRect(60,120,100,30,BLACK);
    showmsgXY(60,120,3,NULL,val);
     delay(50);
  }    
  if (sensor_f_p_btn.justPressed()) 
  {
    unit_frequency = unit_frequency + .01;
    val = String(unit_frequency);
    tft.fillRect(60,190,100,30,BLACK);
    showmsgXY(60,190,3,NULL,val);
     delay(50);
  }
  if (sensor_f_n_btn.justPressed()) 
  {
    if(unit_frequency > 1)
      unit_frequency = unit_frequency -.01;
    val = String(unit_frequency);
     tft.fillRect(60,190,100,30,BLACK);
    showmsgXY(60,190,3,NULL,val);
    delay(50);
  }   
  
}

void showmsgXY(int x, int y, int sz, const GFXfont *f, float msg)
{
  int16_t x1, y1;
  uint16_t wid, ht;
  // tft.drawFastHLine(0, y, tft.width(), WHITE);
  tft.setFont(f);
  tft.setCursor(x, y);
  tft.setTextColor(GREEN);
  tft.setTextSize(sz);
  tft.print(msg);
}

void showmsgXY(int x, int y, int sz, const GFXfont *f, int msg)
{
  int16_t x1, y1;
  uint16_t wid, ht;
  // tft.drawFastHLine(0, y, tft.width(), WHITE);
  tft.setFont(f);
  tft.setCursor(x, y);
  tft.setTextColor(GREEN);
  tft.setTextSize(sz);
  tft.print(msg);
}

void showmsgXY(int x, int y, int sz, const GFXfont *f, String msg)
{
  int16_t x1, y1;
  uint16_t wid, ht;
  // tft.drawFastHLine(0, y, tft.width(), WHITE);
  tft.setFont(f);
  tft.setCursor(x, y);
  tft.setTextColor(GREEN);
  tft.setTextSize(sz);
  tft.print(msg);
}

void showmsgXY(int x, int y, int sz, const GFXfont *f, unsigned long msg)
{
  int16_t x1, y1;
  uint16_t wid, ht;
  // tft.drawFastHLine(0, y, tft.width(), WHITE);
  tft.setFont(f);
  tft.setCursor(x, y);
  tft.setTextColor(GREEN);
  tft.setTextSize(sz);
  tft.print(msg);
}

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [June 7, 2024, 3:03am UTC](https://forum.arduino.cc/t/temperature-different-on-lcd-and-serial-printouts/1268997/2 "2024-06-07T03:03:12Z")

</div>

Check to make sure the ILI9341 does not use A0.

---

<div class="post-metadata">

**Author:** ![disco\_spidah](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/disco_spidah/32/1077817_2.png) [@disco\_spidah](https://forum.arduino.cc/u/disco_spidah)\
**Post date:** [June 7, 2024, 4:06am UTC](https://forum.arduino.cc/t/temperature-different-on-lcd-and-serial-printouts/1268997/3 "2024-06-07T04:06:23Z")

</div>

It does use the A0 pin. I soldered the input of the temp probe to the bottom side of that pin. Is it most likely drawing an incorrect voltage?

Is there a way around that? I am using an uno board for reference.

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [June 7, 2024, 5:04am UTC](https://forum.arduino.cc/t/temperature-different-on-lcd-and-serial-printouts/1268997/4 "2024-06-07T05:04:40Z")

</div>

> [@disco\_spidah](#):
>
> `temp = float(analogRead(A0)) * 500 / 1024;`

As an example, this would be better to avoid the risk of integer division:  
`temp = float(analogRead(A0)) * 500.0 / 1024.0;`

**EDIT**  
In the loop() there appears to be only one attempt to write to what appears to be an LCD display and that contains no explicit float number:

> [@disco\_spidah](#):
>
> ```arduino
> void loop() {
> temp = float(analogRead(A0)) * 500 / 1024;
> if (temp > 68) {
> LCD.print("Too Hot for Fishing!");
> Serial.print(temp);
> Serial.println(" F");
> delay(1000);
> . . .
> . . .
> 
> ```

Can you post more of the code which highlights this error.  
Indeed, 495F is too hot for fishing!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 4, 2024, 5:05am UTC](https://forum.arduino.cc/t/temperature-different-on-lcd-and-serial-printouts/1268997/5 "2024-12-04T05:05:24Z")

</div>

Closed on December 4, 2024, 5:05am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
