Yes, if I run a code where it detects the temperature only it works fine, if I power on the circuit (with the fan and everything) with only a USB it also works fine- issue being that since its only 5V its not powering on fully. The main issue starts after i plug in the DC power adapter to power on the fan fully.
Also I added in a 1k Ohm resistor to the output of the LM35 in case it might work and added some more stuff into the code :
#include <LiquidCrystal_I2C.h>
// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2); //address is 0x27
// Pin Definitions
const byte sampleBin = 8;
const int tempPin = A0; // LM35 out
const int fanPin = 9; // PWM control
const int ledPin = 6; // LED
// Calibration Constants
const int8_t fudge = 90; // Adjust this for calibration (±5~10 usually fine)
const int kAref = 1090; // Analog reference voltage in mV (1.1V = 1100mV)
const int kSampleBin = sampleBin * 1000;
// Temperature&Controlvars
int tempC, tempF;
int pwm;
bool fanOn = false;
uint32_t temp = 0, start = 0;
const int interval = 500; // Refresh rate
void setup() {
analogReference(INTERNAL);
analogRead(tempPin); // Dummy read
// Pre-fill temp with average of 8 samples
for (int i = 0; i < sampleBin; i++)
temp += analogRead(tempPin);
lcd.init();
lcd.backlight();
pinMode(fanPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (millis() - start > interval) {
start = millis();
// average of samples
temp -= (temp / sampleBin);
temp += analogRead(tempPin);
// Convert to °C
tempC = temp * kAref / (kSampleBin + fudge);
tempF = (tempC * 18 + 3200) / 10;
// LCD Display
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(tempC / 10);
lcd.print(".");
lcd.print(tempC % 10);
lcd.print((char)223);
lcd.print("C ");
// Fan Control with Hysteresis
if (!fanOn && tempC >= 160) { // 16.0°C
fanOn = true;
} else if (fanOn && tempC <= 140) { // 14.0°C
fanOn = false;
}
pwm = fanOn ? map(tempC, 150, 450, 100, 255) : 0;
pwm = constrain(pwm, 0, 255);
analogWrite(fanPin, pwm);
// Overheat LED
digitalWrite(ledPin, tempC > 400 ? HIGH : LOW); // 40.0°C threshold
// Serial Debug
Serial.print("ADC: ");
Serial.print(analogRead(tempPin));
Serial.print(" | Temp: ");
Serial.print(tempC / 10);
Serial.print(".");
Serial.print(tempC % 10);
Serial.print(" C | Fan PWM: ");
Serial.println(pwm);
}
}
