GSM Based Home Automation project using Arduino

In this Arduino project, we will create a home automation system that will control home appliances using GSM module and Arduino. We will use the GSM Module SIM900A to control devices from large distances over an SMS as compared to IR based home automation. The devices will switch on and off when the GSM module receives specified messages.

GSM based home automation system using arduino

GSM Controlled Home Automation System Overview

We will require the following components for this project.

Required Components:

  1. Arduino Uno: We will use Arduino due to its easy use. It also provides several digital pins to interface with the LCD, GSM module and relay module at the same time.
  2. 4 Channel Relay Modules: The module we will use in this project is HL-54S. It switches on and off using a 5V logic signal from Arduino. It can handle up to 250VAC and 10A. These modules have 4 relays so we can control 4 AC devices or appliances.
  3. GSM SIM900A: SIM900A GSM Module is the smallest and cheapest module for GPRS/GSM communication. The module offers GPRS/GSM technology for communication with the uses of a mobile sim. It uses a 900 and 1800MHz frequency band and allows users to receive/send mobile calls and SMS. Note use a power supply which can supply up to 1 amp.
  4. 16×2 LCD: LCD will be used to display the states of the bulbs in our project. We will use 16×2 LCD because it is easy to interface with Arduino and very cheap in price. 10k potentiometer is used to control the contrast of the display.
  5. AC bulbs with holders: AC bulbs are used to represent devices and appliances. Because it is easy to handle and very useful when you are prototyping any AC project. In the final product, you can just replace it with an AC socket to control.
  6. AC wire with plug: Use good quality wire when working with higher voltages. It is always good to use electrical tape to cover connections.
  7. External 5 volt supply: 5-volt dc supply is required to switch the relay ON and OFF. Otherwise, it does not work. Also do not supply 5v from Arduino.

How does GSM Controlled Home Automation Work?

In this GSM controlled home automation system, Arduino Uno is used as the brain of our circuit.

You can view the operations constituting the GSM control home automation system in the block diagram below:

block diagram of GSM Based Home Automation using Arduino
Block Diagram

Connection Diagram of GSM Controlled Home Automation Project

In this section, we will explain the connections of the various components specified above with Arduino Uno to form the home automation system.

GSM Based Home Automation using Arduino circuit diagram

Let us discuss them one by one.

Arduino with SIM900A GSM Module

The communication between Arduino and GSM module is serial. So we are supposed to use serial pins of Arduino (Rx and Tx). Connect the Tx pin of GSM module to Rx pin of Arduino and Rx pin of GSM module to Tx pin of Arduino. Connect VCC pin to external 5v supply which can deliver up to 1 amp and GND pin with GND.

Note: While programming, Arduino uses serial ports to load program from the Arduino IDE. If these pins are used in wiring, the program will not be loaded successfully to Arduino. So you have to disconnect wiring in Rx and Tx each time you burn the program to Arduino. Once the program is loaded successfully, you can reconnect these pins and have the system working!

SIM900A GSM Module

Arduino with 16×2 LCD

We will use a 16×2 LCD in our project to display the states of the appliances. To connect a 16×2 LCD with Arduino we will require an additional 10k potentiometer as well.

We have a dedicated tutorial regarding interfacing 16×2 LCD display with Arduino with some example sketches. Have a look at it before proceeding further for a better understanding of the LCD.

Recommended Reading: 16×2 LCD Interfacing with Arduino – Explained with Example Codes

There are two types of pins on the whole 16×2 LCD module. Some pins are used to send to 16×2 LCD and some are command pins. In other words, every pin has a role in controlling a single pixel on the display.16 x 2 LCD has sixteen columns and two rows. That means, it can display sixteen characters per row and it has two such rows.

Pinout

The diagram shows the pin configuration of a 16×2 LCD display. It has sixteen pins.

16X2 LCD Pinout diagram Pic Microcontroller tutorial
  • D0 – D7: Pin number 7-14 are data bus lines that are used to send data from Arduino which you want to display on LCD. With these 8 data lines, data can be transferred either in an 8-bit format or in a 4-bit format. In a 4-bit format, only upper four bits (D4-D7) are used to send data from Arduino to LCD. The full byte is transmitted in two successive transmissions. A 4-bit format is used to save GPIO pins of Arduino. Because fewer GPIO pins of Arduino will be required to transfer data.
  • Contrast Select (VEE): It will help to control the contrast of PIXELS according to the 16X2 LCD light.
  • RS: This pin is known as a register select pin. It helps to toggle the command/data register.
  • R/W: The signal on this pin will decide whether it is going to read from LCD or write on it.
  • EN: Enable pin will help to transfer the instruction from the data pins and another command pin to the LCD. It act as permission to internal registers.
  • VSS: It’s a ground pin for common grounds.
  • VCC: The power pin will use for voltage input to the 16X2 LCD.

Arduino connections with 16×2 LCD

We are using the following connections as described below. Refer to the schematic diagram to have a clearer idea of the connections.

16×2 LCDArduino
D4 – D7Pin 10,11,12,13
EnablePin 9
RSPin 8
RWGND
VEE10k POT (Middle Leg)
VSSGND
VCC+5V
LED++5V
LED-GND

