GSM Based Home Automation: Arduino Code and Circuit

In this post we are going to build a home automation circuit by which you can control your home gadgets via GSM network or in other words using SMS commands, you can control up to 5 devices independently. The proposed GSM based home automation system is loaded with several features that make this project standout, like: password protected control for all outputs, automatic recovery from power outage, acknowledgement SMS to the user. We will also see how to automate SMS commands so that you can schedule your commands and execute without your intervention.

We will see:

  • Overview and block diagram.
  • Key features of the proposed home automation project.
  • Full Circuit diagram.
  • Source code.
  • How to send SMS commands to the circuit.
  • Prototype image.
  • How to send automated SMS commands to the circuit.

Main Functions of this Home Automation Circuit:

These are the features that out shines other GSM based home automation projects:

1) Pin / password protected ON – OFF action. 

2) Automatic recovery after power outage.

3) SMS acknowledgment.

Pin protected operation:

Cyber criminals are on the rise and they will exploit all the loop holes they discover, so it is essential to protect your gadgets that are connected to any home automation system. Not only cyber criminals but anyone who is close to you like: friends, neighbors, even kids may control your home appliances for a prank, so it is very important to protect with a pin or pass-code.

In this project we need to enter a 3 digit pin in the program code which must be kept confidential. Only by sending the correct pin number with your SMS command you can turn on or off the connected home appliances.   

Automatic recovery after power outage:

Power outages can occur unexpectedly for several reasons and we want all our devices back to operation as soon as power is restored. The proposed home automation circuit can remember which devices were operating before a power outage and it will turn ON those devices when power is restored.

This is accomplished using EEPROM that is present on ATmega328P microcontroller IC. EEPROM is a non-volatile memory which means the data on the microcontroller / Arduino will not get erased when power gets disconnected. In EEPROM, the status of all the 5 relays are stored and when power is restored microcontroller reads the data from EEPROM and turn ON only the devices that were operating before the power outage.        

SMS acknowledgement:

Once you sent an SMS to the circuit you may have a doubt whether your request has been successfully executed or not especially when you are away from your home. The proposed circuit will send you an SMS acknowledgement regarding the status of the relay / device to the phone number that you entered in the source code. By this way you can be sure that your action is executed.

Even if an unauthorized person able to control your home appliances an acknowledgement will be sent to your phone.

Overview and Block Diagram:

The proposed home automation circuit consists of a GSM module SIM 800 (or you can use any other GSM modem), an Arduino board and 5 relays to control 5 electrical appliances independently.

Block Diagram of GSM based Home Automation
Block Diagram of GSM based Home Automation

A valid SIM card needs to be inserted to the GSM modem which will send and receive SMS. The Arduino microcontroller communicates with GSM modem using serial communication protocol and Arduino will decode and check incoming SMS for these three parameters:

1) 3-digit pin number.

2) Device number (1 to 5 devices).

3) Action data (ON / OFF action).      

If these 3 parameters exist in the SMS command and if the pin number matches with the pin that you entered in the source code, Arduino will take action as per your action data (ON /OFF) that you sent with the SMS.

If someone sends SMS with incorrect pin number or in incorrect format, Arduino will simply ignore the SMS and continue what it was doing before. Once the Arduino turned on or off the gadget as per your request, the circuit will respond with an acknowledgement SMS to you, so that you can be sure that your request has been executed.

Circuit Diagram:

GSM Based Home Automation Circuit
GSM Based Home Automation Circuit

NOTE: Disconnect Tx and Rx before uploading code to Arduino.

Circuit Description:

The Arduino can be powered via VIN pin or USB or even using on-board DC jack, however the GSM modem should be powered only via on-board DC jack using a wall adapter. The whole circuit can also be powered from a 9V wall adapter rated at 1 amp as shown in the diagram.

The Tx of the Arduino should connect to Rx of the GSM module and Rx of the Arduino should connect to Tx of the GSM module and ground to ground connection between Arduino and GSM must be made.

There are 5 relays to control five different gadgets independently. The relay voltage can be 9 to 12V, if you are using 12V relays then you should supply 12V to the circuit and this is the maximum.

The 5 relays are controlled by 5 NPN transistors, where it takes 5V signal from Arduino and switches 9 to 12V rated relay ON and OFF.

A diode across all the 5 the relays must be connected as shown below. Diode will arrest high voltage spike generated while energizing and de-energizing the relay coil.

Diode protection
Diode protection

Cathode terminal of the diode should connect to +Ve supply and anode terminal of the diode should connect to collector of the transistor.

Source code:

//--------- Electronics-project-hub --------//
#include<EEPROM.h>
const int output1 = 2;
const int output2 = 3;
const int output3 = 4;
const int output4 = 5;
const int output5 = 6;

int var_1 = 0;
int var_2 = 0;
char input_string[15];

