How to send SMS from GSM modem using arduino

sim800 gsm module, interfacing sim900a with arduino

Interfacing sim800 / sim900A GSM module with arduino

Here describes the interfacing between a SIM 800 GSM / SIM 900A GSM module with an Arduino board via TTL serial communication.

Arduino gsm module pinout

Arduino pin GSM modem pin
RX TX
TX  RX
GND  GND

For a TTL serial connection, connect the pins from the GSM device to Arduino as,  Rx pin to TX pin and TX pin to Rx pin. Or if the software serial is used, connect to the declared Transmitter and Receiver pins. If the Arduino code uses the serial communication as Serial.begin(9600), the TX and RX pins will be default TX and RX pins of the AVR microcontroller which is labelled on the Arduino UNO board. So just plug to TX pin (pin 1) and the RX pin (pin 0).

E.g: – SoftwareSerial mySerial (2, 3); // RX digital pin 2, TX digital pin 3.

gsm module sim900 , GSM module interface , GSM module pin

GSM modems may also have a female RS232 serial port for direct connection to PC. In order to work directly with hyper terminal or similar serial terminals using AT commands.

GSM modem booting

Insert the SIM card into the SIM socket of the module and lock it.

gsm module sim900a , gsm module sim800

The GSM module requires a considerable amount of power to establish a network connection, and for operations. So power from the Arduino board will not be enough for it. So an external power is required for most GSM modules.

The GSM module Commonly has a 12V DC input, which can be connected either via DC socket or through connector pins. Plug a 12V adapter into the Socket or power it by connector pins on the board.

On powered up, the power LED gets ON and the Network Status LED starts to blink. The network LED blink every second and after when the Modem registers the network, the LED will blink in every 3 seconds. The GSM module takes some time, around 60 seconds to establish a connection with mobile networks.

Send sms using gsm module with Arduino

Here we are sending TEXT messages from GSM modem with Arduino UNO using AT commands.

Connect GSM device to Arduino as  Rx and TX pin of the GSM module to TX and Rx pin of the arduino respectively; as explained above in interfacing. Using the below code the GSM module can be directly connected to the Arduino serial pins.

Arduino GSM SMS code 

void setup()
{
 //Set Exact Baud rate of the GSM/GPRS Module.
 Serial.begin(9600);  
 Serial.print("\r");
 delay(1000);                  
 Serial.print("AT+CMGF=1\r");    
 delay(1000);
 /*Replace XXXXXXXXXX to 10 digit mobile number &  ZZ to 2 digit country code*/
 Serial.print("AT+CMGS=\"+ZZXXXXXXXXXX\"\r");    
 delay(1000);
 //The text of the message to be sent.
 Serial.print("HELLO WORLD");   
 delay(1000);
 Serial.write(0x1A);
 delay(1000); 
}
void loop()
{
}

The set of operation code is placed in the void setup (). Each time when the Arduino is powered ON’s an SMS will send, as the void setup function only run once. Or Resetting the Arduino board (press the reset switch on the Arduino board) also sends a message.

In the AT commands,

AT+CMGF=1\r,  put the modem in SMS text format mode.

AT+CMGS=\”+ZZ XXXXXXXXXX\”\r, The SMS will send to the 10 digit  number written as XXXXXXXXXX with 2 digit country code ZZ.

Here the TEXT is “Hello world”, replace it with the TEXT need to be sent.

0x1A, it is the hexadecimal of the decimal character code 26, which is  Substituted (SUB) symbol in ASCII control characters.

Send SMS using push switch

In this device whenever the switch is pressed, a text message (text in the code) will send to the number specified in the code. When the pushbutton is pressed, the Arduino sends the AT commands to the GSM module, for a text SMS; the GSM module works with the “AT commands”.

gsm send sms, arduino gsm

Connect the TX, RX and GND pins from the Arduino to the GSM as labelled in the diagram. Then connect the push switch between the 5V and pin2. Add a pull-down resistor across the pin2 and Ground. The Pin2 is configured as the input pin. When the pin2 reads an active high state, the sketch inside the if condition will be executed. And the SMS sends.

