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.
Here are the pinouts of the module.
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.
Here is an image of my actual devices setup.
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.
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.
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.
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.
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.
Here is the serial monitor output
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.
Here is my setup for the experiment
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.
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.
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.
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)
Bulb turns on
Send OFF1 command (SMS)
Bulb turns 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.