Automatic Temperature Controlled Fan using LM35 and AVR Microcontroller

This AVR project automatically controls the fan when the temperature rises from the given limit. This is fan control using temperature sensor lm35 and AVR ATmega8. We can control any AC or DC fan by connecting it to the relay circuit.

Working Principle of Automatic Fan Controller

This is simply a fan that is automatically ON and OFF based on the temperature set by the user. There are some input switches by that user can set the temperature. We call it user temperature. When the room temperature rises above the user temperature then a signal goes to the relay. When the relay gets the signal it will ON the fan connected to it. When room temperature goes below the user temperature the fan will OFF.

By doing this it will maintain the desired temperature.

How to Set the Temperature

After the start of the system, it will ask for the user to enter the temperature. There are three switches to enter the desired temperature.

  • SW0 – Increase Temperature
  • SW1 – DecreaseTemperature
  • SW2 – Enter

Switches are connected at PC0, PC1 and PC2 of ATmega8 respectively. After setting user temperature the atmega8 continuously checks the room temperature. If the room temperature is greater than user temperature then a relay connected at PB5 switched ON and fan connected to it also ON.

How to Make Automatic Fan Controller

The main components of the fan controller are AVR ATmega8, LM35 temperature sensor, LCD display, relay circuit and input switches. For this automatic fan controller project, we should know the following interfacing and the programming with the AVR.

By understanding all the above we can design this fan controller project easily. The complete circuit of automatic fan controller is given in the circuit below.

Automatic Temperature Controlled Fan AVR Circuit

Automatic Temperature Controlled Fan AVR Code

#include <avr/io.h>
#include <avr/interrupt.h>
#include<util/delay.h>
#include”lcd.h”
char buffer[5];

int ReadADC(uint8_t ch)
{
ADMUX=ch;
ADCSRA |= (1<<ADSC);
while(!(ADCSRA&(1<<ADIF)));
ADCSRA|=(1<<ADIF);
return(ADC);
}

void initADC()
{
ADMUX=(1<<REFS0);  // For Aref=AVcc;
ADCSRA=(1<<ADEN)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1);//|(0<<ADPS0);
}
int start=0;
int main()
{
lcd_init(LCD_DISP_ON);
initADC();
int rtemp;
int utemp=0;
int flag=0;
int switchInput=0;
DDRB=0b00100000;
DDRC=0b0000000;

while(1)
{
switchInput = PINB&0b00000111;
lcd_gotoxy(0,0);
lcd_puts(“EnterTemperature”);
if(switchInput==0)
{
flag=1;
}
if(switchInput==1 && flag==1)
{
utemp=utemp+1;
flag=0;
}
if(switchInput==2 && flag==1)
{
utemp=utemp-1;
flag=0;
}
if(switchInput==4)
{
start=1;
}
lcd_gotoxy(0,1);
sprintf(buffer,”%d”,utemp);
lcd_puts(buffer);

if(start==1){
lcd_clrscr();
lcd_puts(“UserTemp=”);
lcd_gotoxy(9,0);
sprintf(buffer,”%d”,utemp);
lcd_puts(buffer);
lcd_gotoxy(13,0);
lcd_puts(“FAN”);
lcd_gotoxy(0,1);
lcd_puts(“RoomTemp=”);
lcd_gotoxy(9,1);
rtemp=ReadADC(0);
rtemp=rtemp/2;
sprintf(buffer,”%d”,rtemp);
lcd_puts(buffer);
if(rtemp>utemp)
{
PORTB|=(1<<PORTB5);//FAN ON
lcd_gotoxy(13,1);
lcd_puts(“ON”);
}
else
{
PORTB&=~(1<<PORTB5);//FAN OFF
lcd_gotoxy(13,1);
lcd_puts(“OFF”);
}
_delay_ms(800);
lcd_clrscr();
}
}
return 0;
}