Advertisement

SIM800L ONE-CHANNEL SMS RELAY

It is very easy to build an “SMS-controlled relay” with the help of a cheap GSM modem module and a small microcontroller board. Here, I’m using the SIM800L and Arduino Nano V3 to develop a nifty one-channel SMS relay. The device is, in fact, an “isolated electric switch” that can be controlled through the messaging app on your mobile phone!

Let’s take a look at the circuit diagram, which exposes the electrical connections:

 

 

As you can see from the schematic above, my circuit needs a stabilized and clean 5-V/1-A (min) DC power supply for proper performance. The microcontroller board (Nano_V3) runs on the 5-Vdc supply, and the modem (SIM800L) gets around 4.3 Vdc. Though most SIM800L modules work on 3.7 V, my SIM800L module from a new eBay seller seems to easily lose the network connection under 4 V. That’s why it’s set to 4.3 Vdc.

 

The code (Arduino sketch) is a basic one without any dedicated SIM800L libraries. In the code, the RX and TX pins are assigned as software serial pins. This is done deliberately so that the Arduino’s hardware serial port remains to be used with the serial monitor. Once the code is uploaded, you can move to the next step. At first, take some time to ensure that the hardware setup is in order. Don’t forget to insert a valid micro-SIM card in the slot before powering up the system. Usually, the SIM card registers automatically to the home network within a few seconds. Note that the onboard LED indicator of the SIM800L module will blink once every two to three seconds (not continuously) when it has completely registered to the network. Thereafter, you should push the reset switch once to make the system fully functional.

 

 

Let’s start with sending SMS: Sending the text “ON” (not case-sensitive) turns the relay on, and “OFF” turns the relay off. The relay contacts can be wired as desired to switch any suitable electrical load. That’s all!

/*
 * SIM800L SMS RELAY v1.0
 * Arduino Hardware (Author): Nano V3 (ATmega328)
 * Arduino IDE (Author): 1.6.9
 * T.K.Hareendran/2018
 */

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11);  // (Rx,Tx  > Tx,Rx) 

char incomingByte; 
String inputString;
int relay = 13; // Output for Relay Control

void setup() 
{
      pinMode(relay, OUTPUT);
      digitalWrite(relay, LOW); // Initial state of the relay
      Serial.begin(9600);
      mySerial.begin(9600); 

     while(!mySerial.available()){
        mySerial.println("AT");
        delay(1000); 
        Serial.println("Connecting...");
        }
      Serial.println("Connected!");  
      mySerial.println("AT+CMGF=1");  //Set SMS to Text Mode 
      delay(1000);  
      mySerial.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived messages(command name in text: new message indications to TE) 
      delay(1000);
      mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read Unread Messages
     }

void loop()
{  
  if(mySerial.available()){
      delay(100);

      // Serial Buffer
      while(mySerial.available()){
        incomingByte = mySerial.read();
        inputString += incomingByte; 
        }

        delay(10);      

        Serial.println(inputString);
        inputString.toUpperCase(); // Uppercase the Received Message

        //turn RELAY ON or OFF
        if (inputString.indexOf("ON") > -1){
          digitalWrite(relay, HIGH);
          }
         if (inputString.indexOf("OFF") > -1){
          digitalWrite(relay, LOW);
          }          

        delay(50);

        //Delete Messages &amp; Save Memory
        if (inputString.indexOf("OK") == -1){
        mySerial.println("AT+CMGDA=\"DEL ALL\"");

        delay(1000);}

        inputString = "";
  }
}

 

Here is a screenshot of my serial monitor:

 

 

Some important notes

  • The SIM card must be inserted into the slot with its contacts facing the circuit board and the notched end sticking out.
  • SIM800L itself requires an input voltage between 3.4 V and 4.4 V (1–2 A), and the module (the breakout board) used here comes without an integrated voltage regulator (and logic-level shifter). This calls for a neat power supply unit (and a logic-level shifter circuit). If the power source can’t keep up, the module will shut down/reset in the middle of the action.
  • In case of a malfunction, try to power-cycle the system. Make sure the network registration is successful again, and only after that, do a hardware reset using the reset switch.
  • The given code obviously needs some refinements. I just dug this code up and tried it out, and it does fit for me. If you spot mistakes, or think I’m casually skipping over crucial bits, please let me know.

 

 

If you want to learn more about the SIM800L module, check out these other posts

How to Play with SIM800L – Part 1

How to Play with SIM800L – Part 2

No Comments

Join the conversation!

Error! Please fill all fields.
Looking for the latest from TI?