boolean mem_1 = 0;
boolean mem_2 = 0;
boolean mem_3 = 0;
boolean mem_4 = 0;
boolean mem_5 = 0;

//------- Phone number here ------//
char ph[] = "+91xxxxxxxxxxxxx"; // Replace +91 with your country code.
//--------------------------------//

void setup()
{

  Serial.begin(9600);
  pinMode(output1, OUTPUT);
  pinMode(output2, OUTPUT);
  pinMode(output3, OUTPUT);
  pinMode(output4, OUTPUT);
  pinMode(output5, OUTPUT);

  if (EEPROM.read(100) != 50)
  {
    EEPROM.write(100, 50);
    EEPROM.write(0, 0);
    EEPROM.write(1, 0);
    EEPROM.write(2, 0);
    EEPROM.write(3, 0);
    EEPROM.write(4, 0);
  }
  if (EEPROM.read(100) == 50)
  {
    mem_1 = EEPROM.read(0);
    mem_2 = EEPROM.read(1);
    mem_3 = EEPROM.read(2);
    mem_4 = EEPROM.read(3);
    mem_5 = EEPROM.read(4);
  }

  digitalWrite(output1, mem_1);
  digitalWrite(output2, mem_2);
  digitalWrite(output3, mem_3);
  digitalWrite(output4, mem_4);
  digitalWrite(output5, mem_5);

  delay(20000);
  delay(20000);
  delay(20000);
  delay(20000);

  Serial.println("AT+CNMI=2,2,0,0,0");
  delay(1000);
  Serial.println("AT+CMGF=1");
  delay(500);
  Serial.print("AT+CMGS=");
  Serial.print("\"");
  Serial.print(ph);
  Serial.println("\"");
  delay(1000);
  Serial.println("System is ready to receive SMS commands.");
  delay(100);
  Serial.println((char)26);
}
void loop()
{
  if (var_1 == 1)
  {
    Action();
    var_1 = 0;
    var_2 = 0;
    delay(1000);
  }
}
void serialEvent()
{
  while (Serial.available())
  {
    //--------- PIN HERE -------//

    if (Serial.find("/yyy")) // <<< '/yyy' where yyy is 3 digit PIN.

      //--------- PIN HERE -------//
    {
      delay(1000);
      while (Serial.available())
      {
        char input_char = Serial.read();
        input_string[var_2++] = input_char;
        if (input_char == '/')
        {
          var_1 = 1;
          return;
        }
      }
    }
  }
}
void Action()
{
  if (!(strncmp(input_string, "1on", 3)))
  {
    digitalWrite(output1, HIGH);
    delay(200);
    EEPROM.write(0, 1);
    ack_on();
  }

  else if (!(strncmp(input_string, "1off", 4)))
  {
    digitalWrite(output1, LOW);
    delay(200);
    EEPROM.write(0, 0);
    ack_off();
  }

  else if (!(strncmp(input_string, "2on", 3)))
  {
    digitalWrite(output2, HIGH);
    delay(200);
    EEPROM.write(1, 1);
    ack_on();
  }

  else if (!(strncmp(input_string, "2off", 4)))
  {
    digitalWrite(output2, LOW);
    delay(200);
    EEPROM.write(1, 0);
    ack_off();
  }

  else if (!(strncmp(input_string, "3on", 3)))
  {
    digitalWrite(output3, HIGH);
    delay(200);
    EEPROM.write(2, 1);
    ack_on();
  }

  else if (!(strncmp(input_string, "3off", 4)))
  {
    digitalWrite(output3, LOW);
    delay(200);
    EEPROM.write(2, 0);
    ack_off();
  }

  if (!(strncmp(input_string, "4on", 3)))
  {
    digitalWrite(output4, HIGH);
    delay(200);
    EEPROM.write(3, 1);
    ack_on();
  }

  else if (!(strncmp(input_string, "4off", 4)))
  {
    digitalWrite(output4, LOW);
    delay(200);
    EEPROM.write(3, 0);
    ack_off();
  }

  if (!(strncmp(input_string, "5on", 3)))
  {
    digitalWrite(output5, HIGH);
    delay(200);
    EEPROM.write(4, 1);
    ack_on();
  }

  else if (!(strncmp(input_string, "5off", 4)))
  {
    digitalWrite(output5, LOW);
    delay(200);
    EEPROM.write(4, 0);
    ack_off();
  }

  else if (!(strncmp(input_string, "allon", 5)))
  {
    digitalWrite(output1, HIGH);
    digitalWrite(output2, HIGH);
    digitalWrite(output3, HIGH);
    digitalWrite(output4, HIGH);
    digitalWrite(output5, HIGH);
    delay(200);

    EEPROM.write(0, 1);
    EEPROM.write(1, 1);
    EEPROM.write(2, 1);
    EEPROM.write(3, 1);
    EEPROM.write(4, 1);

    ack_on();
  }

  else if (!(strncmp(input_string, "alloff", 6)))
  {
    digitalWrite(output1, LOW);
    digitalWrite(output2, LOW);
    digitalWrite(output3, LOW);
    digitalWrite(output4, LOW);
    digitalWrite(output5, LOW);
    delay(200);

    EEPROM.write(0, 0);
    EEPROM.write(1, 0);
    EEPROM.write(2, 0);
    EEPROM.write(3, 0);
    EEPROM.write(4, 0);

    ack_off();
  }
}
void ack_on()
{
  Serial.println("AT+CMGF=1");
  delay(500);
  Serial.print("AT+CMGS=");
  Serial.print("\"");
  Serial.print(ph);
  Serial.println("\"");
  delay(1000);
  Serial.println("Device(s) turned ON.");
  delay(100);
  Serial.println((char)26);
}

