GSM module interfacing with Arduino: Send and receive SMS

Interfacing GSM SIM900A with ArduinoSIM900A is an ultra-compact and reliable wireless module. The SIM900A is a complete Dual-band GSM/GPRS solution in a SMT module which can be embedded in the customer applications. Featuring an industry-standard interface, the SIM900A delivers GSM/GPRS 900/1800MHz performance for voice, SMS, Data, and Fax in a small form factor and with low power consumption. With a tiny configuration of 24mmx24mmx3mm, SIM900A can fit in almost all the space requirements in user applications, especially for slim and compact demand of design.

Features:

  • Dual-Band 900/ 1800 MHz
  • GPRS multi-slot class 10/8GPRS mobile station class B
  • Compliant to GSM phase 2/2+Class 4 (2 W @850/ 900 MHz)
  • Class 1 (1 W @ 1800/1900MHz)
  • Control via AT commands (GSM 07.07 ,07.05 and SIMCOM enhanced AT Commands)
  • Low power consumption: 1.5mA(sleep mode)’
  • Operation temperature: -40°C to +85 °C
  • Status indicator (D5): It will flash continuously whenever the call arrives otherwise it is left ON.
  • Network LED (D6): This led will blink every second which indicates that the GSM module is not connected to the mobile network. Once the connection is established successfully, the LED will blink continuously every 3 seconds.

Booting the GSM Module:

  1. Insert the SIM card to GSM module and lock it.
  2. Connect the adapter to GSM module and turn it ON!
  3. Now wait for some time (say 1 minute) and see the blinking rate of ‘status LED’  or ‘network LED’ (GSM module will take some time to establish connection with mobile network)
  4. Once the connection is established successfully, the status/network LED will blink continuously every 3 seconds. You may try making a call to the mobile number of the sim card inside GSM module. If you hear a ring back, the gsm module has successfully established network connection.

Okay! Now let’s see how to connect a gsm module to Arduino!

Connecting GSM module with ArduinoGSM module interfacing with Arduino

There are two ways of connecting GSM module to Arduino. In any case, the communication between Arduino and GSM module is serial. So we are supposed to use serial pins of Arduino (Rx and Tx). So if you are going with this method, you may connect the Tx pin of GSM module to Rx pin of Arduino and Rx pin of GSM module to Tx pin of Arduino. You read it right? GSM Tx –> Arduino Rx and GSM Rx –> Arduino Tx. Now connect the ground pin of Arduino to ground pin of gsm module! So that’s all! You made 3 connections and the wiring is over! Now you can load different programs to communicate with gsm module and make it work.

Note:- The problem with this connection is that, while programming Arduino uses serial ports to load program from the Arduino IDE. If these pins are used in wiring, the program will not be loaded successfully to Arduino. So you have to disconnect wiring in Rx and Tx each time you burn the program to Arduino. Once the program is loaded successfully, you can reconnect these pins and have the system working!

To avoid this difficulty, I am using an alternate method in which two digital pins of Arduino are used for serial communication. We need to select two PWM enabled pins of Arduino for this method. So I choose pins 9 and 10 (which are PWM enabled pins). This method is made possible with the SoftwareSerial Library of Arduino. Software Serial is a library of Arduino which enables serial data communication through other digital pins of Arduino. The library replicates hardware functions and handles the task of serial communication.

Code for  gsm module interfacing with Arduino

#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);

void setup()

{

  mySerial.begin(9600);   // Setting the baud rate of GSM Module 

  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)

  delay(100);

}

void loop()

{

  if (Serial.available()>0)

   switch(Serial.read())

  {

    case 's':

      SendMessage();

      break;

    case 'd':

      DialCall();

      break;

  }

 if (mySerial.available()>0)

   Serial.write(mySerial.read());

}

 void SendMessage()


{

  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode

  delay(1000);  // Delay of 1000 milli seconds or 1 second

  mySerial.println("AT+CMGS=\"+xxxxxxxxxxx\"\r"); // Replace x with mobile number

  delay(1000);

  mySerial.println("I am SMS from GSM Module");// The SMS text you want to send

  delay(100);

  mySerial.println((char)26);// ASCII code of CTRL+Z

  delay(1000);

}

 /*void RecieveMessage()

{

  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS

  delay(1000);

}

*/

  void DialCall()

 {

  mySerial.println("ATD+xxxxxxxxxxxx;"); // ATDxxxxxxxxxx; -- watch out here for semicolon at the end!!

  delay(100);

 }

Code explanation for gsm module interfacing with Arduino

We begin by including SoftwareSerial library into the program.  In the next line, we create a constructor of SoftwareSerial with name mySerial and we pass the digital pin numbers as parameters. The actual format is like SoftwareSerial mySerial (Rx, Tx);

