GSM Based AC Appliance Control

Introduction
This project would show you how to control an AC appliance remotely from anywhere using your mobile phone. This kind of project is useful in various applications. Say for example a farmer can switch ON or OFF the motor pump present near his field remotely. In this way he need not travel all the way to the field to control it, especially at night times.
Components Required
1. ATMega16 Development board
2. LCD board
3. SIM 300/900 GSM Module
4. 12V/2A Power Supply
5. SIM card with balance (or at least message offer maybe)
6. SPDT Relay Switch
7. Transistor BC547
8. Diode 1N4007
9. 10k Resistor 
10. A PC or a Laptop (Optional. Needed in case of debugging)

Initial Setup
See my previous tutorial GSM BASED INTRUDER ALERTER if you are using the GSM modem for the first time.
About Relays
The switches at our home need to be operated manually by hand or “mechanically triggered” in order to work. If we need to control the switch using a microcontroller which does nothing but produces 5V or 0V at its output pins then we would require extra mechanical arrangements like a servo with an arm, to press the switch. Instead of building such complicated arrangements we can go with a relay. These can directly be controlled by electrical means.
Working of Relay


The above shown diagram is of a SPDT (Single Pole Double Throw) Relay. It consists of 5 terminals. Two of them are from the coil where the input to drive the relay is supposed to be given. The other three are NO (Normally Open), NC (Normally Closed) and COM (Common).
When no input is given to the coil, there is no magnetic field produced. So the “NC” and “COM” terminals are connected and the “NO” terminal is left free.


When the input is given, the current flowing through the coil produces magnetic field (Basic physics) and thus attracting the lever, which breaks the connection between “COM” and “NC” and makes a connection between “COM”  “NO” terminals.
Again when the input is removed, the spring attached to the lever pulls it back and “COM” and “NC” connection is re-established.
Free-wheeling diode
There is a problem in connecting a relay directly to an electronic circuit. When the input power is suddenly removed to switch OFF the relay, a voltage spike occurs due to the presence of the inductive load (The coil inside the relay is an inductor right?). This voltage spike can permanently damage the electronic circuit to which the relay is connected to. And thus we require a diode.
A diode connected in reverse bias with respect to the input supply, will prevent the voltage spike (produced by the relay when the supply is suddenly switched OFF) from reaching the other part of the control circuit.


Next, we need something to supply enough current to the relay input so that it doesn’t trip unexpectedly. So we use a transistor in between microcontroller and relay. And the circuit becomes like this:



Advantages of Relay
1. No degradation of the switched signal.
2. Offers isolation between the AC supply and the Circuit.
3. Lower Impedance and easier to interface.
4. Robust.
Code Explanation
The code is build mostly around the library files “GSM.h” (Thanks to Engineers Garage tutorials) and “LCD.h” which contain the necessary functions for GSM modem interfacing and LCD interfacing.
GSM.h contains the following functions:

gsm_init() : Does the initial testing like checking for response after sending “AT\r”, checks for the presence of SIM card, Network and then sets the modem into Text Mode.

gsm_read() : Tries to read the message stored at location ‘1’ of the SIM. If there is a message then it stores the first 9 characters of it in the array “msg[]”. Then checks if the string in “msg[]” matches with the keyword or not.

gsm_delete() : Deletes the message stored at location ‘1’ of the SIM.

gsm_waitfor(char c) : Waits for a particular characters specified in the variable ‘c’ . If the wait is longer than the timer set in the Watch Dog timer then the whole program resets. 

Algorithm
1. Set PB2 as output and initialize the LCD function
2. Initialize the GSM modem and set the Message format to text mode.
3. Delete the message at location ‘1’ on the SIM.
4. Enter an infinite loop and check for an incoming message constantly.
5. If there is an incoming message, then store the number and the 1st nine characters of the message in the arrays “number[]” and “msg[]”.
6. Display the phone number and the message on the LCD screen.
7. Compare the saved message with the keyword “Motor on ”and if they match then set PB2 to high state.
8. Compare the saved message with the keyword “Motor off” and if they match then set PB2 to low state.
9. Delete the read message and go to the next iteration.
Trouble shooting
1. LCD doesn’t show anything/ Shows blocks on the first line
Adjust the potentiometer connected to the contrast pin/Make sure all the connections are right.
2. Motor doesn’t get turned ON
Check whether you are sending the correct message or not. Remember you need to add a space after “Motor on” and no space after “Motor off”. Check the LCD after sending the message to see if the controller is grasping your text correctly.



This article was published by me on EngineersGarage

Please Like us to Receive updates on FB