Arduino GSM Communication with SIM800L

Author : Dinesh Kumar Wickramasinghe

Introduction and Images

This tutorial is about GSM communication with Arduino. Many GSM modules are available to purchase that you can connect to Arduino and try GSM communication experiments. Some GSM boards have built in GPS receivers also.

In this project I will use the cheap and basic SIM800L GSM module. You can download the datasheet of this module here : Download Datasheet

Here are few images of my completed project setup.

SIM800L GSM Module

You can buy the SIM800L module around 2 - 3 USD. Here is a picture of the module. You have to carefully solder the headers to the module. Do not put too much heat. Also solder the spiral GSM antenna as on the picture.

SIM800L GSM Module

Here are the pinouts of the module.

SIM800L GSM Module Pinouts
What hardware needed?

So, to complete this experiment, you need below hardware components.

  • SIM800 GSM Module
  • Arduino UNO, Nano, Mega or any compatible board
  • Some hook-up wires
  • 2 X 18650 Batteries or Buck power supply (Because the GSM module need more current and difficult to power up using Arduino power out pins)
  • Small speaker and a small Condenser microphone (Only for making call experiment)
Schematic for the basic experiment

Here is the basic setup of the components.

Arduino SIM800L Basic Setup

Here is an image of my actual devices setup.

Arduino SIM800L Basic Setup Actual
Power Supply

The GSM module needs 3.4V to 4.4V voltage and when communicating with the GSM networks, it needs nearly 2A current. So, normally the Arduino power output does not capable of powering up the module. We need to use an external power supply. I could get the best results using parallely connected two 18650 batteries (As on the schematic). It could provide the necessary voltage and current for the module. You can also try using a Buck converter or a Li-Po battery. Be careful about the voltage. Over voltage will burn the module.

Communicating with the SIM800 Module

AT Commands are used to send commands to the module.

You can download the full AT Commands guide here : Download

We can use serial communication between Arduino and GSM module to send AT commands.

Here are a few examples of basic AT commands.

Command Usage
AT Most basic AT command. This command ping the module. You will get OK as a reply if your device communicating correctly with Arduino
AT+CSQ Check the signal strength
AT+CCID Get the SIM card number
AT+GSV Display SIM card product information

Now let’s do some experiments with the module step by step. Double check all your connections and connect your Arduino to PC

GSM Connectivity and Signal strength

You will get a small spiral antenna with the module. You can solder it to the module. If you are living in an area with good GSM signal strength, this small spiral antenna is enough.

SIM800L Spiral Antenna

Otherwise you have to buy a good GSM antenna. There is a small connecter in this module to connect external antennas. I also used an antenna like below and I experienced very good signal strength.

GSM Antenna

Please note that, if the small LED on the module blinks every 1 second, that means the module is running but not made a connection to a GSM network.

If the LED blinks every 3 seconds, that means the module has made a connection to a GSM network.

Let’s Play with the Module

Please note that there are some libraries which you can use for this experiment. But I will use examples without using a library. So that you can understand how the AT commands work. Later I will show you how to use a library. Only few examples I am showing here. But there are many features available with this tiny module like the GPRS connectivity. You can investgage more by reading the datasheet.

Experiment 1 : Basic AT commands test

This sketch (code) will send some basic AT commands to the module and you can see the output on the serial monitor. Please read the code comments to understand different AT commands.

#include <SoftwareSerial.h>

//SIM800L Tx & Rx is connected to Arduino 7 & 8
SoftwareSerial gsmSerial(7, 8); 

void setup()
{
  //Start serial communication
  Serial.begin(9600);
  gsmSerial.begin(9600);

  Serial.println("Initializing...");
  delay(1000);

  gsmSerial.println("AT"); //Basic AT command
  updateSerial();
  gsmSerial.println("AT+CSQ"); //GSM signal strength. 0-31 , 31 is the best
  updateSerial();
  gsmSerial.println("AT+CCID"); //Read SIM card information
  updateSerial();
  gsmSerial.println("AT+CREG?"); //Check if the module is registered to a network
  updateSerial();
  gsmSerial.println("AT+CBC"); //Charging status and remaining battery capacity
  updateSerial();
  gsmSerial.println("AT+GSV"); //Product information
  updateSerial();
}

void loop()
{
  updateSerial();
}

//Method for serial communication
void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    gsmSerial.write(Serial.read());
  }
  while(gsmSerial.available()) 
  {
    Serial.write(gsmSerial.read());
  }
}

                    

You will see an output like below screenshot.

Arduino SIM800L Basic AT Commands Serial Monitor
Experiment 2 : Send an SMS

To send an SMS, try the below code.

#include <SoftwareSerial.h>

SoftwareSerial gsmSerial(7, 8);

void setup()
{
  //Start serial communication
  Serial.begin(9600);
  gsmSerial.begin(9600);

  Serial.println("Starting..");
  delay(1000);

  gsmSerial.println("AT");
  updateSerial();
  gsmSerial.println("AT+CMGF=1");// Change to text mode
  updateSerial();
  gsmSerial.println("AT+CMGS=\"+94123456789\""); //Your mobile number with country code
  updateSerial();
  gsmSerial.print("Hello, Regards from SIM800"); //Your message
  updateSerial();
  gsmSerial.write(26);
}

void loop()
{
  //Nothing to do with the loop in this code
}

//Method for serial communications
void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    gsmSerial.write(Serial.read());
  }
  while(gsmSerial.available()) 
  {
    Serial.write(gsmSerial.read());
  }
}

                    

Here I received the SMS to my mobile phone.

SIM800L Arduino Receive SMS Phone
Experiment 2 : Read SMS messages

Below code will read SMS messages. Everytime when a new SMS message received, it will display it on the serial monitor.

#include <SoftwareSerial.h>

//SIM800L Tx & Rx is connected to Arduino 7 & 8
SoftwareSerial gsmSerial(7, 8);

void setup()
{
  //Start serial communication
  Serial.begin(9600);
  gsmSerial.begin(9600);

  Serial.println("Starting..");
  delay(1000);

  gsmSerial.println("AT");
  updateSerial();
  gsmSerial.println("AT+CMGF=1");// Change to text mode
  updateSerial();
  gsmSerial.println("AT+CNMI=1,2,0,0,0");
  updateSerial();
}

void loop()
{
  updateSerial();
}

//Method for serial communications
void updateSerial()
{
  delay(2000);
  while (Serial.available()) 
  {
    gsmSerial.write(Serial.read());
  }
  while(gsmSerial.available()) 
  {
    Serial.write(gsmSerial.read());
  }
}

                    

I am replying back the the previous SMS that I received from the module.

SIM800L Arduino SMS reply

Here is the serial monitor output

SIM800L Arduino Read SMS Serial Monitor
Experiment 3 : Making a Voice Call

You can make voice calls using the below example code. Call time will be 20 seconds in this example code. You can increase it by modifying the delay.

There are dedicated pins in the SIM800 module for a Speaker and Microphone connectivity. If you connect a small speaker and a small Capacitive Electret Condenser microphone, you can talk.


SIM800L Arduino Voice Call Schematic

Here is my setup for the experiment

SIM800L Arduino Voice Call Setup

Here is the code for this voice call experiment

#include <SoftwareSerial.h>

//SIM800L Tx & Rx is connected to Arduino 7 & 8
SoftwareSerial gsmSerial(7, 8);

void setup()
{
  //Start serial communication
  Serial.begin(9600);
  gsmSerial.begin(9600);

  Serial.println("Starting.."); 
  delay(1000);

  gsmSerial.println("AT"); //Basic AT command
  updateSerial();
  
  gsmSerial.println("ATD+ +9471123456789;\r"); //Number to call
  updateSerial();
  delay(90000); // wait for 20 seconds...
  gsmSerial.println("ATH"); //Hang up the call
  updateSerial();
}

void loop()
{
}

//Method for serial communication
void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    gsmSerial.write(Serial.read());
  }
  while(gsmSerial.available()) 
  {
    Serial.write(gsmSerial.read());
  }
}
                    

You may experience that the voice is not clear. For a better result you need to connect some additional components as below schematic.

