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 Dennis on Sat, 02/06/2016 - 10:32

Permalink

Is this Code really working sir?
I would like to ask for the complete code this sir, i'm interested in making this project sir! Please Send me the the Code sir!

Is it okay sir to use 5v-4 channels relay instead of 5v SPDT relay and a ULN2003 driver?
Cause i've been trying it using your code and a relay module but it doesn't work(still without an Output connected to it).

Hi Sadam,

I have a similar project where i would like to detect water leakage and send sms to my phone, im using use an Analogue water sensor, arduino uno and a gsm shield, would you help me with a code

Submitted by ashind on Wed, 02/10/2016 - 00:22

Permalink

what is this? is this code need any modification
E:\tim\tim.ino: In function 'void serialEvent()':

E:\tim\tim.ino:54:25: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

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

^

Submitted by Dennis on Thu, 02/11/2016 - 07:57

Permalink

Thanks to your code sir! It's working but the initial state of the relay module i used is ON, and all of the commands are inverted. How can i correct this sir?

Submitted by Leloko on Sun, 02/21/2016 - 00:52

Permalink

may you please provide the code for the Gsm based door unlock system
which allows the user to unlock the door remotely by sending an sms to the gsm module.
thank you

Dear
Sir
I followed the your projects Sir I want to make gsm relay contorl project
But I have questions
Gsm module how can I interface with gsm module?
Ofter set at commends do we need to delete them?
Can I use 3phase motor starter contorl?
Please explain me
Thank you very much

Submitted by SAJITH on Fri, 03/04/2016 - 09:48

Permalink

CAN U PLEASE HELP ME TO SET GSM MODULE (900A)) FOR THE SAME APPLICATION

Submitted by siva on Wed, 04/06/2016 - 23:40

Permalink

sir, i did as prescribed and it is working fine with the serial monitor. but when it comes to gsm module it does not respond to the second message. ie, if i send #A.tv on* for the first time , it works fine... but then the system does not respond to whatever message i send to the gsm module.... have been with this problem for a lot of time... please help me sir..

Submitted by sneha on Mon, 04/11/2016 - 17:36

Permalink

@ashind
i m also getting the same error
C:\Users\Home\Documents\Arduino\test11\test11.ino: In function 'void serialEvent()':

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

if(Serial.find("#A."))
how to get rid of this error??
kindly reply soon

Can i knw wat type of GSM that in use in tiz circuit .. bcos went i search Gsm Module its goes wrong can i get the full name of the GSM

Submitted by Asif Ather on Thu, 04/28/2016 - 17:16

Permalink

sir, can i use sim 900A inplace of sim900.
if yes, then is there any change in programme.

Submitted by Shri M on Sat, 05/07/2016 - 20:25

Permalink

Dear Sir,

Please send the GSM module to my email id. Your site is good and I am beginner. I wanted to learn.
[]

Submitted by ALLY on Wed, 05/11/2016 - 15:48

Permalink

Please can you help me i tried to send sms on my gsm but is not turning on any thing what is the problem and sms shows received in my mobile

Submitted by rex on Thu, 05/19/2016 - 21:22

Permalink

sir im using 12v/2A GSM module. can u please explain the current and voltage supply rating for the whole circuit. can i use 12v 2A adaptor for whole circuite..?

Submitted by afreen on Sun, 05/22/2016 - 16:32

Permalink

I am doing project "WIRELESS WEATHER MONITORING SYSTEM"using arduino uno, GSM sim900a, LCD 16x2, temperature and humidity sensor dht11…now I request yourgood self to please provide me a program that will display current temperature and humidity on LCD. also if we send SMS from any mobile number to a mobile number of simcard that is in GSM to send current temperature or humidity it must send back SMS to the mobile number from which it received SMS with current temperature or humidity..

Submitted by ALLY on Mon, 05/23/2016 - 15:03

Permalink

please any one can help me I asked one week before regarding this code not working for me when I send sms to gsm nothing happen even if sms shows received
please please any one can help me is very nice project I need to build it
my email is [] any one can post me working codes

Submitted by ashind on Wed, 06/01/2016 - 14:52

Permalink

i like to share my home automation project.
my project was GSM based home automation system with SMS feedback and gas leakage sms alert system with automatic power shutdown
*when we sent sms to ARDUINO through GSM module. GSM module will sent an feedback sms to owners mobile number
*when smoke is detected by MQ5 the Arduino will sent 3 alert sms to owners mobile number
and will also shunt down the power.
if anybody need this project program pleas contact me

Submitted by Muhammad Hamza on Mon, 07/04/2016 - 11:00

In reply to by ashind

Permalink

Sir i want to do a call based GSM module project on 8051 micro controller to control the door locking system along with other home appliances,
being a new comer i need the complete detail
1) full step what can i do step by step
2) program for 8051
i shall be very thankful to you , looking forward for your kind reply

Bro I am doing the same project plzz help me up with any data ypu have collected so far

Submitted by Mayur on Wed, 08/15/2018 - 21:33

In reply to by ashind

Permalink

Hi @ashind .
Your project is interesting.
Please provide your project details like circuits diagram & project code.

