Arduino Projects

Interface SIM800L with Arduino Tutorial – How It Works, Codes

Introduction

This document shows how to interface the SIM800L with Arduino. The SIM800L module is a low-cost GSM/GPRS module that supports Quad-band GSM/GPRS network. It can be easily interfaced with Arduino Mega using the following steps.

List Of Materials:

  1. Arduino UNO
  2. SIM800L GPRS GSM Module
  3. DC-DC Adjustable Step Down Converter Power Module
  4. Female – Male Jumper Wire
  5. Single Channel 5V Relay Module

What is SIM800L?

SIM800L with Arduino

What is the SIM800L? The SIM800L is a quad-band GPRS module that is a highly integrated module developed with a TCP/IP protocol stack. SIM800L is a small cellular module allowing GPRS communication, sending and receiving SMS, and making and receiving voice calls.

it works on frequencies EGSM 900MHz, DCS 1800MHz, and PCS 1900MHz It supports a GPRS connection with download speeds of up to 85.6kbps. The module has an inbuilt TCP/IP stack that allows it to connect to the internet. The SIM800L also has an inbuilt RTC and can be used to send and receive text messages (SMS). making this module an excellent solution for any project that needs long-range connectivity.

Note: Be ready to take massive power consumption with peek up to 2A. The maximum voltage on UART in this module is 2.8V. Higher voltage will destroy the module.

SIM800L Features and Specifications

Specification:

  • Supply voltage: 3.8V – 4.2V
  • Recommended supply voltage: 4V
  • Power consumption:
    • sleep mode < 2.0mA
    • idle mode < 7.0mA
    • GSM transmission (avg): 350 mA
    • GSM transmission (peek): 2000mA
  • Interface: UART (max. 2.8V) and AT commands
  • SIM card socket: microSIM (bottom side)
  • Supported frequencies: Quad Band (850 / 950 / 1800 /1900 MHz)
  • Antenna connector: IPX
  • Status signalling: LED
  • Working temperature range: -40 do + 85 ° C
  • Full modem serial port
  • Two microphone inputs and speaker output
  • SIM card interface
  • Supports FM and PWM
  • Controlled by AT Command (3GPP TS 27.007, 27.005 and SIMCOM enhanced AT Commands)

SIM800L Module pinout

The SIM800L module has a total of 12 pins that interface it to the Microcontroller. The connections are as follows:

Ad
  • NET – Helical Antenna provided along with the module.
  • VCC – supply voltage This can be anywhere from 3.4V to 4.4 volts. Remember connecting it to a 5V pin will likely destroy your module!
  • RESET –  is a hard reset pin
  • RXD(Reciever)- serial communication
  • TXD (Transmitter)- serial communication
  • GND – Ground Pin and needs to be connected to GND pin on the Arduino.
  • RING  – LOW state while receiving the call, It is by default high and will pulse low for 120ms when a call is received.
  • DTR – pin activates/deactivates sleep mode. Default in HIGH state (module in sleep mode, serial communication disabled). After setting it to LOW the module will wake up.
  • MICP, MICN – microphone (P + / N -) The two microphone pins can be connected directly to these pins.
  • SPKP, SPKN – speaker (P + / N -) The two pins of a speaker can be tied directly to these two pins.

Power Supply for SIM800L

The SIM800L  module can also operate on an external power supply that provides a minimum of 3.6V and a maximum of 4.8V. The SIM800L has an onboard regulator that can step down a higher voltage to the 3.4-4.2V range So, using 5V could damage the SIM800 module and 3.3V is not enough to reliably power it.

The first is to use the Arduino’s 3.3V output pin. This output pin is regulated and can provide up to 500mA of current. However, the SIM800L can consume up to 2A of current, so this is not an ideal solution.

The second way is to use an external power supply that is connected to the Arduino’s 5V output pin. This pin can provide up to 1A of current, Therefore, an external 3.7V Li-ion Polymer battery is used as the power for the module.

SIM800L Applications

The SIM800L is a mini GSM/GPRS module, which can be used in a wide range of applications that require connectivity to a cellular network. Some of these applications include

  1. Home automation
  2. Emergency systems
  3. Communication
  4. Remote monitoring and control
  5. Alarm and security systems
  6. Wireless data logging
  7. SMS and MMS
  8. Voice call

How To Use SIM800L with Arduino

The SIM800L is a GSM module with a serial interface. It can send and receive text messages and receive calls. The SIM800L can be attached to a microcontroller using the serial UART interface, and in the example shown down, connected to an Arduino.

SIM800L with Arduino Connections

Interface SIM800L with Arduino Tutorial

S.N. ARDUINO SIM800L
1. +5 V VCC
2. GND GND
3. 3 TXD
4. 2 RXD

Note:  don’t ignore connecting the ground first and then add connections with your Arduino to avoid module harm or make all connections first then power up the circuit.

Now, insert the SIM card into the back of the SIM800 module. Then, attach the SIM800 TX and RX to the Arduino D8 and D7 respectively. These are the serial UART connections that allow communication between the two modules.

Connecting the SIM800L to the Network

If the power to the SIM800L is enough, the onboard LED starts blinking. If there’s not enough power, the LED blinks for almost three seconds and then turns off.

The frequency of the blinking indicates something: Here’s a video displaying these LED indications:

Netlight LED Blinking Finding Network Connection

  • Every second:  searching for a network.

Netlight LED Blinking Active GPRS Connection

  • Every three seconds: connected to a network.

Netlight LED Blinking Network Connection Established

  • Twice per second: connected through GPRS.

Programming

Here is the full sketch for the AT command test:

#include <SoftwareSerial.h>

SoftwareSerial sim800l(2,3);

void setup() {
  Serial.begin(9600);
  sim800l.begin(9600);
}

void loop() {
  while (Serial.available()) {
      delay(1);
      sim800l.write(Serial.read());
  }
  while (sim800l.available()) {
       Serial.write(sim800l.read());
  }
}

Test Code

#include <SoftwareSerial.h>

String Arsp, Grsp;
SoftwareSerial gsm(2, 3); // RX, TX

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  Serial.println("Testing GSM SIM800L");
  gsm.begin(4800);

}

void loop() {
  // put your main code here, to run repeatedly:

  if(gsm.available())
  {
    Grsp = gsm.readString();
    Serial.println(Grsp);
  }

  if(Serial.available())
  {
    Arsp = Serial.readString();
    gsm.println(Arsp);
  }

}

Once you upload the code to the Arduino board, you’ll be able to send all the AT commands in order to send/receive text messages or make calls.

Sending SMS

We will directly use the basic Sim800l library to send messages. The code requests you to enter the phone number and the message to be sent and sends the message!

*    
 *   [email protected] 
 *   Example to Send messages Interactively using GSM SIM800L
 *    
 *   LIBRARY CREDITS:
 *   Thanks to Cristian Steib([email protected]) for the library.
 *    
 *  
 *     
 *      PINOUT: 
 *        _____________________________
 *       |  ARDUINO UNO >>>   SIM800L  |
 *        -----------------------------
 *            GND      >>>   GND
 *        RX   2       >>>   TX    
 *        TX   3       >>>   RX
 *       RESET 8       >>>   RST 
 *                 
 *  
*/

#include <Sim800l.h>
#include <SoftwareSerial.h> //is necesary for the library!! 
Sim800l Sim800l;  //to declare the library
char text[161]=""; //buffer to store message
char number[11]=""; //phone number to send message
int cnt;
bool error; //to catch the response of sendSms

void setup(){
      Sim800l.begin(); // initializate the library. 
    Serial.begin(9600);
}

