Arduino Home Automation Using RF

Home automation is one of in demand concepts in today’s world. Hobbyists do simple automation systems with the components readily available. If we are more concerned about the reliability and performance of the system, then we can go for the expensive Home Automation Consoles.
Home automation reduces the physical efforts and integrates the control for any number of appliances in to a single control unit. Hence, a simple home automation system is a remote control of different electrical appliances i.e. turning them on or off with the help of a remote.
There are many ways to implement this remote control system. With the extensive use of smart phones and tablets, Bluetooth may be the best option to implement the home automation. And one is implemented here in Bluetooth based Home Automation using Arduino.  The other methods of remote controlled home automation system are ZigBee, Wi-Fi, Radio Frequency (RF Module), GSM etc.
In this project, a simple but efficient home automation system using RF Module (Transmitter – Receiver pair) is designed. The system is designed with Arduino (ATmega 328) as the main processing unit.

Circuit Diagram

The circuit diagram is divided in to the transmitter section and receiver section for easy understanding. The transmitter section of the project is shown on the following image.
transmitter
The receiver section of the project is shown in the following image.
receiver

Required Component

The list of components mentioned here are specifically for controlling two different loads. The list of components may vary if the no. of loads is more.
Transmitter Section
  • Arduino UNO
  • 434 MHz RF Transmitter
  • Push Buttons X 4
  • 1 KΩ Resistor X 4
  • Prototyping Board
  • 9V Battery
  • Connecting wires
Receiver Section
  • Arduino UNO
  • 434 MHz RF Receiver
  • 2N2222 NPN Transistor X 2
  • 1 KΩ Resistor X 2
  • 1N4007 PN Junction Diode X 2
  • 12V Relay X 2
  • Prototyping Board
  • Power Supply (Adapter)
  • Connecting wires

Component Description

Arduino UNO:
Two Arduino UNO’s is used in the project, one in the transmitter section which reads the inputs from switches and transmit the message and the other is used in receiver section to decode the message and control the loads.
434 MHz RF Module:
The 434 MHz Radio Frequency Transmitter – Receiver Module is the best and cheapest way to implement a wireless communication for a reasonably longer ranges.


Relay Board:

A relay board consists of all the components that are required for a relay to be operated by a microcontroller. A four channel relay board is used although only two relays are used in the practical implementation.

Circuit Design

The design of the circuit is explained with respect to transmitter section and receiver section individually.
Transmitter Circuit Design:
The first component we need to connect to the Arduino UNO is the RF Transmitter module. The data in pin of the RF Transmitter module is connected to the 8th digital I/O pin of Arduino. The other pins of the transmitter module i.e. VCC and GND are connected to 5V and ground pins of the Arduino respectively. An optional antenna wire can be connected to the antenna pin of the transmitter module.
The next step is to connect the push buttons. First, we need to pull down the digital I/O pins 3 through 6 with the help of four 1KΩ resistors. Then connect four switches to these four pins with the other ends of the switches connected to 5V supply.
Receiver Circuit Design:
The RF Receiver receives the data through RF links and must transmit this data to the Arduino. Hence, the data out pin of the receiver module must be connected to digital I/O pin 11 of the Arduino.
The VCC and GND pins of the receiver module are connected to 3.3V and ground pins of the Arduino. An antenna can be connected to the antenna terminal of the module.
If you are using a relay board, as we are in this project, simply connect the digital I/O pins 4 and 5 of the Arduino to the input pins of the relay.
If you are not using the relay board, the connections must be made as per the circuit diagram.
Note: We need to be extremely careful when connecting AC Mains supply to the relay board.

Project Working Process

