Electronics > GSM Module >> Arduino and GSM Module Views : 37698
Rate This Article :

Turn ON OFF LEDs using Arduino and GSM Module

Introduction:

This article will help us to understand to turn ON/OFF LEDs using ARDUINO based on messages received by GSM Module. Before starting this, reader should have basic understanding about serial interface and its communication between GSM Module and ARDUINO.

Required Components:

  1. ARDUINO
  2. 8 to 10 Jumper Male and Female Pins
  3. Bread Board
  4. GSM Module and Working SIM to receive messages
  5. LED Light

Interface between Components:

In this project, GSM Module sends messages to ARDUINO which will TURN ON/OFF LED based incoming messages.

Instead of LEDs (5V), we can control AC appliances using relay connecting with ARDUINO (but Extra care should be taken when dealing with 240V, please don't attempt if you are not confident). I will do share a separate article on this.

Message Format:

I have used below format to Turn ON/OFF LEDs.

1.    @Light On#

2.    @Light Off#

Circuit Design: (using LEDs)

Arduino and GSM Module

ARDUINO Source Code:

Given below the code to Turn ON/OFF LEDs. Copy and Paste it in ARDUINO Code explorer and burn the code in ARDUINO by selecting required COM Port. Then Send Message from Sender Mobile as mentioned above.

String inputString = "";         // a string to hold incoming data

boolean stringComplete = false;  // whether the string is complete


String incomingString ="";

int startIndex = 0;

int endIndex = 0;


int led1 = 4;


void setup() {

  // initialize serial:

  Serial.begin(9600);


   // prepare the digital output pins

   pinMode(led1, OUTPUT);


   // initially all are off

   digitalWrite(led1, LOW);

 

  // reserve 200 bytes for the inputString:

  inputString.reserve(200);


  //--Start: Send SMS --

  Serial.print("AT+CMGF=1\r");    

  delay(1000);


  //Serial.print("AT+CMGD=1,4\r");  // Deletes all SMS saved in SIM memory

  Serial.print("AT+CMGDA=\""); 

  Serial.println("DEL ALL\"");

  

  delay(1000);

  Serial.print("AT+CMGS=\"+91XXXXXXXXXX\"\r");    //Number to which you want to send the sms


  delay(1000);

  Serial.print("Test SMS - It Started Working1..\r");   //The text of the message to be sent

  delay(1000);

  Serial.write(0x1A);

  delay(1000);


  Serial.print("AT+CNMI=2,2,0,0,0\r"); 

  delay(1000);

  //--End: SMS--

}


void loop() {

  // print the string when a newline arrives:

  if (stringComplete && inputString!="") {


    //Serial.print("AT+CMGL=ALL\r");

    inputString.toLowerCase();


    if(inputString=="@light on#")

    {

      digitalWrite(led1, HIGH); 

    }

    else if(inputString=="@light off#")

    {

      digitalWrite(led1, LOW); 

    }

   Serial.print("AT+CMGDA=\""); 

   Serial.println("DEL ALL\""); // To Delete Messages from SIM Memory

   delay(1000);

  // clear the string:

   inputString = "";

   stringComplete = false;

  }

}


void serialEvent() 

{

   if(stringComplete == false)

   { 

        incomingString = Serial.readString();

        if(incomingString!="")

        {

          startIndex = incomingString.indexOf("@");

          endIndex = incomingString.indexOf("#");


          if(startIndex>0 && endIndex>0)

          {

             inputString = incomingString.substring(startIndex,endIndex+1);

             stringComplete = true;

          }

   }

}

That’s all, Follow the steps to Turn ON/OFF LEDs.

1.    Burn the ARDUINO code into ARDUINO.

2.    Make sure you have connected GSM Module and ARDUINO along with LED.

3.    Make sure the SIM present in GSM Module is working.

4.    Send Message from your mobile.

By replacing LEDs, we can use Relays to connect to turn ON/OFF electrical house appliances (Extra care should be taken when dealing with 240V, please don't attempt if you are not confident). I will do share a separate article on this.

Hope this article gave an idea on how to work with arduino and GSM Module. Thanks for reading this article.
About Author
Raj Kumar
Total Posts 57
Developer in .Net!
Comment this article
Name*
Email Address* (Will not be shown on this website.)
Comments*
Enter Image Text*
   
View All Comments
Hadi
Hello I used This Code, when I send sms to on led it does not work please help me. ------------------------------------------------ String inputString = ""; // a string to hold incoming data boolean stringComplete = false; // whether the string is complete String incomingString =""; int startIndex = 0; int endIndex = 0; int led1 = 4; void setup() { // initialize serial: Serial.begin(9600); // prepare the digital output pins pinMode(led1, OUTPUT); // initially all are off digitalWrite(led1, LOW); // reserve 200 bytes for the inputString: inputString.reserve(200); //--Start: Send SMS -- Serial.print("AT+CMGF=1\r"); delay(1000); //Serial.print("AT+CMGD=1,4\r"); // Deletes all SMS saved in SIM memory Serial.print("AT+CMGDA=\""); Serial.println("DEL ALL\""); delay(1000); Serial.print("AT+CMGS=\"+93747912178\"\r"); //Number to which you want to send the sms delay(1000); Serial.print("Test SMS - It Started Working1..\r"); //The text of the message to be sent delay(1000); Serial.write(0x1A); delay(1000); Serial.print("AT+CNMI=2,2,0,0,0\r"); delay(1000); //--End: SMS-- } void loop() { // print the string when a newline arrives: if (stringComplete && inputString!="") { //Serial.print("AT+CMGL=ALL\r"); inputString.toLowerCase(); if(inputString=="@light on#") { digitalWrite(led1, HIGH); // Serial.println("All is will"); } else if(inputString=="@light off#") { digitalWrite(led1, LOW); } Serial.print("AT+CMGDA=\""); Serial.println("DEL ALL\""); // To Delete Messages from SIM Memory delay(1000); // clear the string: inputString = ""; stringComplete = false; } } void serialEvent() { if(stringComplete == false) { incomingString = Serial.readString(); if(incomingString!="") { startIndex = incomingString.indexOf("@"); endIndex = incomingString.indexOf("#"); if(startIndex>0 && endIndex>0) { inputString = incomingString.substring(startIndex,endIndex+1); stringComplete = true; } } } }
Ayodeji
This code ain't working. The function serialEvent() wasn't later called. I didn't even see the Serial.available() etc Could you send me the original code to my email gracergysolary.ng@gmail.com
Krishna Das
A6 gsm module light on/off code
Rajkumar
Hi Feredy, The Provided example is demonstrate how we can connect and control LEDs through Arduino. For your use case, you need to put digitalWrite(led1, HIGH); in the setup() method (Now it is Low, means Light Off). you need to customize the code based on your needs. Thanks.
feredy
Hi this code have a problem. if sim800l be reset (with each reason for example power or network or handly reset) , arduino can not auto reconnect , just with reset. have you an idea?
aditya
hey is it possible to display that LED is ON/OFF using the same arduino,please send the code
Oops!
Does the serialevent() still functional without calling it in the setup()? And what does this function do? Ty.
Jeyvii
Hi Jeyvii, Yes, this code will work for Arduino atmega328p and GSM 900A Regards, Raj
Rajkumar
Hi sadhana bhagwat, Yes, this code will work for Arduino atmega328p and GSM 900A Regards, Raj
sadhana bhagwat
I have Arduino atmega328p and GSM 900A …. given code is satisfied to that components?
Jeyvii
Hello sir! Can this code will work in arduino mega and gsm sim 900a?
Anjith k
Sir.... Can you compress this code to do only for turn ON one LED. please give the least code only Control a LED... pls give the program without testing messages and Serial monitor...
sivakami
shall i implement this code in proteus.if yes how i will do
Chami epimark
Sir i have bee trying but wont work. Pls help me
balwinder singh
sir this code is not working for the A6 gsm module please send me code at my email . please help me
rahul kumar
hii...will this code provide the feedback message..i mean if i send the message to turn on the led and led is turned on..will i be able to get a message that led has turned on..is it possible to develop such codes?? if yes can you send the same??
Rajkumar
Hi Shaik Anwar, It is mentioned in the "Message Format" section. Please find it (@Light On# , @Light Off#)
shaik anwar
what is the formate of message we have to send inordder to on or ff led
Rajkumar
Hi Bhautik, Ideally arduino will not be able to directly read data which is present in SIM Card but using AT Commands, you will be able to read messages which are present in SIM (through GSM Module) and able to view the Phone numbers as well.
bhautik
is it possible that ardunio read number from sim card and send sms to only saved number into sim card??
Radhakrishnan
plz send me the code for this circuit...
J.MOHAN RAJ
Gsm code send me
jimmy
thank u nice
Rajkumar
I didn't used any library. Are you facing any difficulty in this code?
Diego
good afternoon. What library do you use? I have a sim 800C plate
Rajkumar
Hi, You can use of "Delay(Milliseconds)" following with your LED OFF. i.e. 1. Make your LED ON 2. Delay (30000) in milliseconds 3. TURN LED OFF. Let me know if any challenges. Thanks, Raj
AMN
thanks a lot but if i want make a delay to led for ex : send message from my phone (@on led 30m) and led stay on for 30min . how can i do it and thanks a lot
Rishi
Very nice But actually i wanna make a project to operate a servo motor by sending msg to gsm module . Plz send me the code and connection . I have A6 mini gsm module so give the code and connection according to it.
mc
Copy and paste is not compiled. Sketch is giving an error. Download link. Please.
mc
Copy is not compiled with build. Sketch is giving an error. Download link. Please.
Manish
Work like charm!! Thanks.
kamley
Nice
  Privacy   Terms Of Use   Contact Us
© 2016 Developerin.Net. All rights reserved.
Trademarks and Article Images mentioned in this site may belongs to Microsoft and other respective trademark owners.
Articles, Tutorials and all other content offered here is for educational purpose only and its author copyrights.