Submitted by Richmond on Tue, 08/02/2016 - 23:47

Permalink

Can someone help me? This is my code.

#include <LiquidCrystal.h>
#include "SIM900.h"
#include "sms.h"
#include <SoftwareSerial.h>
//#include <sms.h>
#include <PString.h>
SMSGSM sms;
boolean started = false;
char buffer[160];
char smsbuffer[160];
char n[20];
//LiquidCrystal lcd(4,2,3,7,8,9);
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
boolean st = false;
int buzzer = 12;

void setup() {

//lcd.begin(16, 2);
Serial.begin(9600);
if (gsm.begin(2400))
{
started = true;
}
if (started)
{
delsms();
}
sms.SendSMS("+6xxxxxxxxxx" , "Gas Sensor and GSM module activated");

}

void loop() {

//lcd.setCursor(0, 0);

//lcd.print("Detektor Gas SMS");
int val = analogRead(A0);
val = map(val, 0, 1023, 0, 100);
//lcd.setCursor(0,1);
//lcd.print("Kadar: ");
//lcd.print(val);
//lcd.print("% ");

//code using sensor detection
if (val > 10) {
tone(buzzer,800,500);
delay(1000);
st = true;
}
else st = false;

if (st != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {

if (st != buttonState) {
buttonState = st;

if (buttonState == HIGH) {
PString str(buffer, sizeof(buffer));
str.begin();
str.print("Gas Detected! Gas leakage at ");
str.print(val);
str.print("%");
//String a=str;
sms.SendSMS("+6xxxxxxxxxx", buffer);
}
}
}

//code using sms lapor.
lastButtonState = st;
int pos = 0;
if (started)
{
pos = sms.IsSMSPresent(SMS_ALL);
if (pos)
{
sms.GetSMS(pos, n, smsbuffer, 100);
delay(2000);
if (!strcmp(smsbuffer, "lapor"))
{
PString str(buffer, sizeof(buffer));
str.begin();
str.print("Rate of gas leakage currently at ");
str.print(val);
str.print("%");
//String a=str;
sms.SendSMS("+6xxxxxxxxxx", buffer);
}
delsms();
}
}
}

//delete sms yang dihantar
void delsms()
{
for (int i = 0; i < 10; i++)
{
int pos = sms.IsSMSPresent(SMS_ALL);
if (pos != 0)
{
if (sms.DeleteSMS(pos) == 1) {} else {}
}
}
}

I'm using arduino uno, sim900 module, mq2 gas sensor and buzzer to create a gas sensor detector based on sms.
I have to 2 option :
1. The mq2 gas sensor detects and send the result via sms to the number set in the code.
2. We can send a specific string to know the surrounding gas percentage and send the result to the specific number set in the code.

But I want to change the second option to be auto reply to any incoming number. What should I do?

Submitted by riya on Thu, 08/04/2016 - 19:31

Permalink

Sir, I want to do the same project by using micro-controller 8051.
As i am new comer i want detail information about it.
program, all steps, if u have any video then it also etc
plz send me on my email.
Thanks, looking forward for your kind reply.

Submitted by ravindra on Sat, 08/06/2016 - 21:55

Permalink

Dear SK,
Very nicely explained project ever I came across. It has created a lot of interest in my son including me. I thank for your sincere and fare sharing of knowledge.
Thnaks a lot.

Submitted by kshitish on Sun, 08/14/2016 - 09:31

Permalink

sir if we are using microprocessor there are two circuit diagrams one is for transmitter and another is for receiver but here is only one so is it like combined or what??

Submitted by LESLEY on Thu, 09/22/2016 - 15:27

Permalink

pliz i need to know how the program works and how i use the gsm

Submitted by drew on Wed, 09/28/2016 - 00:15

Permalink

can you give me the code of this project .. please modify the code please can you take the crystal or lcd ... and make it compatible with GSM sim 800L please

Submitted by Syakir on Fri, 09/30/2016 - 22:31

Permalink

Sir. I have try Interfacing GSM Module with Arduino, its working fine. But when i try your project. The bulb does not light up when i send sms to gsm. It show that the sim card in the gsm receive the message but it keep resetting itself. Please help me

Submitted by Dani on Mon, 10/03/2016 - 05:13

Permalink

please help me
I did all the right connections
I uploaded the source code
But when I send a message
None of the relay switches not
what is the problem?

Submitted by Trinity on Fri, 10/07/2016 - 16:04

Permalink

can someone help me pls. I connected the circuit as shown above. the LCD is ok but it is not responding to the on/off messages. I'm using sim800l. is there any modification in the program? if yes, how do I go about it?

Submitted by Benson on Fri, 10/07/2016 - 19:33

Permalink

Hello sir, please am using SIM900A with two sets of pins (6 pins in group and 3 pins also in group), i do not know how to go about the connections since its a bit different from your own module

Submitted by kiri on Wed, 10/12/2016 - 20:10

Permalink

hye sir , do you create ur own relay circuit ? can i have the schematic . i just kind of confuse whether the relay circuit have connection that related to Arduino Uno.