Home Automation System is a useful and helpful way to manage electrical appliances without any physical contact with the switch. This is possible by utilizing the wireless communication technologies. In this project, an RF based home automation system is implemented using Arduino. The working of the project is explained here.
At the transmitter section, the Arduino continuously monitors the status of the switches (or buttons). Whenever a switch is pressed, a logic HIGH is detected at that particular I/O pin. As a result, the Arduino transmits a suitable message corresponding to the switch pressed.
For example, if LOAD1_ON switch, which is connected to pin 6, is pressed, Arduino detects a logic HIGH at pin 6. Hence, Arduino sends a message as “@ABC$” via the RF transmitter.
At the receiver end, the RF receiver receives this message and transmits the same to Arduino for decoding. When the Arduino at the receiver end decodes the message and understands that the transmitter characters are “@ABC$”, it then writes a HIGH signal on the digital I/O pin 4.
As a result, the relay connected to load 1 is activated and the load is turned on.
Similar actions are performed when other switches are pushed.
If there is any error in the data transmission i.e. the desired data is not transmitted, the Arduino at the receiver section lights up the error LED which is connected to the 13th pin.
A data transmission successful LED and an error buzzer can also be implemented to indicate those actions more efficiently.

Applications

  • Although Bluetooth based home automations are easy to implement as almost everyone has a smart phone, the advantage of the RF based home automation system is the range of communication. RF based system is useful if we have a large house where Bluetooth might go out of range.
  • The system can be expanded to a smart home system with security by integrating several sensors like temperature, humidity, light and security devices like burglar sensors, CCTV’s etc.

Project Images






CODE

/**** RF TRANSMITTER LINK SETUP ******/
#include <VirtualWire.h> 
//int RF_TX_PIN = 2;
#undef int
#undef abs
#undef double
#undef float
#undef round
char msg[6] = "";
int bulbON = 0;
int bulbOFF = 0;
int fanON = 0;
int fanOFF = 0;
int doorOPEN = 0;
int doorCLOSE = 0;
/**** RF TRANSMITTER LINK SETUP ******/

/**** PUSH BUTTON SETUP ******/
#define FAN_ON 8
#define FAN_OFF 7
#define DOOR_OPEN 6
#define DOOR_CLOSE 5
#define BULB_ON 4
#define BULB_OFF 3
/**** PUSH BUTTON SETUP ******/

/**** ALARM LED BUZZER SETUP ******/
#define BUZZER_PIN 9
#define LED_PIN 11
/**** ALARM LED BUZZER SETUP ******/

void setup()
{
  Serial.begin(9600);
  // Initialise the IO and ISR
  vw_set_ptt_inverted(true); // Required for DR3100
//  vw_set_tx_pin(RF_TX_PIN); // Setup transmit pin
  vw_setup(2000); // Transmission speed in bits per second.  
  
  // Setup Pin Mode as INPUT for Push Buttons
  pinMode(BULB_ON, INPUT);
  pinMode(BULB_OFF, INPUT);
  pinMode(FAN_ON, INPUT);
  pinMode(FAN_OFF, INPUT);
  pinMode(DOOR_OPEN, INPUT);
  pinMode(DOOR_CLOSE, INPUT);
  
  // Setup Pin Mode as OUTPUT for LED and BUZZER
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);  
}

void loop()
{
  // Start Get Push Buttons Data    
  bulbON = digitalRead(BULB_ON);
  bulbOFF = digitalRead(BULB_OFF);
  fanON = digitalRead(FAN_ON);
  fanOFF = digitalRead(FAN_OFF);
  doorOPEN = digitalRead(DOOR_OPEN);
  doorCLOSE = digitalRead(DOOR_CLOSE);  
  // End Get Push Buttons Data    
  
  if (bulbON == HIGH)
  {    
    strcpy(msg, "@BON$");    
  }
  
  if (bulbOFF == HIGH)
  {    
    strcpy(msg, "@BOFF$");    
  }
  
  if (fanON == HIGH)
  {
    strcpy(msg, "@FON$");    
  }
  
  if (fanOFF == HIGH)
  {
    strcpy(msg, "@FOFF$");    
  }
  
  if (doorOPEN == HIGH)
  {
    strcpy(msg, "@DON$");    
  }
  
  if (doorCLOSE == HIGH)
  {
    strcpy(msg, "@DOFF$");    
  }
  
  if(strlen(msg)>0)
  {    
    sendRFMsg(msg);    
  }
  
  strcpy(msg, "");  
  resetButtons();
}