SIM800L Arduino Speaker Configuration
SIM800L Arduino Microphone Configuration

I got the above schematic from the data sheet of the module.

Controlling Relays by SMS

This example is a practical simple application of controlling a device by SMS. I used a two-way relay module connected to Arduino that I can control two devices separately by two SMS messages.

Here is the schematic.

SIM800L Arduino SMS Relay Schematic

Here is the picture of my setup. You can see I’ve connected a 230V bulb in this experiment. You can also connect devices that works on main voltage. But be careful when you are working with high voltages. Also pay attention to the maximum current that the relay can support.

SIM800L Arduino SMS Relay

Here is the code for this example

#include <SoftwareSerial.h>

SoftwareSerial gsmSerial(7, 8);
char incomingByte; 
String inputString;

void setup() 
{
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(2, LOW);
    pinMode(3, LOW);
    Serial.begin(9600);
    gsmSerial.begin(9600); 

    while(!gsmSerial.available()){
    gsmSerial.println("AT");
        delay(1000); 
        Serial.println("Connecting...");
    }
    Serial.println("Connected!");  
    gsmSerial.println("AT+CMGF=1");
    delay(1000);  
    gsmSerial.println("AT+CNMI=1,2,0,0,0"); 
    delay(1000);
    gsmSerial.println("AT+CMGL=\"REC UNREAD\"");
}

void loop()
{  
  if(gsmSerial.available()){
    inputString = "";
    delay(1000);
    while(gsmSerial.available()){
      incomingByte = gsmSerial.read();
      inputString += incomingByte; 
    }
    delay(100);      
    //inputString.replace("\n","");
    Serial.print("input : ");
    Serial.print(inputString);
    Serial.println("   end");
    
    inputString.toUpperCase();
    Serial.println("input up : " + inputString);
    if (inputString.indexOf("ON1") > 0){
      pinMode(2, HIGH);
    }
    if (inputString.indexOf("ON2") > 0){
      pinMode(3, HIGH);
    } 
    if (inputString.indexOf("OFF1") > 0){
      pinMode(2, LOW);
    }
    if (inputString.indexOf("OFF2") > 0){
      pinMode(3, LOW);
    }         
    delay(50);
    if (inputString.indexOf("OK") > -1){
      gsmSerial.println("AT+CMGDA=\"DEL ALL\"");
      delay(1000);
    } 
  }
}

                    

The SMS message ON1 will turn the Relay 1 on, and ON2 for the second relay. Also OFF1 and OFF2 messages to turn them off.

Send ON1 command (SMS)

SIM800L Arduino SMS Relay ON

Bulb turns on

SIM800L Arduino SMS Relay ON

Send OFF1 command (SMS)

SIM800L Arduino SMS Relay OFF

Bulb turns off

SIM800L Arduino SMS Relay OFF

There are many libraries that support the SIM800 module. Out of those libraries, I found that the GSMSIM library is nicely written.

Git Repo : https://github.com/erdemarslan/GSMSim

Go to Arduino library manager and search for GSMSIM and install it.

You will get a few interesting examples with the library. Here is an example of sending and reading SMS

#include <GSMSim.h>

#define RX 7
#define TX 8
#define RESET 2
#define BAUD 9600


GSMSim gsm;

/*
 * Also you can this types:
 * GSMSim gsm(RX, TX);
 * GSMSim gsm(RX, TX, RESET);
 * GSMSim gsm(RX, TX, RESET, LED_PIN, LED_FLAG);
 */

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println("GSMSim Library - SMS Example");
  Serial.println("");
  delay(1000);

  gsm.start(); // baud default 9600
  //gsm.start(BAUD);

  Serial.println("Changing to text mode.");
  gsm.smsTextMode(true); // TEXT or PDU mode. TEXT is readable :)

  char* number = "+905123456789";
  char* message = "Hi my friend. How are you?"; // message lenght must be <= 160. Only english characters.

  Serial.println("Sending Message --->");
  Serial.println(gsm.smsSend(number, message)); // if success it returns true (1) else false (0)
  delay(2000);

  Serial.println("Listing unread message(s).");
  Serial.println(gsm.smsListUnread()); // if not unread messages have it returns "NO_SMS"

  Serial.println("Read SMS on index no = 1");
  Serial.println(gsm.smsRead(1)); // if no message in that index, it returns IXDEX_NO_ERROR
  
}

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

                    
Conclusion and References