We have an Arduino library for easy communication between LCDs called the LiquidCrystal library. It is an inbuilt library by Arduino Adafruit version.

Arduino with Channel Relay modules

The relay module for Arduino is one of the most powerful applications for Arduino as it can be used to control both A.C and D.C devices by simply controlling the relay by giving 5V. A relay is basically a switch which is operated electrically by an electromagnet. A relay can be used to control high voltage electronic devices such as motors as well as low voltage electronic devices such as a light bulb or a fan.

We will be using the 4 relay Arduino module in our home automation project.

relay module for arduino
  • External 5 volt to JD VCC.
  • Ground to ground.
  • IN1 to Pin 3.
  • IN2 to Pin 4.
  • IN3 to Pin5.
  • VCC to Arduino 5V.
  • Connect one end of all bulbs to the normally open terminal of relays.
  • One end of 220VAC to all common terminals of relay and other ends with other terminals of the light lamps.

Working  GSM Controlled Home Automation using Arduino

We will send a specified command to the GSM module through messages using any mobile. GSM module will receive the messages which the Arduino will fetch the desired instruction from the message and perform the required operation such as turn the devices ON/OFF and display their status on the LCD.

For demonstration purposes, we will use different colored bulbs: white, blue and green to depict a fan, light and tv.

The table below shows the commands that we will send by our mobile and its corresponding functions that will occur:

CommandsFunctions
#A.fan on*Fan ON
#A.fan off*Fan OFF
#A.light on*Light ON
#A.light off*Light OFF
#A.tv on*TV ON
#A.tv off*TV Off
#A.all on*All ON
#A.all off*All OFF

GSM Based Home Automation System Arduino Code

We will use Arduino IDE to program our Arduino board.

Arduino Sketch

Open your Arduino IDE and go to File > New to open a new file. Copy the code given below in that file and save it.

#include<LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

#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);
  digitalWrite(Fan, HIGH);
  digitalWrite(Light, HIGH);
  digitalWrite(TV, HIGH);
  lcd.setCursor(0, 0);
  lcd.print("GSM Control Home");
  lcd.setCursor(0, 1);
  lcd.print(" Automaton ");
  delay(2000);
  lcd.clear();
  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, LOW);
    lcd.setCursor(13, 1);
    lcd.print("ON ");
    delay(200);
  }

  else if (!(strncmp(str, "tv off", 6)))
  {
    digitalWrite(TV, HIGH);
    lcd.setCursor(13, 1);
    lcd.print("OFF ");
    delay(200);
  }

  else if (!(strncmp(str, "fan on", 5)))
  {
    digitalWrite(Fan, LOW);
    lcd.setCursor(0, 1);
    lcd.print("ON ");
    delay(200);
  }

  else if (!(strncmp(str, "fan off", 7)))
  {
    digitalWrite(Fan, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("OFF ");
    delay(200);
  }

  else if (!(strncmp(str, "light on", 8)))
  {
    digitalWrite(Light, LOW);
    lcd.setCursor(7, 1);
    lcd.print("ON ");
    delay(200);
  }

  else if (!(strncmp(str, "light off", 9)))
  {
    digitalWrite(Light, HIGH);
    lcd.setCursor(7, 1);
    lcd.print("OFF ");
    delay(200);
  }

  else if (!(strncmp(str, "all on", 6)))
  {
    digitalWrite(Light, LOW);
    digitalWrite(Fan, LOW);
    digitalWrite(TV, LOW);
    lcd.setCursor(0, 1);
    lcd.print("ON ON ON ");
    delay(200);
  }

  else if (!(strncmp(str, "all off", 7)))
  {
    digitalWrite(Light, HIGH);
    digitalWrite(Fan, HIGH);
    digitalWrite(TV, HIGH);

    lcd.setCursor(0, 1);
    lcd.print("OFF OFF OFF ");
    delay(200);
  }
}

How does the Code Work?

We will start by including the library required for our 16×2 LCD as shown below:

#include<LiquidCrystal.h>

Next, we declare the Arduino pins that are connected with the LCD. To define connections, we use the following line of code. This line creates a LiquidCrystal object and lcd is a name of the object that we are going to use to call LCD functions. You can also use any other name.

LiquidCrystal lcd(rs, en, d4, d5, d6, d7)

In our case, the pins are 8, 9, 10, 11, 12, and 13 respectively.

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

Specify the Arduino pins connected with IN1, IN2 and IN3 of 4 Channel Relay Module. These pins will control fan, light and tv. For demonstration purposes we are using different colored bulbs.

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

Moreover, we will define some other variables as well including a temporary variable called ‘temp’ with the value 0, an array of 15 characters called str that we will use later on in the code to monitor the appliances appropriately.

int temp = 0, i = 0;

char str[15];

Additionally, we will blink a 5mm LED connected with the Arduino digital pin 13, whenever data is being received from the mobile. In the following line, we are defining the pin number of the led variable.

int led = 13;

setup()

