GSM Based Home Automation using Arduino

Published  February 10, 2016   198
S Saddam
Author
GSM Based Home Automation Using Arduino

Mobile phone is a revolutionary invention of the century. It was primarily designed for making and receiving calls & text messages, but it has become the whole world after the Smart phone comes into the picture. In this project we are building a home automation system, where one can control the home appliances, using the simple GSM based phone, just by sending SMS through his phone. In this project, no Smart phone is needed, just the old GSM phone will work to switch ON and OFF any home electronic appliances, from anywhere. You can also check some more Wireless Home Automation projects here: IR Remote Controlled Home Automation using ArduinoBluetooth Controlled Home Automation along with DTMF Based Home AutomationPC Controlled Home Automation using Arduino.

If you are completely new to GSM module, you can also consider reading our Arduino GSM Module tutorial to understand the basics before proceeding with this project.  

Working Explanation

In this project, Arduino is used for controlling whole the process. Here we have used GSM wireless communication for controlling home appliances. We send some commands like “#A.light on*”, “#A.light off*” and so on for controlling AC home appliances. After receiving given commands by Arduino through GSM, Arduino send signal to relays, to switch ON or OFF  the home appliances using a relay driver.

 

Circuit Components:

  • Arduino UNO
  • GSM Module
  • ULN2003
  • Relay 5 volt
  • Bulb with holder
  • Connecting wires
  • Bread board
  • 16x2 LCD
  • Power supply
  • Cell phone

 

GSM Based Home Automation System Block diagram

 

Here we have used a prefix in command string that is “#A.”. This prefix is used to identify that the main command is coming next to it and * at the end of string indicates that message has been ended.

 

When we send SMS to GSM module by Mobile, then GSM receives that SMS and sends it to Arduino. Now Arduino reads this SMS and extract main command from the received string and stores in a variable. After this, Arduino compare this string with predefined string. If match occurred then Arduino sends signal to relay via relay driver for turning ON and OFF the home appliances. And relative result also prints on 16x2 LCD by using appropriate commands.

 

Here in this project we have used 3 zero watt bulb for demonstration which indicates Fan, Light and TV.

 

Below is the list of messages which we send via SMS, to turn On and Off the Fan, Light and TV:

 

S.no.

Message

Operation

1

#A.fan on*

Fan ON

2

#A.fan off*

Fan OFF

3

#A.light on*

Light ON

4

#A.light off*

Light OFF

5

#A.tv on*

TV ON

6

#A.tv off*

TV Off

7

#A.all on*

All ON

8

#A.all off*

All OFF

 

GSM Module:

GSM module is used in many communication devices which are based on GSM (Global System for Mobile Communications) technology. It is used to interact with GSM network using a computer. GSM module only understands AT commands, and can respond accordingly. The most basic command is “AT”, if GSM respond OK then it is working good otherwise it respond with “ERROR”. There are various AT commands like ATA for answer a call, ATD to dial a call, AT+CMGR to read the message, AT+CMGS to send the sms etc. AT commands should be followed by Carriage return i.e. \r (0D in hex), like “AT+CMGS\r”. We can use GSM module using these commands:

ATE0 - For echo off

AT+CNMI=2,2,0,0,0  <ENTER>          - Auto opened message Receiving.  (No need to open message)

ATD<Mobile Number>; <ENTER>    -  making a call (ATD+919610126059;\r\n)

AT+CMGF=1 <ENTER>                       - Selecting Text mode

AT+CMGS=”Mobile Number” <ENTER> - Assigning recipient’s mobile number

>>Now we can write our message

>>After writing message

Ctrl+Z  send message command (26 in decimal).

ENTER=0x0d in HEX

GSM Module SIM900A

The SIM900 is a complete Quad-band GSM/GPRS Module which delivers GSM/GPRS 850/900/1800/1900MHz performance for voice, SMS and Data with low power consumption.

 

Circuit Description