// Send Message Via RF 433 MHz Link
void sendRFMsg(char msg[])
{  
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx(); // Wait until the whole message is gone   
  delay(400);
  LedBuzzer();
  Serial.print("Message Sent-->");
  Serial.println(msg);
}

// Send Message Via RF 433 MHz Link
void LedBuzzer()
{
  digitalWrite(BUZZER_PIN, HIGH);
  digitalWrite(LED_PIN, HIGH);  
  delay(500);
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(LED_PIN, LOW);  
}

// Reset variables for buttons
void resetButtons()
{
  bulbON = 0;
  bulbOFF = 0;
  fanON = 0;
  fanOFF = 0;
  doorOPEN = 0;
  doorCLOSE = 0;
}
*********************************************************************************
/**** RF RECEIVER LINK SETUP ******/
#include <VirtualWire.h> 
//int RF_RX_PIN = 2;
#undef int
#undef abs
#undef double
#undef float
#undef round
/**** RF RECEIVER LINK SETUP ******/

/**** ALARM LED BUZZER SETUP ******/
#define BUZZER_PIN 6
#define LED_PIN 7
#define ERROR_LED 8
/**** ALARM LED BUZZER SETUP ******/

/**** LED SETUP ******/
#define FAN_LED 3
#define DOOR_LED 4
#define BULB_LED 5
boolean bBulb = false;
boolean bFan = false;
int iFan = 0;
int iDoor = 0;
/**** LED SETUP ******/

/**** MOTOR SETUP ******/
#define DOOR_MOTOR_A A0
#define DOOR_MOTOR_B A1
#define FAN_MOTOR_A  A2
/**** MOTOR SETUP ******/

void setup()
{
  Serial.begin(9600);
  Serial.println("setup");
  // Initialise the IO and ISR
  vw_set_ptt_inverted(true); // Required for DR3100
  //vw_set_rx_pin(RF_RX_PIN);  // Setup receive pin.
  vw_setup(2000); // Transmission speed in bits per second.
  vw_rx_start(); // Start the PLL receiver.
  
  // Set pinmode for LED, BUZZERS
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(ERROR_LED, OUTPUT);
  pinMode(FAN_LED, OUTPUT);
  pinMode(DOOR_LED, OUTPUT);
  pinMode(BULB_LED, OUTPUT);
  bBulb = false;
  bFan = false;
  iDoor = 0;
}
void loop()
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;  
  if(vw_get_message(buf, &buflen)) // non-blocking I/O
  { 
    delay(500); 
    ProcessCommand ( (char *) buf);
    delay(500); 
  } 
  
  if(bBulb == true)
  {
    digitalWrite(BULB_LED, HIGH);
  }

  if(bBulb == false)
  {
    digitalWrite(BULB_LED, LOW);
  }
  
  if(iFan == 1)
  {
    digitalWrite(FAN_LED, HIGH);
    RunFan();
  }
  
  if(iFan == 2)
  {
    StopFan();
    digitalWrite(FAN_LED, LOW);
    iFan = 0;
  }
  
  if(iDoor == 1 )
  {
    digitalWrite(DOOR_LED, HIGH);
    OpenDoor();
    digitalWrite(DOOR_LED, LOW);
    iDoor =0;
  }
  else  if ( iDoor == 2)
  {
    digitalWrite(DOOR_LED, HIGH);
    CloseDoor();
    digitalWrite(DOOR_LED, LOW);
    iDoor =0;
  }
  else
  {
    iDoor = 0;
    digitalWrite(DOOR_LED, LOW);
  }
  
  /*Serial.println("Fan, Door, Lights");
  Serial.print(iFan);
  Serial.print(",");
  Serial.print(iDoor);
  Serial.print(",");
  Serial.print(bBulb);*/
  
}

