Trouble with fans always on

Hi I need some quick help on this I am working on a project and I am doing it by parts like testing my sensors and understanding them before using them all together
So I have this lm35 temperature sensor and it works fine and displays the temperature fine, but I need to use 2 fans and a heater that I have not included, I added the fans and added some if statements to control when the fans are on and when they are off
But the problem I have is that the fans they are always on

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 4);

int tempPin = A0;
int fan1 = 6;
int fan2 = 7;

void setup(){
	// initialize the LCD
	lcd.begin();
	// Turn on the blacklight and print a message.
	lcd.backlight();
  pinMode(fan1,OUTPUT);
  pinMode(fan2,OUTPUT);
  Serial.begin(9600);
 
	
}

void loop() {
  digitalWrite(fan1,LOW);
  digitalWrite(fan2,LOW);
  int reading = analogRead(tempPin);

  float voltage = reading * (5000/1024.0);
  float temp = voltage/10;

  Serial.println("Temperatura= ");
  Serial.print(temp);

  lcd.setCursor(0,0);
  lcd.print("Temp= ");
  lcd.print(temp);
  lcd.print("C");
  delay(1000);

  if(temp > 20 && temp < 28){
    digitalWrite(fan1,LOW);
    digitalWrite(fan2,LOW);
  } 
  
  if(temp >= 29){
    digitalWrite(fan1,HIGH);
    digitalWrite(fan2,HIGH);
  }
  
}

that is my code not sure what is wrong with the if statements or something that I am missing I am still new to arduino
Also I am using a 4relay module to control my fans so I have 5V on one side to the Vcc of the module the ground to that same ground and VCC pin that is next to the relay pins I have it on 5V on the arduino and tested with just turning off and on the fans and it works but when I include my temperature sensor something went wrong

Is LOW on or off?

void loop() {[color=#222222][/color]
  digitalWrite(fan1,LOW);[color=#222222][/color]
  digitalWrite(fan2,LOW);[color=#222222][/color]
  int reading = analogRead(tempPin);[color=#222222][/color]

EVERY time you start the loop you write low.

You write the fans HIGH, then microseconds later at the top of loop, you write them LOW!

Start with a simple sketch to determine if LOW switches the fans on or off (I suspect that it's the former). Adjust your digitalWrites if needed.

If the temp is above 29, you change the outputs. Next loop() starts again and you change the output again. That's less than a millisecond.

In your loop code there is:
if (temp > 20 && temp < 28) {

Change this to
if ((temp > 20) && (temp < 28)) {

S.

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