As an Amazon Associate we earn from qualifying purchases.
This project shows how to switch a light on and off and run a fan at different speeds from your phone or computer over WiFi. An Arduino does the work. It listens for short text commands from a WiFi module and drives two triacs that switch the mains loads.
Watch the video first to see it working, then follow the circuit and the program below.
How the WiFi controlled fan and light works
The system has three parts that talk to each other in a chain:
- The WiFi module. It connects over the Arduino’s serial port. The code sends it AT commands at start-up, which suggests an ESP8266-style module. It runs a small TCP server on port 23 that accepts commands from your phone or PC.
- The Arduino. It reads each command from the serial port and decides whether to switch the light, switch the fan, or change the fan speed.
- The triacs. One triac switches the fan and the other switches the light. They are the only parts that handle mains voltage.
A zero-crossing signal is also fed to the Arduino. It lets the program know when the AC waveform passes through zero volts, which is what makes fan speed control possible.
Why a zero-crossing input is needed
A triac does not switch off on its own while current is flowing. It turns off when the AC waveform reaches zero. To slow a fan down, the Arduino waits for a zero crossing and then waits a short delay before triggering the triac. The later it fires in each half cycle, the less power the fan gets and the slower it spins. Firing right at the zero crossing gives full power.
The light does not need this. It is simply switched on or off, so the second triac gets a steady HIGH or LOW signal.
Circuit
The diagram below shows how the Arduino, the WiFi module, the zero-crossing detector and the triac stages are connected. In the code, the fan triac is on pin 9, the light triac is on pin A2 and the zero-crossing signal comes in on pin A5.
Safety first
This circuit connects to mains power, so treat it carefully.
- Keep the low-voltage side (Arduino and WiFi module) electrically isolated from the mains side, normally with optocouplers on the triac drivers and the zero-crossing detector.
- Test on the bench with a low-wattage bulb before connecting a fan.
- Never touch the board while it is plugged in, and mount it in an enclosed case when it is finished.
- If you are not comfortable working with mains wiring, ask a qualified electrician to help.
Commands the program understands
The Arduino reads the serial port and looks for a colon. When the module is in server mode, the colon normally marks the end of the incoming-data header, so the byte after it is your command. The program responds to these commands:
- A turns the light on.
- a turns the light off.
- B turns the fan on.
- b turns the fan off.
- + sets the fan speed. It works only while the fan is on. The program ignores the next two characters and then reads three digits from 000 to 255 as the speed value.
How the speed value is used
The three digits are converted into a number from 0 to 10 by multiplying by 10 and dividing by 255. That number is then mapped to a trigger delay in milliseconds:
- 1 gives a delay of 8 and 2 gives 7, which are the slowest settings.
- 3 or 4 gives 5, and 5 or 6 gives 4.
- 7 or 8 gives 2.
- Anything above 8 gives 0, which keeps the triac on for full speed.
- 0 gives 30, which is longer than a half cycle, so the fan stays off.
A longer delay means a slower fan. The value range is coarse, so this gives a handful of speed steps rather than smooth control.
Program
At start-up, the messagereadinit() function sends three AT commands to the module. AT+CWMODE=3 sets it to work as both a station and an access point. AT+CIPMUX=1 allows multiple connections. AT+CIPSERVER=1,23 starts the server on port 23. The serial port runs at 115200 baud, so the module must be set to the same rate.
The main loop either waits for a zero crossing and fires the fan triac, or holds it fully on, and calls wifi() regularly to check for new commands. I corrected one typo from the original listing: a variable written as triggetime is now triggertime. The original would not compile with that typo.
//#include <EEPROM.h>
#define TRIAC1 9
#define TRIAC2 A2
#define zerocrossingpin A5
unsigned short int ZERO,triggertime=20,fan=0;
char uart_data;//SERIAL COMMUNICATION
void setup()
{
Serial.begin(115200); delay(100);messagereadinit();
triacinit();
pinMode(zerocrossingpin,INPUT_PULLUP);
digitalWrite(TRIAC1, HIGH);delay(1000);digitalWrite(TRIAC1, LOW);
//triggertime=EEPROM.read(10);
}
void loop()
{
if(triggertime>0 && triggertime<9 && fan==1)
{
waitfornextzerocrossing ();
delay(triggertime); digitalWrite(TRIAC1, HIGH);
delayMicroseconds(200); digitalWrite(TRIAC1 , LOW);
}
else if(triggertime==0 && fan==1){digitalWrite(TRIAC1, HIGH);wifi();wifi();wifi();wifi();}
else if(triggertime>10 || fan==1 )digitalWrite(TRIAC1, LOW);
wifi();
if(triggertime<5){wifi();wifi();}
}
void triacinit(void)
{
pinMode(TRIAC1, OUTPUT);pinMode(TRIAC2, OUTPUT);
digitalWrite(TRIAC1 , LOW);digitalWrite(TRIAC2, LOW);
}
void wifi(void)
{
if(Serial.available()>0)
{
uart_data=Serial.read();
if(uart_data==':')
{
while(!(Serial.available()>0));
uart_data=Serial.read();
if(uart_data=='+' && fan==1)
{
fancontrol();
}
else if(uart_data=='A')
{
digitalWrite(TRIAC2, HIGH); // Serial.println("light ON");
}
else if(uart_data=='a')
{
digitalWrite(TRIAC2, LOW);// Serial.println("light OFF");
}
else if(uart_data=='B')
{
fan=1;// Serial.println("FAN ON");
}
else if(uart_data=='b')
{
fan=0;// Serial.println("FAN OFF");
}
}
}
}
void waitfornextzerocrossing (void)
{
do
{
ZERO =digitalRead(zerocrossingpin);
}
while(!ZERO); //wait until zero reaches--HIGH PULSE
}
void fancontrol(void)
{
while(!(Serial.available()>0));//skips 1st zero
uart_data=Serial.read();
while(!(Serial.available()>0));//skips 2nd zero
uart_data=Serial.read();
while(!(Serial.available()>0));//msb of speed
uart_data=Serial.read();
triggertime=100*(uart_data-0x30);
while(!(Serial.available()>0));//middle byte of speed
uart_data=Serial.read();
triggertime=triggertime + 10*(uart_data-0x30);
while(!(Serial.available()>0));//lsb byte of speed
uart_data=Serial.read();
triggertime=triggertime + (uart_data-0x30);
triggertime=triggertime*10/255;
if(triggertime==1)
{
triggertime=8;
// EEPROM.write(10,triggertime);
}
else if(triggertime==2)
{
triggertime=7;
// EEPROM.write(10,triggertime);
}
else if(triggertime==3 || triggertime==4)
{
triggertime=5;
// EEPROM.write(10,triggertime);
}
else if(triggertime==5 || triggertime==6)
{
triggertime=4;
// EEPROM.write(10,triggertime);
}
else if(triggertime==7 || triggertime==8)
{
triggertime=2;
// EEPROM.write(10,triggertime);
}
else if(triggertime>8)
{
triggertime=0;
//EEPROM.write(10,triggertime);
}
else if(triggertime==0)
{
triggertime=30;
//EEPROM.write(10,triggertime);
}
//Serial.print("triggertime=");Serial.println(triggertime);
}
void messagereadinit(void)
{
Serial.print("AT+CWMODE=3");//TEXT MODE
Serial.write(0xd);//ENTER
Serial.write(0xa);//NEW LINE
delay(500);
Serial.print("AT+CIPMUX=1");//TEXT MODE
Serial.write(0xd);//ENTER
Serial.write(0xa);//NEW LINE
delay(500);
Serial.print("AT+CIPSERVER=1,23");//TEXT MODE
Serial.write(0xd);//ENTER
Serial.write(0xa);//NEW LINE
delay(500);
}
Using it
- Build the circuit and upload the program to the Arduino.
- Power the board. The fan triac pulses for one second at start-up, which is a quick check that the output works.
- Connect your phone or PC to the WiFi network the module creates, or to the network it joins.
- Open a TCP connection to the module on port 23 using a terminal or a simple app.
- Send the commands from the list above to control the light and fan.
Troubleshooting and ideas
- No response from the module. Check that its baud rate matches the 115200 setting in the code and that it has a stable 3.3 V supply. Many of these modules do not run reliably from a weak supply.
- Fan hums or runs unevenly. Phase-angle control can make some fan motors buzz at low speeds. Try a higher speed setting, and check that the zero-crossing signal is clean.
- Fan stays on after sending the off command at full speed. When the delay is 0, the fan triac is held high. The final
else ifinloop()then does not run when the fan is off. Changing that line to a plainelse digitalWrite(TRIAC1, LOW);should fix it, but test it with a bulb first. - Saved speed. The EEPROM lines are commented out. Enable them if you want the fan to remember its last speed after a power cut.
If you are choosing hardware, read our guide to WiFi modules for Arduino. For a fuller build, there is a speed control version of this project. You can also try using a TV remote instead of WiFi. To understand the speed control method in more depth, see our article on triac based speed control. If you want the fan to react to room conditions instead, look at the temperature controlled fan project.
FAQ
Can I control the fan and light from my phone?
Yes. Any phone app or terminal that can open a TCP connection to the module’s port and send the text commands will work. You can also write a small app that sends the same characters.
Why does the project use triacs instead of relays?
A triac can be triggered at a chosen point in each AC half cycle, which is how the fan speed is changed. A relay can only switch fully on or off, so it works for a light but not for speed control.
Does it work on both 50 Hz and 60 Hz mains?
The program times its delay from each zero crossing, so it adapts to the line frequency. The delay values are fixed in milliseconds, though, so the speed steps may feel slightly different. You may need to adjust them.
Is it safe to build at home?
Only if you keep the mains and low-voltage sides properly isolated and take care with the wiring. Test with a low-power load first, and ask a qualified person to check the build if you are unsure.