void ProcessCommand(char msg[])
{
  if (strlen(msg) ==0) { ErrorAlarm(); return; }  
  
  Serial.println("Message Received-->");
  Serial.println(msg);  
   // check if the string doesn't have valid string
 if ( compareTag(msg, '@') == false || compareTag(msg, '$') == false) { ErrorAlarm(); return;}  
 SucessAlarm();
 Serial.println("Command Received-->");
 Serial.println(msg);  

 char cTag[5] ="";
 int index =0;
 boolean batFound = false;
 boolean dollarFound = false;
 int iLoop = IndexOf(msg,'@');
 if(iLoop == -1){ ErrorAlarm(); return;}
 iLoop++;
 while (iLoop < strlen(msg))
 {
   if(msg[iLoop] == '$'){break;}
   else{cTag[index] = msg[iLoop]; index++;}
   
   iLoop ++;
 }
 String sTag = String(cTag);

  if (sTag.equals("BON")) { bBulb = true; Serial.println("Compared BON - 1");}

  if (sTag.equals("BOFF")) { bBulb = false; Serial.println("Compared BOFF - 0");}

  if (sTag.equals("FON")) { iFan = 1; bFan = true; Serial.println("Compared FON -1");}

  if (sTag.equals("FOFF")) { iFan = 2; bFan = false; Serial.println("Compared FOFF - 0");}
  
  if (sTag.equals("DON")) { iDoor = 1; Serial.println("Compared DON - 1" ); }

  if (sTag.equals("DOFF")) { iDoor = 2; Serial.println("Compared DON - 2"); }
  
}

int IndexOf(char msg[], char tag)
{
  if (strlen(msg) ==0) return false;
  boolean bFlag = false;
  int iIndex = -1;
  
  for( int i=0; i<strlen(msg); i++)
  {
    if(msg[i]== tag)
    iIndex = i;
  }
  
  return iIndex;
}

boolean compareTag (char msg[], char tag)
{
  if (strlen(msg) ==0) return false;
  boolean bFlag = false;
  
  for( int i=0; i<strlen(msg); i++)
  {
    if(msg[i]== tag)
    bFlag = true;
  }
  
  return bFlag;
}

void RunFan()
{
  Serial.println("Running Fan");
  analogWrite(FAN_MOTOR_A, 150);  
}

void StopFan()
{
  Serial.println("Stopping Fan");
  analogWrite(FAN_MOTOR_A, 0);
  delay(50);
}

void OpenDoor()
{
  Serial.println("Open Door");
 analogWrite(DOOR_MOTOR_A, 150);
 analogWrite(DOOR_MOTOR_B, 0);
 delay(1000);
 analogWrite(DOOR_MOTOR_A, 0); 
 analogWrite(DOOR_MOTOR_B, 0);
}

void CloseDoor()
{
 Serial.println("Close Door");
 analogWrite(DOOR_MOTOR_A, 0); 
 analogWrite(DOOR_MOTOR_B, 150); 
 delay(1000);
 analogWrite(DOOR_MOTOR_A, 0);
 analogWrite(DOOR_MOTOR_B, 0);
}

// Function provides alarm in case of success
void SucessAlarm()
{
  digitalWrite(BUZZER_PIN, HIGH);
  digitalWrite(LED_PIN, HIGH);  
  delay(500);
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(LED_PIN, LOW);  
}

// Function provides alarm in case of error
void ErrorAlarm()
{
  digitalWrite(ERROR_LED, HIGH);
  digitalWrite(BUZZER_PIN, HIGH);
  delay(500);
  digitalWrite(ERROR_LED, LOW);
  digitalWrite(BUZZER_PIN, LOW);
}

No comments:

Post a Comment