void ack_off()
{
  Serial.println("AT+CMGF=1");
  delay(500);
  Serial.print("AT+CMGS=");
  Serial.print("\"");
  Serial.print(ph);
  Serial.println("\"");
  delay(1000);
  Serial.println("Device(s) turned OFF.");
  delay(100);
  Serial.println((char)26);
}
//--------- Electronics-project-hub --------//

You need to change the following in the code:

1) You need to insert your phone number to receive SMS acknowledgement:

//------- Phone number here ------//
char ph[] = "+91xxxxxxxxxxxxx"; // Replace +91 with your country code.
//--------------------------------------//

2) You need to create a 3 digit pin (000 to 999 – it should be 3 digits) and insert it here:

//--------- PIN HERE -------//
if (Serial.find("/yyy")) // <<< '/yyy' where yyy is 3 digit PIN.
//--------- PIN HERE -------//

How to send SMS commands to the circuit?

When you power ON the circuit with the completed setup, after one minute you will receive a SMS to your phone from the circuit saying that the system is ready to accept your SMS commands. One minute is per-determined duration, your GSM module will take 30 to 50 seconds to latch in to cellular network.

To send SMS commands to turn ON or OFF individual relay, you need to follow this format:

  • Your SMS should start with “/” and end with “/”.
  • In the place of yyy, replace with your 3 digit pin that you entered in the code.
  • After the pin, “1” represents the device number. We have 5 relay and we are going to turn on relay no. 1 (to turn on or off relay number 2 replace 1 with “2”).
  • On or off represents your action to turn on or off your connected device. “on” and “off” must be in small letters and capital letters will get rejected.   

For example, I am going to turn on relay no. 1 and my pin is 350. You should send /3501on/. To turn off the relay no. 1 send /3501off/

Now you know how to send SMS commands to the circuit to control individual relays. There are two more commands which enable to us to control all the 5 relays all at once.

  • /yyyallon/ to turn on all 5 relays.
  • /yyyalloff/ to turn off all 5 relays.

Now let’s see the circuit in action:

You will receive an SMS from the circuit saying that the system is ready to receive your SMS commands.

Now we are sending /350allon/ to the circuit’s phone number. 350 is 3 digit pin number. When this SMS received by the circuit, all the output pins turns high and an acknowledgement SMS will be send to the owner’s phone as shown above.

GSM Based Home Automation Project
GSM Based Home Automation Project

As we can see all the 5 outputs turned ON, in the place of (green) LED you should connect relays. Now let’s turn off all outputs.

We are sending /350alloff/ this command will turn off all the output at once. We will also receive an acknowledgement saying that devices are turned off.

As you can see all the outputs are turned off.

How to automate SMS commands?

The function of a home automation system is not only to control your gadgets in real time, but also to execute scheduled commands automatically without owner’s intervention.

For example, you left your home for some important function or meeting and no one will be available at your home for next few days. Since you fear theft, you decided to turn ON the lights until you reach home.

Instead of running your lights on continuously, you can turn on say at 6:00 PM and turned off at 6:00 AM automatically without your intervention. Not only you are saving energy but periodic on and off of lights may give an impression that some people may still at home.  

Such action can be accomplished by sending SMS commands automatically from your phone.

Ok, how to send SMS commands automatically?

On most of the basic phones and smartphone we have an option to “send later” through which you can schedule your SMS that need to be sent in future. You can schedule a time, date, month even year.

Sending Automated SMS from phone
Sending Automated SMS from phone

By setting up correct SMS commands and time, your phone will send SMS to the circuit without your intervention. Now this simple hack makes our circuit truly an automated system. 

If you have any questions regarding this project, feel free to comment, you will get a guaranteed reply from us.  

Avatar photo

My nick name is blogthor, I am a professional electronics engineer specialized in Embedded System. I am a experienced programmer and electronics hardware developer. I am the founder of this website, I am also a hobbyist, DIYer and a constant learner. I love to solve your technical queries via comment section.