I've actually fixed it all now, like this time its final it actually works. I just did as Tom first recommended to me with doing it in steps instead of going random bullshit go.
In case anyones wondering , these are the photos:
As for the code:
#include <Wire.h> //enables i2c communication
#include <LiquidCrystal_I2C.h> //i2c library
const int tempPin = A0; //analog pin 0 from the arduino for i2c
const int fanPin = 9; // Fan connected through transistor
const int ledPin= 6; //pin 6 of arduino for LED
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD address 16 columns 2 rows
float Vout; //voltage output from lm35
float Temp; //calculated temp
int SensorValue; //raw analog reading from A0
void setup()
{
analogReference(INTERNAL); //uses the arduinos 1.1v internal reference for more accurate readings
Serial.begin(9600); //intialise serial comms for debugging
lcd.init(); //intialises LCD
lcd.backlight(); //turns on lcd backlight
pinMode(fanPin, OUTPUT); //set pin D9 as output to control fan
pinMode(ledPin, OUTPUT); //set pin D6 as output for overheating led
}
void loop()
{
const int samples = 100; //high samples for maximum accuracy
long total = 0; //sum of all valid sensor readings
int validSamples = 0; //counter for non zero samples in case of issue with lm35
for (int i = 0; i < samples; i++) {
SensorValue = analogRead(tempPin); //reads the analog value from lm35
if (SensorValue > 0) //to ignore any 0 values that kept messing up the sensor's readings
{
total += SensorValue;
validSamples++;
}
delay(5); //very short delay to reduce analog noise and false spikes
}
if (validSamples > 0) {
float avgSensorValue = total / (float)validSamples; //calculating average readings
Vout = avgSensorValue * (1.1 / 1023.0); // 1.1V internal ref conversion of raw
Temp = Vout * 100.0; //lm35s output is 10mV/C, so multiply by 100
// Fan PWM Mapping
int pwm = map(Temp, 15, 30, 0, 255); // Starts at 15°C, full speed at 30°C
pwm = constrain(pwm, 0, 255); //limits the pwm to 255
analogWrite(fanPin, pwm); //sets pwm to control fan control pin
int fanPercent = map(pwm, 0, 255, 0, 100); //for converting pwm to percentage
// LCD Output
lcd.setCursor(0, 0); //move to first line
lcd.print("Temp: ");
lcd.print(Temp, 1); //1 decimal place
lcd.print((char)223); //degree symbol
lcd.print("C "); // clears remainder
lcd.setCursor(0, 1); //move to second line
lcd.print("Fan: ");
lcd.print(fanPercent);
lcd.print("% "); // padding clears remainder
digitalWrite(ledPin, Temp >= 30 ? HIGH : LOW); //LED overheat signal at 30 degrees
//Serial Debug to serial monitor
Serial.print("Avg Temp (C) = ");
Serial.print(Temp, 1);
Serial.print(" | Fan PWM: ");
Serial.println(pwm);
} else
{
Serial.println("No valid samples (all zero)."); //this part is in case it doesnt display a bunch of nonsense and that i get a clear message on LCD in case somethings wrong
lcd.setCursor(0, 0);
lcd.print("Sensor Error ");
}
delay(500); //wait half a second before repeating loop
}
Thank you all for your time.


