Having problems with my project

So I'm a newbie and I've built a little project, a temperature sensor lm35 connected to my arduino uno and LCD I2C that show what's the temperature in celsius and fahrenheit, but I'm having problems.
It doesn't work properly, it shows different temperature every few seconds, from around 0.003 to around 5-15 to around 20-30.
The amount of seconds is the same as the delay command I write in the code, at the end of this code there is a delay(5000) and the numbers change every 5 seconds, if I change it to delay(3000) the numbers change every 3 seconds.

I don't know if the issue is in the code or if I just built it wrong.

code.txt (1.85 KB)

Please post your code.
In code tags

//load libraries
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

//Define variables for the LCD

#define I2C_ADDR          0x3F        //Define I2C Address
#define BACKLIGHT_PIN      3
#define En_pin             2
#define Rw_pin             1
#define Rs_pin             0
#define D4_pin             4
#define D5_pin             5
#define D6_pin             6
#define D7_pin             7

//define variables for the LM35 temperature sensor

float temp;        //Define the temp float variable
float tempf;       //Define the Fahrenheit float variable
int sensor = 0;     // sensor middle pin on analog pin 0

//Initialise the LCD
LiquidCrystal_I2C      lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup()
 {
    //Define the LCD as 16 column by 2 rows 
    lcd.begin (16,2);
    
    //Switch on the backlight
    lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
    lcd.setBacklight(HIGH);
    
    //goto first column (column 0) and first line (Line 0)
    lcd.setCursor(0,0);
    
    //Print at cursor Location
    lcd.print("Temp C = ");
    
    //goto first column (column 0) and second line (line 1)
    lcd.setCursor(0,1);
    
    //Print at cursor Location
    lcd.print("Temp F = ");
    
 }
 
 
void loop()
{
  temp = analogRead(sensor);        //assigning the analog output to temp
  temp = temp * 0.48828125;         //converting volts to degrees celsius ----- 0.48828125 = [(5V*1000)/1024]10
  tempf = (temp * 1.8)+32;          //Converting from celsius to Fahrenheit
  
  lcd.setCursor(8,0);               //move the cursor to position 8 on row 1
  lcd.print(temp);                  //print the temperature in Celsius
  
  lcd.setCursor(8,1);               //move the cursor to position 8 on row 2
  lcd.print(tempf);                 //print the temperature in Fahrenheit  

 delay(5000);
}

Carefully check your wiring and LM35 data sheet. It appears to me, in the fritz, that the sensor (TO92 package?) is connected backwards.

groundfungus:
Carefully check your wiring and LM35 data sheet. It appears to me, in the fritz, that the sensor (TO92 package?) is connected backwards.

Thank you.
What is a TO92 package?
The left pin is now connected to 5V and the right pin is now connected to ground, still the same problem.

If it was backwards before it might be dead. Don't take my word from my interpretation of a fritzing picture. Get the data sheet and make sure of the pinout (page 3) before you connect it.

groundfungus:
If it was backwards before it might be dead. Don't take my word from my interpretation of a fritzing picture. Get the data sheet and make sure of the pinout (page 3) before you connect it.

The pinout is ok.
Probably a silly question, the middle pin (which is the output of the sensor) is connected to A0 on the arduino, where is it mentioned in the code?

Here temp = analogRead(sensor);

AWOL:
Here temp = analogRead(sensor);

Is it dead for sure? There isn't a change it's some other mistake?

int sensor = 0;     // sensor middle pin on analog pin 0

AWOL:

int sensor = 0;     // sensor middle pin on analog pin 0

what shall I do with this?

Leave it as it is, or change it to const byte sensor = 0;

AWOL:
Leave it as it is, or change it to const byte sensor = 0;

Do you think the sensor is dead? Is there a way to check?

This is kinda offtopic, but avoid using delay :slight_smile:

if(millis() - current) >= 5000{
current = millis();
...
...
}

is better if you want, for example, to have more than one sensor at time with different acquisition frequencies

diogotec:
is better if you want, for example, to have more than one sensor at time with different acquisition frequencies

I don't understand what you mean by that, as I as I'm new to this.

and how and where do I use this piece of code?

By frequencies I mean the numbers of acquisition you do in a second! You can have a sensor for which u have 4 acquisitions per second (4Hz) and another with another acquisiton frequency.
What delay does to arduino is that it stops the arduino from running other code, it just freezes - for professional purposes this is not good!

The code is the following (see if you understand :)):

unsigned long current = 0;
void loop()
{
if(millis() - current >= 5000){
current = millis();

temp = analogRead(sensor); //assigning the analog output to temp
temp = temp * 0.48828125; //converting volts to degrees celsius ----- 0.48828125 = [(5V*1000)/1024]10
tempf = (temp * 1.8)+32; //Converting from celsius to Fahrenheit

lcd.setCursor(8,0); //move the cursor to position 8 on row 1
lcd.print(temp); //print the temperature in Celsius

lcd.setCursor(8,1); //move the cursor to position 8 on row 2
lcd.print(tempf); //print the temperature in Fahrenheit
}
}

Hodor:
I don't understand what you mean by that, as I as I'm new to this.

and how and where do I use this piece of code?

Have a look at the blink without delay example sketch in the IDE

@Hodor, stop cross-posting.