So in our code, pin number 9 will act as Rx of Arduino and 10 will act as Tx of Arduino.  Let’s get to the configuration part of program inside setup. The first task is to set baud rates of SoftwareSerial library to communicate with GSM module. We achieve this by invoking mySerial.begin function. Our second task is to set the baud rate of Arduino IDE’s Serial Monitor. We do this by invoking Serial.begin function. Both should be set at the same baud rate and we use 9600 bits/second here in our tutorial.  Configuration part is over with setting baud rates and it’s good to give a small delay of 100 milli seconds. Now let’s get to the actual program inside loop (). To make things simpler, I have developed a user input based program. The program seeks user input via serial monitor of Arduino. If the input is‘s’ the program will invoke function to send an sms from GSM module. If the user input is ‘r’, the program will invoke the function to receive a live SMS from GSM module and display it on serial monitor of Arduino. The whole program is as simple as that!

Serial.available() – checks for any data coming through serial port of arduino. The function returns the number of bytes available to read from serial buffer. If there is no data available, it returns a -1 (value less than zero).

Serial.read() – Reads all the data available on serial buffer (or incoming serial data if put otherwise). It returns the first byte of incoming serial data.

mySerial.available() – checks for any data coming from GSM module through the SoftwareSerial pins 9 and 10. It returns the number of bytes available to read from software serial port. It returns a -1 if no data is available to read.

mySerial.read() – Reads the incoming data through software serial port.

Serial.write() – Prints data to serial monitor of Arduino. So the function Serial.write(mySerial.read()) – prints the data collected from software serial port to serial monitor of Arduino.

SendMessage() – is the function we created in our Arduino sketch to send an SMS. To send an SMS, we should set our GSM module to Text mode first. This is achieved by sending an AT Command “AT+CMGF=1” We send this command by writing this to SoftwareSerial port. To achieve this we use the mySerial.println() function. mySerial.println writes data to software serial port (the Tx pin of our Software Serial – that is pin 10) and this will be captured by GSM module (through its Rx pin). After setting the GSM module to Text mode, we should the the mobile number to which we shall send the SMS. This is achieved with AT command “AT+CMGS=\”+91xxxxxxxxxx\”\r” – where you may replace all x with the mobile number.

In next step, we should send the actual content of SMS. The end of SMS content is identified with CTRL+Z symbol. The ASCII value of this CTRL+Z is 26. So we send a char(26) to GSM module using the line mySerial.println((char)26); Each and every AT command may be followed by 1 second delay. We must give some time for GSM module to respond properly. Once these commands are send to GSM module, you shall receive an SMS in the set mobile number.

RecieveMessage() – is the function to receive an SMS (a live SMS). The AT command to receive a live SMS is “AT+CNMI=2, 2, 0, 0, 0” – we just need to send this command to GSM module and apply a 1 second delay. Once you send this command, try sending an SMS to the SIM card number put inside GSM module. You will see the SMS you had sent displayed on your Arduino serial monitor.

20 thoughts on “GSM module interfacing with Arduino: Send and receive SMS”

  1. is that possible to interface GSM module with micro controller ?
    i need it to interface with micro controller my simulation to have multi purpose?

    Reply
  2. Hello sir.
    Is it possible to send a variable value as text message??… for example, if i want to know the current of a system, then is it possible to send the value of current as text message??

    Reply
  3. sir, i’m beginner in arduino..i want ask a question..my question is .. what phone numbers should i enter in this code, if i want to run this code in Proteus ???

    Reply
    • thank you very much as much as possible please connect to me arduino interface with motor and also sensor like moisture sensor for agricultural project

      Reply
  4. I need to know that if we are making call through GSM and the person who receive call then can that person listen the voice from GSM on live call? kindly suggest me for my project please.

    Reply
  5. Hi,

    May i use this combination to switch ON and OFF Borewell Motor remotely? can i get feedback once the borewell is turned On through a water flow switch or pressure switch ( Digital Input)? And How?

    Reply
  6. D6 network light 💡on always why? What is the meaning of this?
    Not sent massage/call….network light ON. The whole process not working please help..

    Reply
  7. When I am using GSM.h library and its methods instead of “AT” commands, a problem raised at the serial communication when we use SoftwareSerial.h . So , I used AltSoftSerial.h . I’m not getting error but I’m getting “%13%. I don’t know what it mean. Can some one please help me.
    Thanks in advance.

    Reply
  8. all wiring is okey.and i supply the power from arduino instead of external source. both light of sgm module is on. the network light is blinking with out any delay ..and i cannt send sms from module.what should i do..please sir sugest any solution

    Reply

Leave a Comment