TEMP BASED FAN CONTROL + LDR

Hell0,

I am working on Temp controlled fan, below code working fine untill i added LDR sensor,

can anyone help me to correct below code.

#include <LiquidCrystal.h>
//Student
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int tempPin = A1;   // the output pin of LM35
int LDRpin = A2;
int fan = 9;       // the pin where fan is
int led = 13;        // led pin
int led1 = 6;
int temp;
int tempMin = 20;   // the temperature to start the fan
int tempMax = 30;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
int ldrLCD;
int sensorValue;

void setup() {
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(tempPin, INPUT);
  pinMode(LDRpin, INPUT);
  lcd.begin(16,2);
  Serial.begin(9600); //sets serial port for communication  
}

void loop() {  
  temp = readTemp();     // get the temperature
  if(temp < tempMin) {   // if temp is lower than minimum temp
    fanSpeed = 0;      // fan is not spinning
    digitalWrite(fan, LOW);       
  } 
  {
  // read the value from the sensor:
  sensorValue = analogRead(LDRpin);    
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  
  if(sensorValue > 400) //setting a threshold value
  digitalWrite(6,HIGH); //turn relay ON
  
  else digitalWrite(6,LOW); //turn relay OFF              
}
  if((temp >= tempMin) && (temp <= tempMax)) {  // if temperature is higher than minimum temp
    fanSpeed = map(temp, tempMin, tempMax, 0, 255); // the actual speed of fan
    fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
    analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
  } 

  if(temp > tempMax) {        // if temp is higher than tempMax
    digitalWrite(led, LOW);  // turn on led 
  } 
  else {                    // else turn of led
    digitalWrite(led, HIGH); 
  }

  lcd.print("TEMP: ");
  lcd.print(temp);      // display the temperature
  lcd.print("C ");
  lcd.print("LIGHT");
  lcd.setCursor(0,1);   // move cursor to next line
  lcd.print("FAN: ");
  lcd.print(fanLCD);    // display the fan speed
  lcd.print("%    ");
  lcd.print(sensorValue);
  delay(200);
  lcd.clear();   
}

int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
}

void loop() {
temp = readTemp(); // get the temperature
if(temp < tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
}
{ // misplaced curly bracket?
// read the value from the sensor:
sensorValue = analogRead(LDRpin);
Serial.println(sensorValue); //prints the values coming from the sensor on the screen

if(sensorValue > 400) //setting a threshold value
digitalWrite(6,HIGH); //turn relay ON

else digitalWrite(6,LOW); //turn relay OFF
}

I don’t know if this is your problem, but I don’t think this is right.

That curly bracket isn't doing any good, but it isn't doing any harm either.

The arduino only has 1 ADC so it's shared among the analog pins. Reading from one pin can impact the reading from the next. A simple solution is to read each pin twice and ignore the first result. What temperature does the LCD show? What does it show if you comment out the call to read the LDR?

Hello,

Thanks for prompts responce.

Just modified code as below… also refer video for my problem. @wildbill could you please guide me that how to read pin twise and ignor the first result ? or help me to correct in my code. i need correct data which is resulting on display.

//Temprature Based fan control and LDR based light control
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int tempPin = A1;   // the output pin of LM35
int LDRpin = A2;    // the output pin of LDR
int fan = 9;       // the pin where fan driver is connected
int led = 13;      // led pin
int temp;
int tempMin = 20;   // the temperature to start the fan
int tempMax = 30;   // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
int ldrLCD;
int sensorValue;
void setup() {
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  pinMode(LDRpin, INPUT_PULLUP);
  lcd.begin(16,2);
}
void loop() {
    // read the value from the LM35 sensor:
  sensorValue = analogRead(LDRpin);
  Serial.println(sensorValue);    //prints the values coming from the sensor on the screen
  if(sensorValue > 500) {        // if temp is higher than tempMax
    digitalWrite(led, HIGH);      // turn ON led
  }
  else {
    digitalWrite(led, LOW); // else turn off led
  }
  temp = readTemp();     // get the temperature
  if(temp < tempMin)
  {   // if temp is lower than minimum temp
    fanSpeed = 0;      // fan is not spinning
    digitalWrite(fan, LOW);
  }
  if((temp >= tempMin) && (temp <= tempMax))
  {  // if temperature is higher than minimum temp
    fanSpeed = map(temp, tempMin, tempMax, 0, 255); // the actual speed of fan
    fanLCD = map(temp, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
    analogWrite(fan, fanSpeed);                    // spin the fan at the fanSpeed speed
 } 
  lcd.print("TEMP: ");
  lcd.print(temp);      // display the temperature
  lcd.print("C ");
  lcd.print("LIGHT");
  lcd.setCursor(0,1);      // move cursor to next line
  lcd.print("FAN: ");
  lcd.print(fanLCD);      // display the fan speed
  lcd.print("%    ");
  lcd.print(sensorValue/10);
  lcd.print("%    ");
  delay(1000);
  lcd.clear();
}
int readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
}

below youtube link.

Youtube video:-

To read twice, just do this:

  // read the value from the LM35 sensor:
  sensorValue = analogRead(LDRpin);
  sensorValue = analogRead(LDRpin);
  Serial.println(sensorValue);    //prints the values coming from the sensor on the screen

Ok let me check..

little bit improved but not upto mark...

can we connect LDR to digital pin? if two analog inputs create problem than?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?
A picture of your project showing your connections would be helpful.

Tom.... :slight_smile:

followed below link.

and added LDR sensor extra at A2,

Hi, how have you got the LDR connected to A2?

Tom… :slight_smile: