In this post we ‘ll control a relay through GSM by sending message.
Parts required :
GSM SIM900 or SIM800
Relay board (12v board with active HIGH , i.e if you send HIGH from Arduino then Rlay will be ON)
Arduino UNO
DC connector cable & 12v adapter
We’ll make it simple By sending SMS $a1 Relay is made ON
BY sending SMS $a0 the Relay is switched OFF
GSM SIM900 board is used in this demo , but you can use SIM800 also. A valid SIM to be inserted in to the slot on GSM board. Do not use 4G SIM like JIO , as the SIM 800 / 900 is 2G /3G enabled.
A 1 channel relay board is used which is a 12v relay & needs a HIGH to enable the relay. Note that if you use a relay board with opto coupler , it needs a LOW to switch ON the relay .In this demo we use a 12v , HIGH enable relay which uses a simple NPN transistor to switch ON the relay.
i.e from Arduino Digital pin you send a HIGH to switch ON the relay.
Connection
Digital pin 8 of Arduino is connected to Relay board INPUT
Rx of Arduino to Tx of GSM , Tx of Arduino to Rx of GSM
GND of Arduino to GND of GSM
12V Adapter is used as power source. Power socket is provided on GSM & Arduino boards . DC connectors are used to power up GSM & Arduino .As Arduino has 5v regulator on board you can safely use 12v at power socket.
The +ve pin of Relay board is looped with +12v of adapter & -ve to GND of Relay board.
Ensure that all GND pins are made common , that of GSM , Arduino & Relay boards.
Following is the Arduino code to be uploaded. Remember to disconnect wires at Rx/Tx while uploading code to Arduino.
You can Download the Code HERE
——————————
char inchar; // variable to store the incoming character
int Relay = 8;
void setup()
{
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
Serial.begin(9600);
delay(2000);
Serial.println(“AT+CMGF=1”); // set mode to text
delay(100);
Serial.println(“AT+CNMI=2,2,0,0,0”);
// just to get a notification when SMS arrives &direct out SMS upon receipt to the GSM serial out
delay(100);
}
void loop()
{
//If a character comes in from the GSM…
if(Serial.available() >0)
{
inchar=Serial.read();
if (inchar==’$’)
{
delay(10);
inchar=Serial.read();
if (inchar==’a’)
{
delay(10);
inchar=Serial.read();
if (inchar==’0′)
{
digitalWrite(Relay, LOW);
Serial.println(“Relay OFF”);
}
else if (inchar==’1′)
{
Serial.println(“Relay ON”);
digitalWrite(Relay, HIGH);
}
delay(100);
Serial.println(“AT+CMGD=1,4”); // delete all SMS
delay(2000);
}
}
}
}
———————————–
A char variable inchar is used to store the incoming SMS
The relay pin is defined as DIGITAL pin 8
Inside setup function , the Relay pin is declared as OUTPUT using pinMode & then initially it is made LOW
Serial.begin(9600) ; starts Serial communication at baud 9600.
AT+CMGF=1 confirms TEXT mode operation of GSM .Notice the use of SerialPrintln which provides a Carriage Return
Serial.println(“AT+CNMI=2,2,0,0,0”);
// just to get a notification when SMS arrives &direct out SMS upon receipt to the GSM serial out
The AT command +CNMI is used to specify how newly arrived SMS messages should be handled. You can tell the GSM/GPRS modem or mobile phone either to forward newly arrived SMS messages directly to the PC, or to save them in message storage and then notify the PC about their locations in message storage.
Syntax: AT+CNMI=<mode>,<mt>,<bm>,<ds>,<bfr>
Description:
This command selects the procedure for message reception from the network.
Values:
<mode>: controls the processing of unsolicited result codes
Note: Only <mode>=2 is supported.
<mt>: sets the result code indication routing for SMS-DELIVERs. Default is 0.
0: No SMS-DELIVER indications are routed.
1: SMS-DELIVERs are routed using unsolicited code: +CMTI: “SM”,<index>
2: SMS-DELIVERs (except class 2 messages) are routed using unsolicited code: +CMT:…….
AT+CNMI is used anyway just to get a notification when a SMS arrives, in order to read the content of the stored SMS you have to use AT+CMGL= or AT+CMGR=…
Inside Loop
If any serial data is available it is assigned to inchar variable
If the first character is $ , the inchar variable is replaced with next Serial character
If the next character is a , again the inchar is replaced with next serial character
Finally , if the character is 1 , the pin 8 is made HIGH & relay is made ON
If the final character is 0 , pin 8 is made LOW , thus making the relay OFF.
SMS $a1 makes the Relay ON
SMS $a0 makes the Relay OFF
Only basic code is explained in this post to just ON or OFF the relay. Further improvements can be done like :
1. Relay status sent back as SMS to the caller
2. Programming can be done so that SMS from particular Master number only is considered
& SMS from other number will not control the relay
3. Pump starter can be made ON / OFF by using 2 relays .One relay contact used to START the motor & other to STOP .While connected with STARTER we need to momentarily switch ON / OFF the relays. This is similar to manually pressing the START or STOP button of the motor starter.
Support video :