This blog post is only an intro to this module. And there are many articles on the internet about this module. Actually you can do interesting things with this module. If you believe or not, this module has an FM receiver. Also it supports GPRS connectivity. You may try out these features.

I referred few links to prepare this article. Here are the references. Thanks for the authors.

Hope you enjoyed this tutorial. If you see any errors, mistakes, suggestions, questions, please comment. Have a nice day.


Comments Area
Add your questions and comments here. Comments will be published after the admin approval
Published By : midi faz
Added Date :6/8/2022 7:03:22 PM
hi dear i made your project but my serial monitor do not work at (Experiment 3 : Making a Voice Call ) and no voice from speaker and when i connect to project other phone said the phone is off .please help me thanks
Published By : Olusesi Shola
Added Date :8/22/2021 1:54:24 AM
I am a learner anxiously willing to learn. Can I have a complete code of a similar operation, kind of project. Arduino code to start a 'portable generator' Instead of SMS, the first call to SIM800L activate the first relay permanently ( ON STAGE) and only energise the second relay for 5 seconds (CRANK STAGE). After which the second call deactivate the first relay. The status of generator should also be accompany with SMS. Ie Generator running, generator OFF The PIC should only allow calls from numbers only on the inserted SIM card and send SMS accordingly. After the temporary energizing the second relay, a delay of 10 seconds should be observed after which a signal of 5 volt is sent to the PIC. If this 5 volt signal did not come. Relay 2 should be energise again. ( 3 attempt). If the 5 volt fail to come. The unit should send SMS ...Generator faulty. I can be reach on whatsapp 2348070544558
Published By : mojtaba
Added Date :1/19/2021 1:31:50 AM
Hi . Your post is very good. I have a question about ussd; How to send the amount of balance that is shown by running the code at + cusd in the monitor serial as SMS? In fact, how can we understand the balance of the sim800l SIM card with a mobile phone?
Published By : Thomas
Added Date :10/10/2020 6:21:27 PM
Hello, I am a beginner in Arduino projects. What should the code look like after the modification so that by sending a text message from an authorized phone number it activates the relay and ignores it from an unauthorized one. Thank you for your help. Thomas
Published By : Abdulazeez Yusuf
Added Date :5/12/2020 1:12:35 AM
Hello. Thanks a lot for your well detailed information and guidance,it really helped. Please can you help with gsm A6 and Sim 900 gprs/gsm libraries ? Thank you..
Published By : Abdulazeez Yusuf
Added Date :5/11/2020 5:24:34 PM
Thank you so much for the information, it really helped.. please can you assist with the libraries for A6 Gsm and Sim 900 GPRS/Gsm modules libraries,?? Am using them for my projects but I can't find reliable libraries.. Thank you..
Published By : Ian nicky
Added Date :4/24/2020 3:30:22 AM
Hi.. I like your post, you give me a lot information.. I have one question, is it the sms in the sim800 have limit?? If yes, So how to delete the sms using phone?? Can you add the code?
Published By : Feliz
Added Date :4/12/2020 2:34:31 PM
Olá amigo, bom projeto gostei bastante. Eu tenho uma dúvida, no código para ligar 2 relés, não está especificar o número de distinatário
Published By : zamany
Added Date :3/23/2020 3:25:58 PM
Hi i closed the circuit in the monitor monitor information comes but the relays don't work
Published By : manuel
Added Date :12/12/2019 6:38:34 AM
Hi. 'glad to know the details of your work. What I've done last time what a sort of answering machine to handle incoming calls. But my problem is the noisy or static audio signal which is very inconsiderable. I've researched a lot to alleviate the harmonics and I read that it needs low pass filter. But I didn't find the suitable components and electronic diagram. What I need then is the enhancement you shared. I will try it when I have time. Thanks for being generous.

 

Similar Projects

Go Top