Interfacing Sim800L with Arduino
The blog covers a step by step guide for interfacing the SIM800L GSM module with Arduino.
In this blog, we are going to play with the SIM800L GSM module and on completing this blog you will be knowing what is sim800l, how to play with it and a few more interesting stuff about SIM800L.
Today we are living in the world of IoT, remotely controlling an object has become a craze and time saving for many processes. IoT is playing a very important role in many sectors like industry, public sector and private sectors. In this section, I am going to put a small part of this huge IoT in front of you and for this, we are using an Arduino, Sim800l module and some Du-Pont cables.
So let's begin.
So, we have talked about what sim800l is, SIM800l's interface with Arduino and we learned troubleshooting guides. Congratulations!! You are now well versed in sim800l operations. I hope this blog helps in your project. If there is any questions, please let us know in the comment section.
What is SIM800L?
This is the GSM module similar to the one which is on your phone. And I can assume that every one of you knows the application of GSM module. Speaking of SIM800L module, it is a small chip that uses serial-communication to communicate with any microcontroller or microprocessor. It has an in-built onboard antenna and an onboard SIM slot for SIM insertion purposes. The SIM800L module has 12 total pins that are used to establish connections with the microcontroller. For your simplicity, here I have shared a detailed pinout of the SIM800L and a function of each pin.Sim800L PinOut:
-
Ring
-
DTR
-
Mic+, Mic-, SPK+ & SPK-
-
NET
-
RST
-
VCC and GND
-
RX and TX
Components You Will Need
Hardware Parts
- Arduino Uno
- SIM800l module
- Connecting cables
- 2G Sim card (Please note SIM800l is a 2g GSM module, this means that the 4G sim card will not work with this module. If you are looking for 4G GSM module then click on this link)
Softwares
This is the part list you will need when working on a SIM800l module. Now, we've got all the things that will help us to play with sim800l module. Now it's time to hook up this module with the microcontroller. Here we are using Arduino UNO as a microcontroller. To demonstrate it I have shared the interfacing diagram please take a look [caption id="attachment_660008" align="aligncenter" width="843"] Fig. Interfacing Diagram[/caption]SIM800L Interfacing with Arduino Uno
Now you are all set to play with Sim800l. After interfacing, when you power this module, you will see that the onboard LED is on and blinking in different patterns. This blinking pattern of the onboard LED tells us the working status of the SIM800l module. Pattern1: LED is blinking after each 1s this means that SIM800l is turned 'ON' but not established the connection. Pattern2: LED is blinking after each 3S. It means sim800l has successfully established the connection. Pattern3: LED is blinking after each 2S. It means the GPRS feature is turned on. Troubleshooting SIM800L Module In the above section, we have learned about the blinking pattern of the onboard LED and by analyzing this factor, we can find out if the module is working or not but it is not enough if the problem is in the UART port. To test the functionality of the port and SIM we can shoot some AT commands and for this, we can use the following troubleshooting AT commands.Using At Commands to Test the SIM800L Module
To test the module, connect the SIM800l module to your system with the help of Arduino ==> Open Arduino IDE after this select the com port ==> Open the serial monitor ==> Enter the following AT commands. AT: If the Sim800l module is functioning properly then Sim800l will reply OK after receiving this module. AT+CBC- This command is used to check the battery Voltage. AT+ CSQ- Sim800l module tells the strength of the signal after receiving this AT command AT+CREG- You can use this AT command to register the network if your SIM800l module is abruptly losing network and taking a long time to scan the network. AT+COPS?- If you put this command, SIM800l will tell you about its network status. AT+COPS=? - Sim800l tell us the networks available after receiving this AT command. [caption id="attachment_647981" align="aligncenter" width="598"] Network Status Check[/caption]Setting the Baud Rate
SIM800L supports a 9600 standard baud rate as the default baud rate but you can configure it to any baud rate using the following AT command. ​[caption id="attachment_660014" align="alignnone" width="500"] Baud Rate Setting[/caption] | [caption id="attachment_651871" align="alignright" width="407"] Baud Rate AT Commands[/caption] |
Scanning Network
By default, module scans the network automatically, but if it is not scanning automatically then you can use the following AT command to manually scan the network. [caption id="attachment_651883" align="aligncenter" width="1073"] The response of AT+COPS command.[/caption]Dialling a Number using SIM800L
Making a Call Using Serial Monitor
Arduino Sample Code For Sim800L
In the section below, I have shared some sample codes for SIM800L, using these codes you will be able to send and receive SMS.Troubleshooting Guide
If you use the following code, then your module should work, but it is not working then there may be the following reasons:1 Out of Coverage Area:
In this case, you can run the AT + COPS command to check if it is connected to the network. There can be another reason and that is the issue of power. To make sure you can run AT + CSQ and AT + CBC. AT + CSQ will tell you the signal strength and AT + CBC will tell you the percentage of the battery.2 Functionality is Enabled
If all the above works are working and you have followed this blog from the beginning and still it is not sending SMS then the issue may be here. To overcome this problem you can run the following command AT + CFUN = 1 This command enables the functionality of the module.Sample Code For Dialing a Number:
#include <SoftwareSerial.h>
String inputString = "";
String fromGSM = "";
SoftwareSerial mySerial(3, 2);
char sim800l[255];
int sim800lIndex = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
mySerial.begin(9600);
inputString.reserve(200);
fromGSM.reserve(200);
Serial.println(" Command --> AT ");
mySerial.print("AT");
mySerial.print("\r");
delay(100);
Serial.print(" Response --> ");
while (mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex - 1]);
}
sim800lIndex = 0;
Serial.println("");
Serial.println(" Command --> ATEO ");
Serial.print(" Response --> ");
mySerial.print("ATE0");
mySerial.print("\r");
delay(100);
while (mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex - 1]);
}
sim800lIndex = 0;
Serial.println("");
Serial.println(" Command --> AT+CFUN=1 ");
Serial.print(" Response --> ");
mySerial.print("AT+CFUN =1");
mySerial.print("\r");
delay(100);
while (mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex - 1]);
}
sim800lIndex = 0;
Serial.println("");
mySerial.print("ATD8888844444;"); // enter number ATD<number>
mySerial.print("\r");
while ( !(mySerial.available()) );
while (mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex - 1]);
}
sim800lIndex = 0;
Serial.println(" ");
Serial.println(" Enter 'ATH' command to End the call ");
}
void loop()
{
while (1)
{
while (Serial.available())
{
sim800l[sim800lIndex] = Serial.read();
sim800lIndex++;
mySerial.print( sim800l[sim800lIndex - 1]);
}
while (mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
if (sim800l[sim800lIndex - 4] == 'R' && sim800l[sim800lIndex - 3] == 'I' && sim800l[sim800lIndex - 2] == 'N' && sim800l[sim800lIndex - 1] == 'G')
{
delay(8000);
mySerial.print("ATA"); // AT command to answer the call.
mySerial.print("\r");
}
Serial.print( sim800l[sim800lIndex - 1]);
}
}
}
Reading SMS Automatically:
#include <SoftwareSerial.h>
String inputString = "";
String fromGSM = "";
SoftwareSerial mySerial(3, 2);
char Sim800L[255], SMS[255], smsMemory[5], SmS_Index[5];
int Sim800LIndex = 0, SMS_Index = 0, S_Index = 0;
bool NewSMSArrived = 0, SMSmemory = 0;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
inputString.reserve(200);
fromGSM.reserve(200);
Serial.println(" Command --> AT ");
mySerial.print("AT");
mySerial.print("\r");
delay(100);
Serial.print(" Response --> ");
while (mySerial.available())
{
Sim800L[Sim800LIndex] = mySerial.read();
Sim800LIndex++;
Serial.print( Sim800L[Sim800LIndex - 1]);
}
Sim800LIndex = 0;
Serial.println("");
Serial.println(" Command --> ATEO ");
Serial.print(" Response --> ");
mySerial.print("ATE0");
mySerial.print("\r");
delay(100);
while (mySerial.available())
{
Sim800L[Sim800LIndex] = mySerial.read();
Sim800LIndex++;
Serial.print( Sim800L[Sim800LIndex - 1]);
}
Sim800LIndex = 0;
Serial.println("");
Serial.println(" Command --> AT+CFUN=1 ");
Serial.print(" Response --> ");
mySerial.print("AT+CFUN =1");
mySerial.print("\r");
delay(100);
// while( ! (mySerial.available()) );
while (mySerial.available())
{
Sim800L[Sim800LIndex] = mySerial.read();
Sim800LIndex++;
Serial.print( Sim800L[Sim800LIndex - 1]);
}
Sim800LIndex = 0;
Serial.println("");
Serial.println(" Command --> AT+CMGF=1 ");
Serial.print(" Response --> ");
mySerial.print("AT+CMGF =1");
mySerial.print("\r");
delay(100);
// while( ! (mySerial.available()) );
while (mySerial.available())
{
Sim800L[Sim800LIndex] = mySerial.read();
Sim800LIndex++;
Serial.print( Sim800L[Sim800LIndex - 1]);
}
Serial.println(" Command --> AT+CSCS=\"GSM\"");
Serial.print(" Response --> ");
mySerial.print("AT+CSCS =\"GSM\"");
mySerial.print("\r");
delay(100);
// while( ! (mySerial.available()) );
while (mySerial.available())
{
Sim800L[Sim800LIndex] = mySerial.read();
Sim800LIndex++;
Serial.print( Sim800L[Sim800LIndex - 1]);
}
Serial.println(" Command --> AT+CPMS=\"SM\"");
Serial.print(" Response --> ");
mySerial.print("AT+CPMS =\"SM\"");
mySerial.print("\r");
delay(100);
// while( ! (mySerial.available()) );
while (mySerial.available())
{
Sim800L[Sim800LIndex] = mySerial.read();
Sim800LIndex++;
Serial.print( Sim800L[Sim800LIndex - 1]);
}
/*
Serial.println(" Command --> AT+CMGS=\"8669026867\"");
Serial.print(" Response --> ");
mySerial.print("AT+CMGS=\"8669026867\"");
mySerial.print("\n");
delay(100);
//while( ! (mySerial.available()) );
while(mySerial.available())
{
Sim800L[Sim800LIndex] = mySerial.read();
Sim800LIndex++;
Serial.print( Sim800L[Sim800LIndex-1]);
}
mySerial.print(" ENTER THE TXT YOU WISH TO SEND IN THE SMS ");
mySerial.write(0x1A);
*/
Serial.print(" CHECK THE DESTINATION DEVICE ");
}
void loop()
{
while (1)
{
while (Serial.available())
{
Sim800L[Sim800LIndex] = Serial.read();
Sim800LIndex++;
mySerial.print( Sim800L[Sim800LIndex - 1]);
}
while (mySerial.available())
{
Sim800L[Sim800LIndex] = mySerial.read();
Sim800LIndex++;
Serial.print( Sim800L[Sim800LIndex - 1]);
if (Sim800L[Sim800LIndex - 6] == '+' && Sim800L[Sim800LIndex - 5] == 'C' && Sim800L[Sim800LIndex - 4] == 'M' && Sim800L[Sim800LIndex - 3] == 'T' && Sim800L[Sim800LIndex - 2] == 'I' && Sim800L[Sim800LIndex - 1] == ':')
{
SMSmemory = 1;
NewSMSArrived = 1;
}
if (SMSmemory == 1)
{
if (Sim800L[Sim800LIndex - 1] == ',')
{
smsMemory[0] = Sim800L[Sim800LIndex - 4] ;
smsMemory[1] = Sim800L[Sim800LIndex - 3] ;
smsMemory[2] = '\0' ;
SMSmemory = 0;
}
}
delay(5);
} // end of while mySerial.available()
if (NewSMSArrived == 1)
{
int j = 0;
while (Sim800L[j] != '\0')
{
j++;
}
for (int i = 0; i < j ; i++)
{
SMS[i] = Sim800L[i];
}
NewSMSArrived = 0;
Serial.println(SMS);
while (SMS[j - 1] != ',')
{
j--;
}
int i = 0;
while (SMS[j - 1] != '\0' )
{
SmS_Index[i] = SMS[j];
i++;
j++;
}
Serial.println("SMS index is as follow ");
Serial.println(SmS_Index);
Serial.println("SMS Memory is as follow ");
Serial.println(smsMemory);
Serial.println("");
Serial.println(" NEW SMS ARRIVED ");
Serial.println("");
mySerial.print("AT+CMGR=");
mySerial.print(SmS_Index);
mySerial.print("\r");
}
}
}
Sending the SMS Using SIM800L
#include <SoftwareSerial.h>
String inputString="";
String fromGSM="";
SoftwareSerial mySerial(3,2);
char sim800l[255],SMS[255];
int sim800lIndex=0,SMS_Index=0;
bool NewSMSArrived=0;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
Serial.println(" Command --> AT ");
mySerial.print("AT");
mySerial.print("\r");
delay(100);
Serial.print(" Response --> ");
while(mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex-1]);
}
sim800lIndex=0;
Serial.println("");
Serial.println(" Command --> ATEO ");
mySerial.print("ATE0");
mySerial.print("\r");
delay(100);
Serial.print(" Response --> ");
while(mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex-1]);
}
sim800lIndex=0;
Serial.println("");
Serial.println(" Command --> AT+CFUN=1 ");
mySerial.print("AT+CFUN =1");
mySerial.print("\r");
delay(100);
Serial.print(" Response --> ");
// while( ! (mySerial.available()) );
while(mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex-1]);
}
sim800lIndex=0;
Serial.println("");
Serial.println(" Command --> AT+CMGF=1 ");
Serial.print(" Response --> ");
mySerial.print("AT+CMGF =1");
mySerial.print("\r");
delay(100);
// while( ! (mySerial.available()) );
while(mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex-1]);
}
Serial.println(" Command --> AT+CSCS=\"GSM\"");
Serial.print(" Response --> ");
mySerial.print("AT+CSCS =\"GSM\"");
mySerial.print("\r");
delay(100);
// while( ! (mySerial.available()) );
while(mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex-1]);
}
Serial.println(" Command --> AT+CPMS=\"SM\"");
Serial.print(" Response --> ");
mySerial.print("AT+CPMS =\"SM\"");
mySerial.print("\r");
delay(100);
// while( ! (mySerial.available()) );
while(mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex-1]);
}
Serial.println(" Command --> AT+CMGS=\"4444444444\"");
Serial.print(" Response --> ");
mySerial.print("AT+CMGS=\"4444444444\"");
mySerial.print("\n");
delay(100);
// while( ! (mySerial.available()) );
while(mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
Serial.print( sim800l[sim800lIndex-1]);
}
mySerial.print(" ENTER THE TXT YOU WISH TO SEND IN THE SMS "); // txt msg
mySerial.write(0x1A);
Serial.print(" CHECK THE DESTINATION DEVICE ");
}
void loop()
{
while(1)
{
while(Serial.available())
{
sim800l[sim800lIndex] = Serial.read();
sim800lIndex++;
mySerial.print( sim800l[sim800lIndex-1]);
}
while(mySerial.available())
{
sim800l[sim800lIndex] = mySerial.read();
sim800lIndex++;
/* if(sim800l[sim800lIndex-5]=='+'&&sim800l[sim800lIndex-4]=='C'&&sim800l[sim800lIndex-3]=='M'&&sim800l[sim800lIndex-2]=='T'&&sim800l[sim800lIndex-1]=='I')
{
NewSMSArrived=1;
} */
Serial.print( sim800l[sim800lIndex-1]);
}
/* if(NewSMSArrived == 1)
{
for(int i=0;i= (sim800lIndex-1);i++)
{
SMS[i] = sim800l[i];
}
NewSMSArrived=0;
Serial.print(SMS);
} */
}
}