Connections of this GSM based home automation circuit are quite simple, here a liquid crystal display is used for displaying status of home appliances which is directly connected to arduino in 4-bit mode. Data pins of LCD namely RS, EN, D4, D5, D6, D7 are connected to arduino digital pin number 6, 7, 8, 9, 10, 11. And Rx and Tx pin of GSM module is directly connected at Tx and Rx pin of Arduino respectively. And GSM module is powered by using a 12 volt adaptor. 5 volt SPDT 3 relays are used for controlling LIGHT, FAN and TV. And relays are connected to arduino pin number 3, 4 and 5 through relay driver ULN2003 for controlling LIGHT, FAN and TV respectively.

GSM Based Home Automation System Circuit Diagram

Code Description

In programming part of this project, first of all in programming we includes library for liquid crystal display and then we defines data and control pins for LCD and home appliances.

#include<LiquidCrystal.h>
LiquidCrystal lcd(6,7,8,9,10,11);

#define Fan 3
#define Light 4
#define TV 5

int temp=0,i=0;
int led=13;

After this serial communication is initialized at 9600 bps and gives direction to used pin.

void setup()
{
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(led, OUTPUT);
   pinMode(Fan, OUTPUT);
    pinMode(Light, OUTPUT);
     pinMode(TV, OUTPUT);

For receiving data serially we have used two functions one is Serial.available which checks whether any serial data is coming and other one is Serial.read which reads the data that comes serially.

 while (Serial.available()) 
      {
      char inChar=Serial.read();

After receiving data serially we have stored it in a string and then waiting for Enter.

void serialEvent() 
 {
  while(Serial.available()) 
  {
    if(Serial.find("#A."))
    {
      digitalWrite(led, HIGH);
      delay(1000);
      digitalWrite(led, LOW);
      while (Serial.available()) 
      {
      char inChar=Serial.read();
      str[i++]=inChar;
      if(inChar=='*')
      {
        temp=1;
        return;
      } 

When Enter comes program start to compare received string with already defined string and if string matched then a relative operation is performed by using appropriate command that are given in code.

void check()
{
   if(!(strncmp(str,"tv on",5)))
    {
      digitalWrite(TV, HIGH);
      lcd.setCursor(13,1); 
      lcd.print("ON    ");
      delay(200);
    }  
   
   else if(!(strncmp(str,"tv off",6)))
    {
      digitalWrite(TV, LOW);
      lcd.setCursor(13,1); 
      lcd.print("OFF    ");
      delay(200);
    }
Code

#include<LiquidCrystal.h>
LiquidCrystal lcd(6,7,8,9,10,11);

#define Fan 3
#define Light 4
#define TV 5

int temp=0,i=0;
int led=13;

char str[15];
void setup()
{
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(led, OUTPUT);
   pinMode(Fan, OUTPUT);
    pinMode(Light, OUTPUT);
     pinMode(TV, OUTPUT);
  
  lcd.setCursor(0,0);
  lcd.print("GSM Control Home");
  lcd.setCursor(0,1);
  lcd.print("   Automaton    ");
  delay(2000);
  lcd.clear();
  lcd.print("Circuit Digest");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("System Ready");
  Serial.println("AT+CNMI=2,2,0,0,0");
  delay(500);
  Serial.println("AT+CMGF=1");
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Fan   Light  TV ");
  lcd.setCursor(0,1);
  lcd.print("OFF    OFF   OFF "); 
}

void loop()
{
  lcd.setCursor(0,0);
  lcd.print("Fan   Light  TV");
  if(temp==1)
  {
    check();
    temp=0;
    i=0;
    delay(1000);
  }
}

 void serialEvent() 
 {
  while(Serial.available()) 
  {
    if(Serial.find("#A."))
    {
      digitalWrite(led, HIGH);
      delay(1000);
      digitalWrite(led, LOW);
      while (Serial.available()) 
      {
      char inChar=Serial.read();
      str[i++]=inChar;
      if(inChar=='*')
      {
        temp=1;
        return;
      } 
      } 
    }
   }
 }

void check()
{
   if(!(strncmp(str,"tv on",5)))
    {
      digitalWrite(TV, HIGH);
      lcd.setCursor(13,1); 
      lcd.print("ON    ");
      delay(200);
    }  
   
   else if(!(strncmp(str,"tv off",6)))
    {
      digitalWrite(TV, LOW);
      lcd.setCursor(13,1); 
      lcd.print("OFF    ");
      delay(200);
    }
  
    else if(!(strncmp(str,"fan on",5)))
    {
      digitalWrite(Fan, HIGH);
      lcd.setCursor(0,1); 
      lcd.print("ON   ");
      delay(200);
    }
 
    else if(!(strncmp(str,"fan off",7)))
    {
      digitalWrite(Fan, LOW);
      lcd.setCursor(0,1); 
      lcd.print("OFF    ");
      delay(200);
    }
 
    else if(!(strncmp(str,"light on",8)))
    {
      digitalWrite(Light, HIGH);
      lcd.setCursor(7,1); 
      lcd.print("ON    ");
      delay(200);
    }
 
    else if(!(strncmp(str,"light off",9)))
    {
      digitalWrite(Light, LOW);
      lcd.setCursor(7,1); 
      lcd.print("OFF    ");
      delay(200);
    } 
    
    else if(!(strncmp(str,"all on",6)))
    {
      digitalWrite(Light, HIGH);
      digitalWrite(Fan, HIGH);
      digitalWrite(TV, HIGH);
      lcd.setCursor(0,1); 
      lcd.print("ON     ON    ON  ");
      delay(200);
    }
 
    else if(!(strncmp(str,"all off",7)))
    {
      digitalWrite(Light, LOW);
      digitalWrite(Fan, LOW);
      digitalWrite(TV, LOW);
      lcd.setCursor(0,1); 
      lcd.print("OFF   OFF    OFF  ");
      delay(200);
    }     
}

Video

Have any question realated to this Article?

Ask Our Community Members

Comments

Submitted by Trinity on Tue, 10/18/2016 - 04:07

Permalink

can the same program run on arduino mega? ... I can't find uno board in my area but I've a mega board. how can I modify the circuit or /and program to suite mega board?

Submitted by Arif Zaman on Thu, 10/20/2016 - 21:07

Permalink

I have uploaded the code and made the circuit accordingly but now its not connecting with the gsm module allthough all the connections right. i am using SIM900A mini

Submitted by kiri on Fri, 10/21/2016 - 15:05

Permalink

hye sir, ur code work very fine .. thank u very much .

instead of that , can i know how i want to reset each load . not all of it . its like when the load is ON . but when i sent message OFF . its does't off. so i think if i could put switch . and when i push the switch . my load is reset and it off . but i want it to work each load . means in ur code there 3load . so it will be 3 switch for reset .

sorry for asking too much . can i do that and how i want to do it .

Submitted by Lokesh Kumar on Wed, 10/26/2016 - 15:00

Permalink

Relay is On position when arduino powered up ,this will effect at project functioning bcz if power supply gone then all device is automatically shut down, if i connected with AC/DC adapter ,,,how to invert relay on and off in program(CODE) ,,,connection to nc/oc or oc/nc is not a proper solution

i tried but but relay is not working,,,,,,every thing is gud and working properly.but when i send msg display showed that device is on but relay is not working with arduino board ,i checked connection many times everything is gud , i also checked this on bread board And DIY PCB ,but relays is not working ,,what can i do sir ,,,and how to invert relays in program, i am using arduina uno,and arduino nano,sim900, please help

Submitted by kuldeep panchal on Wed, 11/02/2016 - 20:36

Permalink

dear sir i am using gsm900a, i made this cicuit, and relay is working properly from serial monitor, but relay is not working from gsm 900a gsm module , relay is not working through send sms
please sir solve this problem

Submitted by kiri on Wed, 11/02/2016 - 21:36

Permalink

sir in ur code what the mean of this

else if(!(strncmp(str,"all off",7)))

what 7 is representing for ? i bit confuse because there number in each of ur code .
and i face some problem when i change the name . like changing the light to " socketa" . when i sent #A.socketa on* . it does not work . why sir ?

Submitted by Ray on Wed, 11/23/2016 - 15:18

Permalink

someone please help me, can I connect two or more smoke sensor ( MQ ) in one board..???
how to recognition or how to give address one by one of sensor , so I can se where sensor has detect smoke.
thank's

Submitted by H.B.Chandrathilaka on Fri, 11/25/2016 - 15:00

Permalink

Sir, please send me complet code to control home appliences with arduino and sim 900 serial communication(mentioned arduino board type)

Submitted by PRAVEEN on Sun, 11/27/2016 - 22:26

Permalink

//YOUR CODE NOT WORKING
//=============================//
//THIS IS WORKING CODE TO USE
//=============================//
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
#define Fan 3
#define Light 4
#define TV A0
int temp=0,i=0;
int led=13;
char str[15];
void setup()
{

mySerial.begin(9600);
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(Fan, OUTPUT);
pinMode(Light, OUTPUT);
pinMode(TV, OUTPUT);
//========================================
Serial.println("GSM Control Home");
Serial.println(" Automaton ");
delay(2000);
Serial.println("System Ready");
//========================================
mySerial.println("AT+CNMI=2,2,0,0,0");
delay(500);
mySerial.println("AT+CMGF=1");
delay(1000);

}

void loop()
{

if(temp==0)
{
serialEvent();
temp=0;
i=0;
delay(1000);
}
}

void serialEvent()
{
while(mySerial.available())
{
if(mySerial.find("#A."))
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
while (mySerial.available())
{
char inChar=mySerial.read();
str[i++]=inChar;
if(inChar=='*')
{
check();
temp=1;
return;
}
}
}
}
}
void check()
{
if(!(strncmp(str,"tv on",5)))
{
digitalWrite(TV, HIGH);
delay(200);
}

else if(!(strncmp(str,"tv off",6)))
{
digitalWrite(TV, LOW);
delay(200);
}

else if(!(strncmp(str,"fan on",5)))
{
digitalWrite(Fan, HIGH);
delay(200);
}

else if(!(strncmp(str,"fan off",7)))
{
digitalWrite(Fan, LOW);
delay(200);
}

else if(!(strncmp(str,"light on",8)))
{
digitalWrite(Light, HIGH);
delay(200);
}

else if(!(strncmp(str,"light off",9)))
{
digitalWrite(Light, LOW);
delay(200);
}

else if(!(strncmp(str,"all on",6)))
{
digitalWrite(Light, HIGH);
digitalWrite(Fan, HIGH);
digitalWrite(TV, HIGH);
delay(200);
}

else if(!(strncmp(str,"all off",7)))
{
digitalWrite(Light, LOW);
digitalWrite(Fan, LOW);
digitalWrite(TV, LOW);
delay(200);
}
}

Submitted by RAMASWAMY on Thu, 12/01/2016 - 16:07

Permalink

C:\Users\NTSCHYD\Documents\Arduino\gsm\gsm.ino: In function 'void serialEvent()':

C:\Users\NTSCHYD\Documents\Arduino\gsm\gsm.ino:59:25: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

if(Serial.find("#A."))

^

Sketch uses 4,024 bytes (12%) of program storage space. Maximum is 32,256 bytes.
Global variables use 501 bytes (24%) of dynamic memory, leaving 1,547 bytes for local variables. Maximum is 2,048 bytes.

SENT ME THE WHAT WE DID. WHERE WE DO THE PROBLEM
KINDLY SENT REPLY

Submitted by Vikash Kumar on Tue, 12/13/2016 - 15:22

Permalink

Sir , GSM based home automation is my final year project. So , can you please provide me the source code for the same and full process of making the project and full details of components required for the same?

Submitted by Muthuramalingam on Fri, 12/16/2016 - 21:23

Permalink

Sir this project coding is correct or not .this code fake or true . I will try to project so please give me the true coding sir and my friends .thank you......

Submitted by Vikash Kumar on Sat, 12/17/2016 - 15:41

Permalink

when i am trying to compile your code , i am getting the error given below. Please help me to resolve this error.
Rebuild target 'Target 1'
assembling STARTUP.A51...
compiling l.c...
l.c(1): warning C318: can't open file 'LiquidCrystal.h'
l.c(2): error C129: missing ';' before 'lcd'
Target not created.
Build Time Elapsed: 00:00:00

Submitted by susmi on Sun, 12/18/2016 - 12:37

Permalink

i am trying this project but i am getting compilation error unable to rename core.a reason permission denied..if please anyone know this answer me ....thanks in advance

Submitted by Chethan L on Wed, 12/28/2016 - 11:36

Permalink

Hi i have done this project but my problem is
If i send any message to the gsm the gsm is receiving message but there is no response in Arduino output as well as in lcd display
The 3 outputs are remains in off position even when the msg is received
Please help me

Submitted by oemamu on Tue, 01/10/2017 - 14:24

Permalink

First thank you being you are voluntary to help us.
my question is , on what software I will be run this program?

Submitted by berlin benilo on Tue, 01/24/2017 - 23:48

Permalink

Sir, how can I provide an input in simulation(now the bulb is off but I need to on through simulation. How? ?)

Submitted by Siddharth on Fri, 02/17/2017 - 19:16

Permalink

Sir shall i use gsm sim900A for same code?

Submitted by Jai Ganesh on Wed, 03/01/2017 - 22:59

Permalink

Sir, you have defined a function serialEvent() .... bt you didn't called it anywhere in the program,,, Where should i have to call this funcation?

Submitted by dileep on Fri, 03/10/2017 - 20:50

Permalink

sir i just doing this project thku sir but i have face some problem on doing this project that is gsm receives the command and lcd it will displays but the load cant operates. .it does not receives the required supply from the Arduino through ic wht can I do sir can u pls tell me

Submitted by Divyesh on Sat, 03/11/2017 - 22:59

Permalink

When i tried to turn on the any equipment through text messag then its isnt working . But when i type the those command on serial monitor then its works .

What to do about this problem ?

Submitted by Ashok on Sun, 03/12/2017 - 18:37

Permalink

Can I use sim 900 in the place of sim900a.if I use it, is ther any instruction to be changed in the code.pls tell me.
Thank you

Submitted by Ajay on Sat, 03/18/2017 - 01:13

Permalink

Hello Sir,

Thanks for the project details, it is a great project my dream come true.
When I compile the sketch it gives the following error:
if(Serial.find("#A."))
^
Can you please provide me the coding for 8 channels relays to be controlled;
Fan
Light
TV
Main Door Light
Socket
Lamp
Mis 1
Mis 2

I can handle the electronics part but don't know anything about the coding part.

I will be grateful to you.

Regards,

Ajay

Submitted by ashok kumar on Mon, 03/20/2017 - 13:07

Permalink

my project is working good but i have one problem that
,when ever i gave a message for first time the output came but, in second time i can't get the output.i have to reset it everytime.please help me.this is my b.tech final year project.
Thank you

Submitted by Avinesh on Sun, 04/16/2017 - 08:02

Permalink

Sir ,
The code is not working properly. The GSM is responding for the first message .after that it is not responding.
And also the code is run fine in only in serial monitor. So I want to make it working full SMS continuously.what should I do for that.
Please ,send me the valid code for project.
I'm working on it

Submitted by saleh hayat on Wed, 04/26/2017 - 20:45

Permalink

i make this project its working well .but our project is to feedback msg to a personal contact no if current is zero or we remove load then we receive a massage .
this programme is need sir plz send me the edited code .......

Submitted by Shankar on Tue, 05/02/2017 - 13:03

Permalink

My code is not working Any one can help me then pls solve this code......
#include<GSM.h>
GSM_SMS sms;
int RedLed=11;
int BlueLed=10;
int GreenLed=9;
String msg;
void setup()
{
Serial.begin(9600);
pinMode(RedLed,OUTPUT);
pinMode(BlueLed,OUTPUT);
pinMode(GreenLed,OUTPUT);
digitalWrite(RedLed,LOW);
digitalWrite(BlueLed,LOW);
digitalWrite(GreenLed,LOW);
Serial.println("WelCome New SMS Tutorial...");
}
void loop()
{
char c;
if(sms.available()>0)
{
Serial.println("AT+CMGR");
while(c=sms.read())
msg=msg+c;
Serial.println(c);
msg.toLowerCase();
if(msg=="redon")
{
Serial.println(msg);
digitalWrite(RedLed,HIGH);
delay(20);
}
if(msg=="blueon")
{
Serial.println(msg);
digitalWrite(BlueLed,HIGH);
delay(20);
}
if(msg=="greenon")
{
Serial.println(msg);
digitalWrite(GreenLed,HIGH);
delay(20);
}
}
}

Submitted by naseer on Tue, 05/02/2017 - 14:19

Permalink

i dont want to use lcd display so is there any editing needed or can go ahead with same coding. plz help

Submitted by Muzmmal Hussain on Wed, 05/03/2017 - 11:50

Permalink

sir plz tell me the complete name and module number of gsm module

Hello daniyal asif i am mahesh i have taken this project for irrigation purpose that is to operate solenoid valve instead of bulb,
i have purchased all the components except LCD, but the problem is i am unable to load the program in to Aurdino will you please help me. I am waiting for your replay

thank you

Dear sir
I have seen ur home automation project,but I needed some help from you for making project on agriculture irrigation system , when electricity come at my farm I need msg on my phone power on ,then m using 3 phase sensor is there if 3phase ok again m need 1 more msg power ok then m given some command like pump 1 on or off,pump 2 on or off m using some valve and operate valve as per my recommendation valve 1 on or off or more valves

Submitted by May on Tue, 09/12/2017 - 10:01

Permalink

can I ask whats that third module? how did you do it? arduino, GSM module and the third one im a bit confused :(

Submitted by papile on Wed, 09/20/2017 - 13:57

Permalink

frist thanks for code about gsm based home automation but i have question on the arduino code
the mobile number is not mention in arduino code how do we put number of mobile

Submitted by luke on Sun, 10/08/2017 - 22:39

Permalink

sir how are you the code is okay can l use a ULM2004 in place of the ULM2003

Submitted by adams on Sat, 12/02/2017 - 05:05

Permalink

thanks for your effort but i have a problem having copied and paste your code to arduino ide i compiled and download, built the circuit as per your instruction but the far i can get is the lcd display fan off, light off tv off.
i sent a msg #A. fan on* but no response i tried with serial monitor too but no change pls advice thanks

Submitted by Anthony on Mon, 12/18/2017 - 23:44

Permalink

hi code does not work with Arduino ide any ideas.

Submitted by Daniel on Sat, 12/30/2017 - 21:54

Permalink

Hej! The code is working flawless. But I can't figure out how to make the code running without PC. Everything is working OK if the Arduino is connected to PC. If I connect it to an external 5V battery, my SMS will cause nothing to happen. Some additional info will be great!

Submitted by vignesh on Sat, 02/10/2018 - 13:39

Permalink

sir this method used to the microcontroller8051.how to put a program please hlp me sir.

Submitted by asim faraz on Thu, 02/15/2018 - 15:35

Permalink

sir, if i use 16chanel relay for more ac loads then what i changing in code? kindle relpy me.
2question. can i send sms any city to operate home automation

Submitted by Rishabh Jaiswal on Sun, 03/04/2018 - 14:50

Permalink

Sir,
If I use GSM module TTL then is there any changes in code?
If yes plz give me appropriate solution

Submitted by randhir on Wed, 03/07/2018 - 21:38

Permalink

i want relay on or off by a particular mobile number not any other number plz help me the code.

Submitted by Mokshada Rajes… on Tue, 03/27/2018 - 19:32

Permalink

Sir, I am working on this project but I got difficulties for connection can you send me picture of connections