A variable called state is also added to the condition function of the program, it is to prevent multiple sending of SMS on a single switch press. The “if” condition becomes true only when the digitalRead at pin 2 is high and the value of variable state is ‘ 0 ‘. The value of variable state is initialized as ‘ 0 ‘. On pressing the switch, the “if” condition becomes true, that the SMS sends and the state value set to ‘ 1 ‘. Only when the switch is released the state value sets back to ‘ 0 ‘. Only the switch press after the setback of the state to ‘ 0 ‘ makes the if condition true. So a long press will not send multiple SMS, only press after a release sends the next SMS. That is, each consecutive press sends each SMS.

Arduino GSM send SMS code

int state = 0;
const int pin = 2;
void setup()
{
 //Set Exact Baud rate of the GSM/GPRS Module.
 Serial.begin(9600);
}
void loop()
{
 if (digitalRead(pin) == HIGH && state == 0) {
   Serial.print("\r");
   delay(1000);
   Serial.print("AT+CMGF=1\r");
   delay(1000);
   /*Replace XXXXXXXXXX to 10 digit mobile number & ZZ to 2 digit country code*/
   Serial.print("AT+CMGS=\"+ZZXXXXXXXXXX\"\r");
   delay(1000);
   //The text of the message to be sent.
   Serial.print("HELLO WORLD");
   delay(1000);
   Serial.write(0x1A);
   delay(1000);
   state = 1;
 }
 else {
   state = 0;
 }
}
1 Star2 Stars3 Stars4 Stars5 Stars Rate Us!
Loading...

You may also like...

4 Responses

  1. Pradeep Bhikaji Thombre says:

    Sir how to send current location via sms to phone using gsm module and aurdino

  2. tamil says:

    Sir how to store the data from ultra sonic for every 15mins and stored data’s are to be averaged{(15 +15+15+15) ÷4} by 1 hour.finally the one hour data to send the receiver.plz send the coding logic to my mail

    • admin says:

      This is the complete code to display almost all the available data in the GPS. If you need only the latitude and longitude values use the code given in the article.

      #include <SoftwareSerial.h>
      #include <TinyGPS.h>
      TinyGPS gps;
      SoftwareSerial serialgps(4,3);
      int year;
      byte month, day, hour, minute, second, hundredths;
      unsigned long chars;
      unsigned short sentences, failed_checksum;
      void setup()
      {
      Serial.begin(9600);
      serialgps.begin(9600);
      Serial.println("");
      Serial.println("GPS Shield QuickStart Example Sketch v12");
      Serial.println(" ...waiting for lock... ");
      Serial.println("");
      }
      void loop()
      {
      while(serialgps.available()) 
      {
      int c = serialgps.read(); 
      if(gps.encode(c)) 
      {
      float latitude, longitude;
      gps.f_get_position(&latitude, &longitude);
      Serial.print("Lat/Long: "); 
      Serial.print(latitude,6); 
      Serial.print(", "); 
      Serial.println(longitude,6);
      gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
      Serial.print("Date: "); Serial.print(month, DEC); Serial.print("/"); 
      Serial.print(day, DEC); Serial.print("/"); Serial.print(year);
      Serial.print(" Time: "); Serial.print(hour, DEC); Serial.print(":"); 
      Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC); 
      Serial.print("."); Serial.println(hundredths, DEC);
      Serial.print("Altitude (meters): "); Serial.println(gps.f_altitude()); 
      Serial.print("Course (degrees): "); Serial.println(gps.f_course()); 
      Serial.print("Speed(kmph): "); Serial.println(gps.f_speed_kmph());
      Serial.print("Satellites: "); Serial.println(gps.satellites());
      Serial.println();
      gps.stats(&chars, &sentences, &failed_checksum);
      }
      }
      }
      

Leave a Reply

Your email address will not be published. Required fields are marked *