We use lcd.begin() routine to define the size of an LCD. The first argument to this function is a number of rows and the second argument is a number of columns. For instance, this line declares the size as 16 columns and 2 rows. That means 16×2 size.

lcd.begin(16, 2);

Open the serial communication at a baud rate of 9600.

Serial.begin(9600);

Configure led, Fan, Light and TV as output pins by using pinMode() function. Specify the pin as the first parameter and the mode as the second parameter.

  pinMode(led, OUTPUT);
  pinMode(Fan, OUTPUT);
  pinMode(Light, OUTPUT);
  pinMode(TV, OUTPUT);

Initially set the value to HIGH by using digitalWrite() function. Specify the pin as the first parameter and the value as the second parameter.

  digitalWrite(Fan, HIGH);
  digitalWrite(Light, HIGH);
  digitalWrite(TV, HIGH);

Print a series of messages on the LCD display with delays in between. Initially all the three bulbs are off hence it will be displayed on the LCD that Fan Light TV are all OFF.

  lcd.setCursor(0, 0);
  lcd.print("GSM Control Home");
  lcd.setCursor(0, 1);
  lcd.print(" Automaton ");
  delay(2000);
  lcd.clear();
  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 ");

Moreover, we use AT commands through Arduino COM port to operate the GSM module. The first command is for new SMS message indications and the second command is for selecting the text mode.

For more information about the AT commands set for this GSM module refer to the following link: (https://microcontrollerslab.com/wp-content/uploads/2020/01/AT-Command-Set-Datasheet.pdf)

  Serial.println("AT+CNMI=2,2,0,0,0");
  delay(500);
  Serial.println("AT+CMGF=1");
  delay(1000);

loop()

Inside the infinite loop() we first check if the temporary variable we created is ‘1’ or not. If it is then call the check() function that will be responsible for controlling the appliances by comparing the received data and the command in the application. Moreover, the temporary variable is again set to 0.

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

serialEvent()

This function is automatically called at the end of loop() when there is serial data available in the buffer. In our case, if the data available in the buffer consists of ‘#A’ which indicates the start of a command, then the LED will blink and the characters are added to the array str. If the available character is ‘*’ which indicates the end of the command, then the temporary variable will be set 1 and we return.

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;
        }
      }
    }
  }
}

check()

The check() function is responsible for controlling the appliances by comparing the strings received in the buffer and a predefined string. In the first case, if ‘tv on’ command was sent, then turn the TV on. Otherwise, if ‘tv off’ command was sent, then turn the TV off. Likewise, if ‘fan on’ command was sent then turn the Fan ON. Otherwise, if ‘fan off’ command was sent, then turn the Fan OFF. Likewise, if ‘light on’ command was sent then turn the Light ON. Otherwise, if the ‘light off’ command was sent, then turn the Light OFF.

The LCD will display all the states of the three appliances as they change at each step.

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

  else if (!(strncmp(str, "tv off", 6)))
  {
    digitalWrite(TV, HIGH);
    lcd.setCursor(13, 1);
    lcd.print("OFF ");
    delay(200);
  }

  else if (!(strncmp(str, "fan on", 5)))
  {
    digitalWrite(Fan, LOW);
    lcd.setCursor(0, 1);
    lcd.print("ON ");
    delay(200);
  }

  else if (!(strncmp(str, "fan off", 7)))
  {
    digitalWrite(Fan, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("OFF ");
    delay(200);
  }

  else if (!(strncmp(str, "light on", 8)))
  {
    digitalWrite(Light, LOW);
    lcd.setCursor(7, 1);
    lcd.print("ON ");
    delay(200);
  }

  else if (!(strncmp(str, "light off", 9)))
  {
    digitalWrite(Light, HIGH);
    lcd.setCursor(7, 1);
    lcd.print("OFF ");
    delay(200);
  }

  else if (!(strncmp(str, "all on", 6)))
  {
    digitalWrite(Light, LOW);
    digitalWrite(Fan, LOW);
    digitalWrite(TV, LOW);
    lcd.setCursor(0, 1);
    lcd.print("ON ON ON ");
    delay(200);
  }

  else if (!(strncmp(str, "all off", 7)))
  {
    digitalWrite(Light, HIGH);
    digitalWrite(Fan, HIGH);
    digitalWrite(TV, HIGH);

    lcd.setCursor(0, 1);
    lcd.print("OFF OFF OFF ");
    delay(200);
  }
}

Demonstration

Make sure you choose the correct board and COM port before uploading your code to the board. Go to Tools > Board and select Arduino UNO. Next, go to Tools > Port and select the appropriate port through which your board is connected.

select Arduino uno

Click on the upload button to upload the code to the board.
After you have uploaded your code to the development board, pick your mobile and send an SMS with the appropriate command and the bulbs will light up in response. The LCD will also display the current state of the three bulbs.

You can watch this video for demo:

You may also like to read:

13 thoughts on “GSM Based Home Automation project using Arduino”

  1. How u can send message via Hardware serial not the software ?
    it’s possible to receive and send in the same time ?
    if i connect the lCD with I2C and rotary encoder is it fine or the connection will be lost ?

    Reply

Leave a Comment