void loop(){

      //Read the Number
        Serial.print("\nEnter 10 digit Phone Number:");
      while(Serial.available()<=0); 
      cnt = Serial.readBytesUntil('\n',number, 11);
      number[cnt] = '\0';
      Serial.println(number);

      //clear the serial input buffer so that no typed characters are pending
            
      delay(1000); //delay required before clearing the input buffer
      while(Serial.available()>0) //clear buffer
      {
        Serial.read();
      }


      //Read the Message to be sent
      Serial.print("Enter Message:");
      while(Serial.available()<=0); 
      cnt = Serial.readBytesUntil('\n',text, 160);
      text[cnt] = '\0';
      Serial.println(text);
      delay(1000);

      while(Serial.available()>0) //clear buffer
      {
        Serial.read();
      }
  
      //Send the message and display the status
      error = Sim800l.sendSms(number,text);
      if(error)
      Serial.println("Error Sending Message");
      Serial.println("Message Sent Successfully!");

      
}

//Thanks to Cristian Steib for the SIM800l library.

The FONA Library

The FONA library is a library for interfacing with the SIM800 module. It allows you to send and receive SMS messages, make and receive calls, and connect to the internet. The SIM800 FONA library is open source and is released under the MIT license. The SIM800 FONA library is easy to use and is well documented. I would recommend this library to anyone looking for an easy way to interface with the SIM800 module.

The library I recommend is Adafruit’s FONA library you can go to Sketch > Include Library > Manage Libraries and type “FONA” in the search bar.

Download the source and click the library:

  1. Library SIM800L: Adafruit FONA

The library comes with a number of examples. The FONA_test sketch provides us access to all the SIM800l functions! If you upload the sketch and use the same wiring diagram.

Control Relay by SMS using SIM 800l

Interface SIM800L with Arduino Tutorial - How It Works, Codes

Source Code to Control Relay by SMS using SIM 800l

#include "Adafruit_FONA.h"
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4

char replybuffer[255];

#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

#define RELAY_PIN 9
#define BUTTON_PIN 8

short sButton_Read = 0;
String smsString = "";
unsigned short gusIsSend_Bef = 0;

void setup() 
{
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH);
  
  pinMode(BUTTON_PIN, INPUT);

  Serial.begin(115200);
  Serial.println(F("FONA SMS caller ID test"));
  Serial.println(F("Initializing....(May take 3 seconds)"));

  // make it slow so its easy to read!
  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial))
  {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }
  Serial.println(F("FONA is OK"));

  // Print SIM card IMEI number.
  char imei[16] = {0}; // MUST use a 16 character buffer for IMEI!
  uint8_t imeiLen = fona.getIMEI(imei);
  if (imeiLen > 0) 
  {
    Serial.print("SIM card IMEI: "); Serial.println(imei);
  }
  
  fonaSerial->print("AT+CNMI=2,1\r\n");  //set up the FONA to send a +CMTI notification when an SMS is received
  Serial.println("FONA Ready");
}

char fonaNotificationBuffer[64];          //for notifications from the FONA
char smsBuffer[250];

