Arduino Sim800L GSM GPRS Module Tutorial
Table of Contents
Arduino SIM800L:
Arduino Sim800L GSM GPRS Module, Send SMS, Receive SMS, Request SMS, and Alert Message- Now, you can control your electrical loads and monitor sensors by sending a message from any cell phone using the Arduino and GSM Sim800L module.
You might be wondering if anyone can control loads or monitor sensors by sending a message from any cell phone, then anyone could misuse it.
Well, that’s not the case. I have designed this system with careful consideration. To control any load or request the value of a sensor, you need to enter a password. And you know, without a password, nobody would be able to control anything or request the sensor value.
And the most amazing thing about this project is that you can send control and request commands from any cell phone. And my designed system will reply back to you on that same number. When Arduino receives a message, it extracts the sender’s cell number and replies back to that number. Isn’t it amazing? Of course, it is.
Now, if your SMS package runs out, or your cell phone is turned off, or if it gets lost, there’s no need to worry. You can use anyone’s cell phone to control your loads and monitor sensors. Let me practically demonstrate it for you.
Since this is just a prototype model and a beginners level project so I have tried my level best to make it as simple as possible. So, that’s why, for the demonstration purposes I am using a potentiometer as a sensor and 110/220Vac Bulb as the load. But if you want you can use any other analog or digital sensor and the same thing applies to the Bulb.
Anyway, I have powered up the Arduino board, and I have also connected the 220Vac supply. When the 110/220Vac supply is connected, never touch the relay contacts.
It is important to note that when working with mains voltage, proper safety precautions should always be taken, and it is advisable to consult relevant electrical codes and standards.
I am sure you might have noticed that I am using a single 12V power supply and this is because my designed Arduino Nano development board has a 5V and 3A power supply. Which is more then enough to power up the Arduino, GSM Sim800L module, and all the other electronics.
I have seen many people complaining about the GSM Sim800L module, stating that it doesn’t connect to the network or keeps restarting. Such issues are often related to the power supply. So, if you use the same 5V and 3A power supply, you shouldn’t face any problems.
If you don’t want to make such a development board, you can also make this 5V and 3A power supply.
As you can see, the Sim800L module is connected to the GSM network. Now, let’s try turning this bulb on and off.
The message simply consists of the password.
This password can consist of letters, numbers, and special characters; then the word relay, and finally the command on or off.
You might have noticed I used percentage sign; I am doing this because when the Arduino receives this message. Then the Arduino can easily split this message using % sign as the delimiter. Anyway, the message is ready, and I am going to send it to the Arduino.
As you can see, the bulb has turned ON, and it also sent me a feedback message that the “relay is turned on”. No matter which number you use to send the message, you will receive the feedback on that same number. This feature is incredibly amazing, and I’m sure you will like it too.
Let’s turn off the Blub. This time, in the message I have written “off.”
As you can see, the bulb has turned OFF, and the Arduino also sent me a feedback message “relay is turned off”.
Now, let’s check the value of the sensor.
This time, I have only written the password, and the command “req” the message format remains exactly the same. Let’s send this message.
As you can see, I have received the sensor value 221. Now, I’ll rotate the knob of the potentiometer slightly to change the value. Let’s request the sensor value again.
This time, you can see I have received a different value. In this way, you can monitor the sensor at any time and from any cell number, provided you remember the password and message format.
And not just that. In this project, I have also added an automatic feature. Whenever the sensor value crosses the threshold value, I receive a message saying “value exceeded.” So, let’s increase the value and see if we receive the message.
This is amazing. In one article, you will learn all these things. So, without any further delay, let’s get started!
Amazon Links:
Arduino Nano USB-C Type (Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Sim800L GSM module:
The SIM800L is a GSM/GPRS module that enables communication over the GSM (Global System for Mobile Communications) network. It is commonly used in embedded systems and IoT (Internet of Things) projects for applications such as sending SMS messages, making phone calls, and connecting to the internet over GPRS. Sim800L module supports quad-band GSM/GPRS networks, which means it will work almost anywhere in the world.
The SIM card holder or slot is on the bottom side. Direction for inserting the SIM card is engraved.
Just make sure the Notch is pointing upwards when inserting the SIM. On my side, I have successfully tested it using Ufone and Zong sim cards, and I am sure its also going to work with Jazz and Telenor.
This is the u.fl connector for connecting other antennas other than the Helical Antenna connected to the pin number 1. In my case, I have connected both the Antenna’s.
This is the Network Status indicator Led. So when you power up the Sim800L module, initially, it blinks faster, so, you will need to wait for a few seconds. When it’s connected to the network then it starts to blink at a slower rate. But, if for any reason it blinks at a faster rate then remove the SIM card and insert it again and also check your power supply, I already explained it during the practical demonstration.
Here are some key features and technical specifications of the SIM800L module:
Network Compatibility:
Supports GSM/GPRS 850/900/1800/1900MHz bands.
Can be used globally, as it covers the widely used GSM frequency bands.
Communication Interfaces:
UART (Universal Asynchronous Receiver/Transmitter) interface for communication with microcontrollers or other devices.
AT command set for controlling and configuring the module.
Data Transfer:
GPRS multi-slot class 12/10.
GPRS mobile station class B.
SMS (Short Message Service):
Supports sending and receiving SMS messages.
AT commands are used to manage SMS functionality.
Voice Functionality:
Allows for making and receiving phone calls.
Supports audio codecs like HR, FR, EFR, and AMR.
Internet Connectivity:
GPRS connectivity for internet access.
TCP/IP stack for data transfer over the internet.
SIM Card Interface:
Standard SIM card slot for GSM network authentication.
Supports SIM card hot-plugging.
Power Supply:
Typically operates on 3.4V to 4.4V DC.
Low power consumption.
Dimensions:
Compact size, suitable for integration into small devices.
Operating Temperature:
Generally, it can operate in a temperature range from -40°C to +85°C.
Control and Configuration:
Controlled through AT commands, which are ASCII commands sent via the UART interface.
Can be configured for various modes and functionalities using these commands.
Sim800L Pinout:
It has a total of 12 pins and the bad thing is there is no labeling. So, you will have to remember all these pins.
Net Pin: Net pin is where you can solder the Helical Antenna that comes with the module.
VCC Pin: VCC Pin is the power supply pin. Connect it to a stable power source within the specified voltage range (typically 3.4V to 4.4V). This provides the necessary operating power for the module.
RST Pin: The RST pin is used to reset the SIM800L module. When this pin is pulled low or connected to a logic low signal for atleast 100ms, it triggers a hard reset, restarting the module.
RXD Pin: RXD is the pin used for receiving data by the SIM800L module. Connect it to the TX pin of your microcontroller or other communication device.
TXD Pin: TXD is the pin used for transmitting data from the SIM800L module. Connect it to the RX pin of your microcontroller or other communication device.
GND Pin: GND is the ground reference pin. Connect this pin to the ground of your system to establish a common ground reference.
The SPK- and SKP+ pins are part of the audio interface and are used for connecting the negative and positive terminals of an external 8-ohm speaker.
The MIC- and MIC+ pins are used to connect the negative and positive terminals of an external microphone.
DTR (Data Terminal Ready) Pin: DTR Pin controls the sleep mode. Pulling it HIGH causes the module to enter sleep mode, disabling serial communication. Pulling it LOW for approximately 50ms will wake up the module.
Ring Pin: Ring Pin is the Ring Indicator. This is basically the ‘interrupt-out’ pin from the module. It is by default HIGH and can be configured to go LOW when a call or SMS is received.
If you want to know more about its technical specifications and other features then ready my article. I have added a link in the description. Now, let’s go ahead and take a look at the wiring.
SIM800L interfacing with Arduino:
Connect the VCC and GND pins to the 5V and 3A power supply. Connect the TXD pin to the Arduino D9 and connect the RXD pin to the Arduino D10.
Out of these 4 relays I am using only 1 relay. It’s a 5V SPDT type relay and its connected to the Arduino digital pin D4. Make sure you connect the Relay module VCC and GND pins to the 5V power supply, if incase you are using an external 1 channel or multiple channels relay module.
Connect the Leftmost and rightmost legs of the Potentiometer to the 5V and GND. Connect the middle leg of the potentiometer to the Arduino analog pin A0.
You can see the Vin and GND wires in the circuit diagram. Simply, connect these wires to a 5V and 3A power supply.
You can follow this circuit diagram; if you want to make your own 5V and 3A power supply.
SIM800L Arduino Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
#include <SoftwareSerial.h> SoftwareSerial sim800(9, 10); // TXpin=9 RXpin=10 int relay=4; int pot=A0; String text; String callerID; String caller_id; String passcode="abc123"; String incoming = ""; boolean isReply = false; boolean getResponse(String expected_answer, unsigned int timeout=1000,boolean reset=false){ boolean flag = false; String response = ""; unsigned long previous; //************************************************************* for(previous=millis(); (millis() - previous) < timeout;){ while(sim800.available()){ response = sim800.readString(); //---------------------------------------- //Used in resetSIM800L function //If there is some response data if(response != ""){ Serial.println(response); if(reset == true) return true; } //---------------------------------------- if(response.indexOf(expected_answer) > -1){ return true; } } } //************************************************************* return false; } void setup() { Serial.begin(9600); pinMode(relay,OUTPUT); Serial.println("Arduino serial initialize"); //----------------------------------------------------- sim800.begin(9600); Serial.println("SIM800L software serial initialize"); delay(100); sim800.println("AT"); getResponse("OK",1000); sim800.println("AT+CMGF=1"); getResponse("OK",1000); sim800.println("AT+CNMI=1,2,0,0,0"); getResponse("OK",1000); //delete all sms sim800.println("AT+CMGD=1,4"); delay(1000); sim800.println("AT+CMGDA= \"DEL ALL\""); delay(1000); } void loop() { int potvalue=analogRead(pot); Serial.println(potvalue); while(sim800.available()) { incoming=sim800.readString(); Serial.println(incoming); if(incoming.indexOf("+CMT:") > -1){ String callerID = getCallerID(incoming); caller_id=callerID; String cmd = getMsgContent(incoming); Serial.println("cmd command = "); Serial.print(cmd); String fulldata = getValue(incoming, '%', 0); String password = getValue(incoming, '%', 1); //contains password String load= getValue(incoming, '%', 2); //contains load number String loadstatus= getValue(incoming, '%', 3);// contains on and off state Serial.print("value 1: "); Serial.println(password); Serial.print("value 2: "); Serial.println(load); Serial.print("value 3: "); Serial.println(loadstatus); //int loadvalue = loadstatus.toInt(); if((password==passcode)&&(load=="relay")&&(loadstatus=="on")) { digitalWrite(relay,HIGH); text=""; text ="relay is turned on"; Reply(text, callerID); } else if((password==passcode)&&(load=="relay")&&(loadstatus=="off")) { digitalWrite(relay,LOW); text=""; text ="relay is turned off"; Reply(text, callerID); } else if((password==passcode)&&(load=="req")) { text=""; text =potvalue; Reply(text, callerID); } delay(20); } } if(potvalue>900) { text=""; text ="Value Exceed"; Reply(text, caller_id); delay(3000); } //Serial.println(caller_id); } String getValue(String data, char separator, int index) { int found = 0; int strIndex[] = { 0, -1 }; int maxIndex = data.length() - 1; for (int i = 0; i <= maxIndex && found <= index; i++) { if (data.charAt(i) == separator || i == maxIndex) { found++; strIndex[0] = strIndex[1] + 1; strIndex[1] = (i == maxIndex) ? i+1 : i; } } return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; } String getCallerID(String buff){ unsigned int index, index2; index = buff.indexOf("\""); index2 = buff.indexOf("\"", index+1); String callerID = buff.substring(index+1, index2); callerID.trim(); //Serial.print("index+1= "); Serial.println(index+1); //Serial.print("index2= "); Serial.println(index2); //Serial.print("length= "); Serial.println(callerID.length()); Serial.println("Caller ID: "+callerID); return callerID; } String getMsgContent(String buff){ unsigned int index, index2; index = buff.lastIndexOf("\""); index2 = buff.length(); String command = buff.substring(index+1, index2); command.trim(); command.toLowerCase(); //Serial.print("index+1= "); Serial.println(index+1); //Serial.print("index2= "); Serial.println(index2); //Serial.print("length= "); Serial.println(msg.length()); Serial.println("Command:"+command); return command; } void Reply(String text, String caller_id) { //return; sim800.print("AT+CMGF=1\r"); delay(1000); sim800.print("AT+CMGS=\""+caller_id+"\"\r"); delay(1000); sim800.print(text); delay(100); sim800.write(0x1A); //ascii code for ctrl-26 //sim800.println((char)26); //ascii code for ctrl-26 delay(1000); Serial.println("SMS Sent Successfully."); } |
Code Explanation:
1 |
#include <SoftwareSerial.h> |
This line includes the SoftwareSerial library, which is used for software-based serial communication.
1 |
SoftwareSerial sim800(9, 10); // TXpin=9 RXpin=10 |
This line defines a SoftwareSerial object named sim800 and specifies that the transmit (TX) pin is 9 and the receive (RX) pin is 10.
1 2 3 |
int relay = 4; int pot = A0; |
These lines define integer variables relay and pot and assign them the pin numbers 4 and A0, respectively.
1 2 3 4 5 6 7 8 9 10 11 |
String text; String callerID; String caller_id; String passcode = "abc123"; String incoming = ""; boolean isReply = false; |
These lines declare several String variables (text, callerID, caller_id, passcode, and incoming) to store information, and a boolean variable (isReply) to track whether a reply is needed.
1 2 3 |
boolean getResponse(String expected_answer, unsigned int timeout=1000, boolean reset=false){ …. } |
This line defines a function named getResponse, which is used to obtain a response from the SIM800L module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
void setup() { Serial.begin(9600); pinMode(relay, OUTPUT); Serial.println("Arduino serial initialize"); sim800.begin(9600); Serial.println("SIM800L software serial initialize"); delay(100); sim800.println("AT"); getResponse("OK", 1000); sim800.println("AT+CMGF=1"); getResponse("OK", 1000); sim800.println("AT+CNMI=1,2,0,0,0"); getResponse("OK", 1000); sim800.println("AT+CMGD=1,4"); delay(1000); sim800.println("AT+CMGDA= \"DEL ALL\""); delay(1000); } |
The setup() function initializes the serial communication, sets the relay pin as an output, and configures the SIM800L module. It sends AT commands to the module to set up SMS functionality.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
void loop() { int potvalue = analogRead(pot); Serial.println(potvalue); while (sim800.available()) { incoming = sim800.readString(); Serial.println(incoming); if (incoming.indexOf("+CMT:") > -1) { String callerID = getCallerID(incoming); caller_id = callerID; String cmd = getMsgContent(incoming); Serial.println("cmd command = "); Serial.print(cmd); String fulldata = getValue(incoming, '%', 0); String password = getValue(incoming, '%', 1); String load = getValue(incoming, '%', 2); String loadstatus = getValue(incoming, '%', 3); Serial.print("value 1: "); Serial.println(password); Serial.print("value 2: "); Serial.println(load); Serial.print("value 3: "); Serial.println(loadstatus); if ((password == passcode) && (load == "relay") && (loadstatus == "on")) { digitalWrite(relay, HIGH); text = ""; text = "relay is turned on"; Reply(text, callerID); } else if ((password == passcode) && (load == "relay") && (loadstatus == "off")) { digitalWrite(relay, LOW); text = ""; text = "relay is turned off"; Reply(text, callerID); } else if ((password == passcode) && (load == "req")) { text = ""; text = potvalue; Reply(text, callerID); } delay(20); } } if (potvalue > 900) { text = ""; text = "Value Exceed"; Reply(text, caller_id); delay(3000); } } |
The loop() function is the main execution loop. It reads incoming SMS messages, processes commands, and sends replies. It also monitors the potentiometer value and sends a warning message if it exceeds a certain threshold.
1 2 3 4 5 |
String getValue(String data, char separator, int index) { // ... } |
The getValue function extracts a specified value from a string based on a separator and index.
1 2 3 4 5 6 7 8 9 10 11 |
String getCallerID(String buff) { // ... } String getMsgContent(String buff) { // ... } |
The getCallerID and getMsgContent functions extract the caller ID and message content from an SMS message.
1 2 3 4 5 |
void Reply(String text, String caller_id) { // ... } |
The Reply function sends a reply SMS to the specified caller ID using the SIM800L module.