arduino , bluetooth/pc and triac based speed control of ac fan using phase angle control method


in this speed of home fan controlled using pc and also tested from android mobile using its bluetooth and android app "Bluetooth Rc controller".In this temperature sensor (LM35)  also used to control the speed of motor.
Internal EEPROM also used to remember  last speed of fan in case power failure at the time of sleeping.
  • Triac-BTA20 (driver -moc3021)
  • SNUBBER FOR TRIAC -100nF-400V CAPACITOR AND 100 OHMS-2WATT RESISTOR 
  • Fan -230V AC ,60W
WAVEFORMS
yellow-zero crossing out and blue- traic gate pulse at 60% speed

yellow-zero crossing out and blue- traic gate pulse at 80% speed

yellow-zero crossing out and blue- traic gate pulse at 100% speed

yellow-zero crossing out and blue- traic gate pulse at 0% speed
PROGRAM (ARDUINO)
#include <EEPROM.h>
#define TRIAC 9 
#define zerocrossingpin A2 
#define TEMPERATURESENSOR A0 
int temperature,thresholdtemperature=38;
unsigned short int ZERO,triggertime,oldtriggertime,tempflag=0;
char uart_data;//SERIAL COMMUNICATION
void setup() 
Serial.begin(9600);  
pinMode(TRIAC, OUTPUT);Serial.println("haiu");
pinMode(zerocrossingpin,INPUT_PULLUP);digitalWrite(TRIAC , LOW);
triggertime=EEPROM.read(10);   
 delay(100);
}

void loop() 
{
if(triggertime>0 && triggertime<9)
{  
waitfornextzerocrossing ();delay(triggertime);    digitalWrite(TRIAC, HIGH);
delayMicroseconds(200); digitalWrite(TRIAC , LOW);            
}
else if(triggertime==0 )digitalWrite(TRIAC, HIGH); 
else if(triggertime>10 )digitalWrite(TRIAC, LOW); 
bluetooth();
temperaturecheck();

}
void temperaturecheck(void)
{
readtemperature();
if(temperature>45  && tempflag==0 ){
  EEPROM.write(10,triggertime);
  oldtriggertime=triggertime; tempflag=1;triggertime=0; Serial.println(oldtriggertime);
  }
else if(tempflag==1 && temperature<40)
{
  triggertime=oldtriggertime;tempflag=0;  EEPROM.write(10,triggertime);
}
}
void bluetooth(void)
{
 if(Serial.available()>0)
  {
    uart_data=Serial.read();
    if(uart_data=='1')
    {
    triggertime=8;readtemperature();
readtemperature();if(temperature>thresholdtemperature){ triggertime=7;}
 EEPROM.write(10,triggertime);
    }
    else if(uart_data=='2')
    {
      triggertime=7;
      readtemperature();if(temperature>thresholdtemperature){ triggertime=6;}
       EEPROM.write(10,triggertime);
    }
    else if(uart_data=='3' || uart_data=='4' )
    {
    triggertime=5;
    readtemperature();if(temperature>thresholdtemperature){ triggertime=4;}
     EEPROM.write(10,triggertime);
    }
    else if(uart_data=='5' || uart_data=='6')
    {
     triggertime=4;
    readtemperature();if(temperature>thresholdtemperature){ triggertime=3;}
     EEPROM.write(10,triggertime);
    }
    else if(uart_data=='7' || uart_data=='8')
    {
     triggertime=2;
           readtemperature();if(temperature>thresholdtemperature){ triggertime=0;}
            EEPROM.write(10,triggertime);
    }
     else if(uart_data=='8' || uart_data=='9')
    {
     triggertime=0; EEPROM.write(10,triggertime);
    }
      else if(uart_data=='0')
    {
     triggertime=30; EEPROM.write(10,triggertime);
    }
     
    
}}
void readtemperature(void)
{
temperature = analogRead(TEMPERATURESENSOR)/2;
}
void waitfornextzerocrossing (void)
{
 do
   { 
     ZERO =digitalRead(zerocrossingpin); 
   }
   while(!ZERO); //wait until zero reaches--HIGH PULSE
}