void loop() 
{
  char number, message;
  
  char* bufPtr = fonaNotificationBuffer;    //handy buffer pointer
  
  sButton_Read = sRead_Button();

  if(sButton_Read == 1)
  {
    if(gusIsSend_Bef == 0)
    {
      if(!fona.sendSMS("+919835306268", "Button is pressed!"))
      {
        Serial.println(F("Failed"));
      } 
      else 
      {
        Serial.println(F("Sent!"));
      }
      gusIsSend_Bef = 1;
    }
  }
  else
  {
    gusIsSend_Bef = 0;
  }
  
  if (fona.available())      //any data available from the FONA?
  {
    int slot = 0;            //this will be the slot number of the SMS
    int charCount = 0;
    //Read the notification into fonaInBuffer
    do
    {
      *bufPtr = fona.read();
      Serial.write(*bufPtr);
      delay(1);
    } while ((*bufPtr++ != '\n') && (fona.available()) && (++charCount < (sizeof(fonaNotificationBuffer)-1)));

    //Add a terminal NULL to the notification string
    *bufPtr = 0;

    //Scan the notification string for an SMS received notification.
    //  If it's an SMS message, we'll get the slot number in 'slot'
      if (1 == sscanf(fonaNotificationBuffer, "+CMTI: " FONA_PREF_SMS_STORAGE ",%d", &slot)) 
      {
        Serial.print("slot: "); Serial.println(slot);

        char callerIDbuffer[32];  //we'll store the SMS sender number in here

        // Retrieve SMS sender address/phone number.
          if (! fona.getSMSSender(slot, callerIDbuffer, 31)) 
          {
            Serial.println("Didn't find SMS message in slot!");
          }
          Serial.print(F("FROM: ")); Serial.println(callerIDbuffer);

          // Retrieve SMS value.
          uint16_t smslen;
            if (fona.readSMS(slot, smsBuffer, 250, &smslen)) // pass in buffer and max len!
            { 
              smsString = String(smsBuffer);
              Serial.println(smsString);
            }
              if (smsString == "RELAY_ON")
              {
                Serial.println("Relay is activated.");
                digitalWrite(RELAY_PIN, LOW);
                delay(100);

                if(!fona.sendSMS(callerIDbuffer,"Relay is activated." ))
                {
                  Serial.println(F("Failed"));
                } 
                else 
                {
                  Serial.println(F("Sent!"));
                }
              }
              else if(smsString == "RELAY_OFF")
              {
                Serial.println("Relay is deactivated.");
                digitalWrite(RELAY_PIN, HIGH);
                delay(100);

                if(!fona.sendSMS(callerIDbuffer, "Relay is deactivated."))
                {
                  Serial.println(F("Failed"));
                }
                else
                {
                  Serial.println(F("Sent!"));
                }
              }
               
              while(1)
              {
                if(fona.deleteSMS(slot))
                {
                  Serial.println(F("OK!"));
                  break;
                }
                else
                {
                  Serial.print(F("Couldn't delete SMS in slot ")); Serial.println(slot);
                  fona.print(F("AT+CMGD=?\r\n"));
                }
              }
            }  
          }
}

short sRead_Button()
{
  short sButton_Value = 0;
  sButton_Value = digitalRead(BUTTON_PIN);
  return sButton_Value;
}

Change your phone number in this line

Conclusion

 

SMS Sending

The FONA library owns a simple send SMS() function that takes the number and message as parameters and returns true if the message was sent through the network successfully. Here’s an example code that sends one SMS:

#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"

#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4

char replybuffer[255];

SoftwareSerial sim800l = SoftwareSerial(FONA_TX, FONA_RX);

Adafruit_FONA gsm = Adafruit_FONA(FONA_RST);

//uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

void setup() {
  Serial.begin(9600);
  sim800l.begin(9600); 
  if (! gsm.begin(sim800l)) {            
    Serial.println(F("Couldn't find SIM800L!"));
    while (1);
  }
  
   char sendto[21] = "09173234058";
   char message[141] = "Hello from SIM800L";
   gsm.sendSMS(sendto, message);
   delay(1000);
}

void loop() {}

Upload this sketch to your Arduino board and open the serial monitor to send SMS.

Making a Call

To make a call, we must attach a speaker to the SPKRP and SPRKRN terminals of the SIM800L and an electric microphone to the MICP and MICN terminals.

Then we add another part:

void callNumber(char * number){
   if (!gsm.callPhone(number)) {
       Serial.println(F("Failed"));
   } else {
       Serial.println(F("Sent!"));
   }
}

Calling a number is possible via the callPhone() part from the library. Moreover, this function produces true if the call is successful.

Related Articles

Leave a Reply

Your email address will not be published.

Ad
Back to top button