Post Page Advertisement [Top]

tutorial

Interfacing SIM900A GSM Modem With Arduino

SIM900A is a GSM module that functions like a phone. It can send a message, call a phone number and use GPRS to send data.


SIM900A blockIMG_0078 copy

Description:
SIM900A Modem is built with Dual-Band GSM/GPRS based SIM900A modem from SIMCOM. It works on frequencies 900/ 1800 MHz. SIM900A can search these two bands automatically. The frequency bands can also be set by AT Commands. The baud rate is configurable from 1200-115200 through AT command. The GSM/GPRS Modem is having internal TCP/IP stack to enable you to connect with internet via GPRS. SIM900A is an ultra compact and reliable wireless module. This is a complete GSM/GPRS module in an SMT type and designed with a very powerful single-chip processor integrating AMR926EJ-S core, allowing you to benefit from small dimensions and cost-effective solutions.
Specification:

    1.   Dual-Band 900/ 1800 MHz
      2.  GPRS multi-slot class 10/8GPRS mobile station class B
      3.  Compliant to GSM phase 2/2+
      4.  Dimensions: 24*24*3 mm
      5.  Weight: 3.4g
      6.  Control via AT commands (GSM 07.07, 07.05 and SIMCOM enhanced AT Commands)
      7.  Supply voltage range: 12V
      8.  Low power consumption: 1.5mA (sleep mode)
      9.  Operation temperature: -40°C to +85 °
Booting Up SIM900A:
1.     Insert your SIM card to GSM module and lock it.
2.     power up your gsm by connecting it to 12v adapter.
3.     Connect the Antenna.
4.       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 a connection with the mobile network.
5.      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 the GSM module. If you hear a ring back, the gsm module has successfully established a   network connection.

Basic AT Command:
1. To change SMS sending mode: AT+CMGF=1
     mySerial.println("AT+CMGF=1");

2. To read SMS in text mode : AT+CNMI=2,2,0,0,0
     mySerial.println("AT+CNMI=2,2,0,0,0");

3. To make a call: ATD+60XXXXXXXXX; //replace X with the number you want to call, change +60 to your country code
     mySerial.println("ATD+60XXXXXXXXX;");

4. To disconnect/hangup call: ATH
     mySerial.println("ATH");

5. To redial: ATDL
     mySerial.println("ATDL");

6. To receive a phone call: ATA
     mySerial.println("ATA");

Connecting the GSM Module to Arduino:
There are two ways of connecting the 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 the GSM module to Rx pin of Arduino and Rx pin of the GSM module to Tx pin of Arduino, ie. 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 the G module and make it work.


Code:

 #include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);

void setup()
{
  mySerial.begin(57600);   // Setting the baud rate of GSM Module  
  Serial.begin(57600);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);
}

void loop()
{
  if (Serial.available()>0)
   switch(Serial.read())
  {
    case 's':
      SendMessage();
      break;
    case 'r':
      RecieveMessage();
      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=\"+91xxxxxxxxxx\"\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 receive a live SMS
  delay(1000);
   mySerial.println("enter ur message");
 }

Output:

When You enter “s” in the serial monitor you will get the message to the phone number You have entered in the program and the message You send to that phone number will be displayed in Serial monitor.

The Program Explanation:

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 the program inside setup. The first task is to set baud rates of SoftwareSerial library to communicate with the 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 57600 bits/second here. Configuration part is over with setting baud rates and it’s good to give a small delay of 100 milliseconds.
Now let’s get to the actual program inside the loop(). To make things simpler, I have developed a user input based program. The program seeks user input via the serial monitor of Arduino. If the input is ‘s’ the program will invoke a function to send an SMS from the 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 the serial port of Arduino. The function returns the number of bytes available to read from the serial buffer. If there is no data available, it returns a -1 (value less than zero).

Serial.read() – Reads all the data available on the serial buffer (or incoming serial data if put otherwise). 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. Returns the number of bytes available to read from software serial port. 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.

Le;ts get the functions SendMessage()  and RecieveMessage()

These are the functions in which we actually send commands to the GSM module from Arduino. These commands to communicate with the GSM module are called AT Commands. There are different commands to perform different tasks using the GSM module.

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 the GSM module (through its Rx pin). After setting the GSM module to Text mode, we should enter 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 the 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 a 1-second delay. We must give some time for the GSM module to respond properly. Once these commands are sent to the 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 the GSM module. You will see the SMS you had sent displayed on your Arduino serial monitor.


No comments:

Post a Comment

Bottom Ad [Post Page]