Calls and messages using Arduino and GSM modules

Original text: https://circuitdigest.com/microcontroller-projects/call-and-message-using-arduino-sim900-gsm

Calls and messages using Arduino and GSM modules

ARDUINO

Pass by** Saddam **August 17, 2016 39

Sometimes, people find it difficult to use GSM module for its basic functions, such as calling, sending text messages, etc., especially in microcontrollers. Therefore, here we will use Arduino to build a simple mobile phone, in which the GSM module is used to make calls, answer calls, send text messages and read text messages, and the Arduino mobile phone also has a microphone and speaker to make calls through the mobile phone. The project will also be used as the correct interface between the GSM module and Arduino, as well as all the codes required to operate the basic functions of any mobile phone.

Required components:

  • Arduino Uno
  • GSM module SIM900
  • 16x2 LCD
  • 4x4 keyboard
  • Bread board or PCB
  • Connecting jumper
  • Power supply
  • speaker
  • Microphone
  • SIM card

Job description:

In this Arduino mobile phone project, we use Arduino Uno to control the functions of the whole system and connect all components in the system. A 4x4 alphanumeric keyboard is used for shooting all kinds of image input: entering mobile phone number, type message, making phone calls, answering calls, sending text messages, reading text messages and other GSM modules are used to communicate with the network for the purpose of calls and messages. We also connected a MIC and a speaker for voice calls and ringing tones, and a 16x2 LCD for displaying messages, instructions and alarms.

Alphanumeric is a method of entering numbers and letters using the same keyboard. In this approach, we have 4x4 keyboard connected to Arduino, And also wrote the code to accept letters. Please check the code in the code section below.

The work of this project is easy. All functions will be performed by using the alphanumeric keyboard. Check out the complete code and demo video below to understand the process correctly. Here, we will explain all four functions of the following items.

Explain the four functions of the Arduino phone:

1. Call:

To make a call using an Arduino based phone, we have to press the "C" key, and then we need to enter the phone number to make the call. The alphanumeric keypad will be used to enter numbers. After entering the number, we need to press "C" again. Arduino will now use the AT command to handle the process of connecting the call to the input number:

ATDxxxxxxxxxx; <Enter>´╝îWhere input xxxxxxxxx phone number.

2. Answer the phone:

It's very easy to answer the phone. When someone calls your system SIM card number (located in GSM module), your system will display "Incoming..." message on LCD and display the caller's Incoming number. Now, we just need to press the "A" key to participate in this call. When we press "A", Arduino will send the given command to the GSM module:

ATA <input>

3. Send SMS:

When we want to send SMS using Arduino based mobile phones, we need to press' B '. Now, the system will ask for the recipient number, that is, the "Recipient" to whom we want to send a text message. After entering the number, we need to press'D ', and now the LCD asks for information. Now, we need to input information like using an ordinary mobile phone. The method is to use the keyboard, and then after entering the information, we need to press the "D" key to send a text message. Send SMS Arduino to send the given command:

AT + CMGF = 1 <enter>
AT + CMGS =" xxxxxxxxxx" <enter>Of which: xxxxxxxxxx Entered the mobile phone number

And send 26 SMS to GSM.

4. Receiving and reading SMS:

This function is also very simple. In this case, the GSM will receive the SMS and store it in the SIM card. Arduino continuously monitors SMS instructions received through UART. When we see the "new message" symbol (such as envelope: see the video at the end) on the LCD, just press the "D" key to read the SMS. The following is the "SMS Received" indication displayed on the serial port:

+CMTI: "SM" <SMS stored location>    //< SMS storage location >       
+CMTI: "SM",6  Where 6 is message location where it stored in SIM card. //6 where 6 is the message location stored in the SIM card.

When Arduino receives this "received SMS" indication, it will extract the SMS storage location and send a command to GSM to read the received SMS. The "new message symbol" is displayed on the LCD.

AT + CMGR = <SMS storage location> <enter>
AT + CMGR = 6

Now, the GSM sends the stored message to Arduino, and then Arduino extracts the main SMS and displays it on the LCD. After reading this SMS, Arduino clears the "new SMS symbol" from the LCD.

Note: MIC and speaker are not coded.

Check out the complete code and demo video below to understand the process correctly.

Circuit diagram and Description:


The circuit diagram for connecting GSM SIM900 and Arduino is given above. 16x2 LCD pins RS, EN, D4, D5, D6 and D7 are connected to Arduino PIN numbers 14, 15, 16, 17, 18 and 19 respectively. Rx and Tx pins of GSM module are directly connected with pins D3 and D2 of Arduino respectively (the ground of Arduino and GSM must be connected with each other). The row pins R1, R2, R3 and R4 of the 4x4 keyboard are directly connected with the pin numbers 11, 10, 9 and 8 of Arduino, and the Colum pins of the keyboards C1, C2 and C3 are connected with the pin numbers 7, 6, 5 and 4 of Arduino. mic is directly connected to mic + and mic - of GSM module, and speaker is directly connected to sp + and SP - pins of GSM module.

Programming instructions:

For beginners, the programming part of the project is not complicated. In this code, we use the keyboard library * #include < keypad h> To connect a simple keyboard for entering numbers. To enter letters using the same keyboard, we created the function void alfakey() *. This means that we make each key versatile and can enter any character or integer with only 10 keys.

Just like our key 2 (abc2), it will display "a". If you press the key again, it will replace "a" with "b". If you press it again three times, it will display "c" in the same position in the LCD. If we wait for a period of time after pressing the key, the cursor will automatically move to the next position of the LCD. Now we can enter the next character or number. The same procedure is used for other keys.

#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = 
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void alfakey()
{
 int x=0,y=0;
 int num=0;
  while(1)
  {
    lcd.cursor();
    char key=customKeypad.getKey();
    if(key)
    {
       if(key=='1')
       {
         num=0;
         lcd.setCursor(x,y);
         .... .....
         ........ ....

In addition to operating the keyboard, we have created many other functions, such as * invalid call (), calling the phone, message passing function of feature invalid SMS (), invalid lcd_status() is used to display lcd status. Invalid gsm_init() * used to initialize the GSM module, etc. check that all other functions below are related to sending and receiving SMS using GSM module and Arduino. All functions are self-evident and easy to understand.

#include <SoftwareSerial.h>
SoftwareSerial Serial1(2, 3); // RX, TX

#include<LiquidCrystal.h>
LiquidCrystal lcd(14,15,16,17,18,19);

byte back[8] = 
{
  0b00000,
  0b00000,
  0b11111,
  0b10101,
  0b11011,
  0b11111,
  0b00000,
  0b00000
};

String number="";
String msg="";
String instr="";
String str_sms="";
String str1="";
int ring=0;
int i=0,temp=0;
int sms_flag=0;
char sms_num[3];
int rec_read=0;
int temp1=0;

#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char hexaKeys[ROWS][COLS] = 
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

String ch="1,.?!@abc2def3ghi4jkl5mno6pqrs7tuv8wxyz90 ";

void setup() 
{
  Serial1.begin(9600);
  lcd.begin(16,2);
  lcd.createChar(1, back);
  lcd.print("Simple Mobile ");
  lcd.setCursor(0,1);
  lcd.print("System Ready..");
  delay(1000);
  gsm_init();
  lcd.clear();
  lcd.print("System Ready");
  delay(2000);
}

void loop() 
{
  serialEvent();

  if(sms_flag==1)
  {
    lcd.clear();
    lcd.print("New Message");
    int ind=instr.indexOf("+CMTI: \"SM\",");
    ind+=12;
    int k=0;
    lcd.setCursor(0,1);
    lcd.print(ind);
    while(1)
    {
      while(instr[ind]!= 0x0D)
      {
        sms_num[k++]=instr[ind++];
      }
      break;
    }
    ind=0;
    sms_flag=0;
    lcd.setCursor(0,1);
    lcd.print("Read SMS --> D");
    delay(4000);
    instr="";
    rec_read=1;
    temp1=1;
    i=0;
  }

  
  if(ring == 1)
  {
    number="";
    int loc=instr.indexOf("+CLIP: \"");
    if(loc > 0)
    {
      number+=instr.substring(loc+8,loc+13+8);
    }
    lcd.setCursor(0,0);
    lcd.print("Incomming...    ");
    lcd.setCursor(0,1);
    lcd.print(number);
    instr="";
    i=0;
  }

  else
  {
  serialEvent();
  lcd.setCursor(0,0);
  lcd.print("Call --> C      ");
  lcd.setCursor(0,1);
  lcd.print("SMS  --> B   ");
  if(rec_read==1)
  {
    lcd.write(1);
    lcd.print("   ");
  }
  else
  lcd.print("     ");
  }
  
   char key=customKeypad.getKey();
  if(key)
  {
    if(key== 'A')
    {
      if(ring==1)
      {
      Serial1.println("ATA");
      delay(5000);
      }
    }
    else if(key=='C')
    {
      call();
    }

    else if(key=='B')
    {
      sms();
    }

    else if(key == 'D'  && temp1==1)
    {
      rec_read=0;
      lcd.clear();
      lcd.print("Please wait...");
      Serial1.print("AT+CMGR=");
      Serial1.println(sms_num);
      int sms_read_flag=1;
      str_sms="";
      while(sms_read_flag)
      {
        while(Serial1.available()>0)
        {
          char ch=Serial1.read();
          str_sms+=ch;
          if(str_sms.indexOf("OK")>0)
          {
            sms_read_flag=0;
            //break;
          }
        }
      }
      int l1=str_sms.indexOf("\"\r\n");
      int l2=str_sms.indexOf("OK");
      String sms=str_sms.substring(l1+3,l2-4);
      lcd.clear();
      lcd.print(sms);
      delay(5000);
      }
      delay(1000);
    }
}

void call()
{
  number="";
  lcd.clear();
  lcd.print("After Enter No.");
  lcd.setCursor(0,1);
  lcd.print("Press C to Call");
  delay(2000);
  lcd.clear();
  lcd.print("Enter Number:");
  lcd.setCursor(0,1);
  while(1)
  { 
     serialEvent();
    char key=customKeypad.getKey();
    if(key)
    {
      if(key=='C')
      {
        lcd.clear();
        lcd.print("Calling........");
        lcd.setCursor(0,1);
        lcd.print(number);
        Serial1.print("ATD");
        Serial1.print(number);
        Serial1.println(";");
        long stime=millis()+5000;
        int ans=1;
        while(ans==1)
        {           
          while(Serial1.available()>0)
          {
            if(Serial1.find("OK"))
            {
              lcd.clear();
              lcd.print("Ringing....");
              int l=0;
              str1="";
              while(ans==1)
              {
                while(Serial1.available()>0)
                {
                  char ch=Serial1.read();
                  str1+=ch;
                  if(str1.indexOf("NO CARRIER")>0)
                  {
                    lcd.clear();
                    lcd.print("Call End");
                    delay(2000);
                    ans=0;
                    return;
                  }
                 }
                  char key=customKeypad.getKey();
                   if(key == 'D')
                  {
                    lcd.clear();
                    lcd.print("Call End");
                    delay(2000);
                    ans=0;
                    return;
                  }
                   if(ans==0)
                   break;
                }
              }  
            }
        } 
      }
      else
      {
        number+=key;
        lcd.print(key);
      }
    }
  }
}

void sms()
{
  lcd.clear();
  lcd.print("Initilising SMS");
  Serial1.println("AT+CMGF=1");
  delay(400);
  lcd.clear();
  lcd.print("After Enter No.");
  lcd.setCursor(0,1);
  lcd.print("Press D        ");
  delay(2000);
  lcd.clear();
  lcd.print("Enter Rcpt No.:");
  lcd.setCursor(0,1);
  Serial1.print("AT+CMGS=\"");
  while(1)
  {
    serialEvent();
    char key=customKeypad.getKey();
    if(key)
    {
      if(key=='D')
      {
        //number+='"';
        Serial1.println("\"");
        break;
      }
      else
      {
        //number+=key;
        Serial1.print(key);
        lcd.print(key);
      }
    }
  }  
  lcd.clear();
  lcd.print("After Enter MSG ");
  lcd.setCursor(0,1);
  lcd.print("Press D to Send ");
  delay(2000);
  lcd.clear();
  lcd.print("Enter Your Msg");
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  alfakey();
}

void alfakey()
{
 int x=0,y=0;
 int num=0;
  while(1)
  {
    lcd.cursor();
    char key=customKeypad.getKey();
    if(key)
    {
       if(key=='1')
       {
         num=0;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='1')
          {
           num++;
           if(num>5)
           num=0;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
         if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

       else if(key=='2')
       {
         num=6;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='2')
          {
           num++;
           if(num>9)
           num=6;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
          if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

       else if(key=='3')
       {
         num=10;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='3')
          {
           num++;
           if(num>13)
           num=10;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
          if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

       else if(key=='4')
       {
         num=14;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='4')
          {
           num++;
           if(num>17)
           num=14;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
          if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

              else if(key=='5')
       {
         num=18;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='5')
          {
           num++;
           if(num>21)
           num=18;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
          if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

        else if(key=='6')
       {
         num=22;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='6')
          {
           num++;
           if(num>25)
           num=22;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
          if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

       else if(key=='7')
       {
         num=26;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='7')
          {
           num++;
           if(num>30)
           num=26;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
          if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

       else if(key=='8')
       {
         num=31;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='8')
          {
           num++;
           if(num>34)
           num=31;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
          if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

       else if(key=='9')
       {
         num=35;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='9')
          {
           num++;
           if(num>39)
           num=35;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
          if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

        else if(key=='0')
       {
         num=40;
         lcd.setCursor(x,y);
         lcd.print(ch[num]);
         for(int i=0;i<3000;i++)
         { 
          lcd.noCursor();
          char key=customKeypad.getKey();
          if(key=='0')
          {
           num++;
           if(num>41)
           num=40;
           lcd.setCursor(x,y);
           lcd.print(ch[num]);
           i=0;
           delay(200);
          } 
         }
         x++;
          if(x>15)
         {
           x=0;
           y++;
           y%=2;
         }
         msg+=ch[num];
        }

        else if(key=='D')
        {
          lcd.clear();
          lcd.print("Sending SMS....");
         // Serial1.print("AT+CMGS=");
         // Serial1.print(number);
         // delay(2000);
          Serial1.print(msg);
          Serial1.write(26);
          delay(5000);
          lcd.clear();
          lcd.print("SMS Sent to");
          lcd.setCursor(0,1);
          lcd.print(number);
          delay(2000);
          number="";
          break;
        } 
      }
    }
}

void send_data(String message)
{
  Serial1.println(message);
  delay(200);
}

void send_sms()
{
  Serial1.write(26);
}

void lcd_status()
{
  lcd.setCursor(2,1);
  lcd.print("Message Sent");
  delay(2000);
  //lcd.setCursor()
  //lcd.print("")
  //return;
}

void back_button()
{
  //lcd.setCursor(0,15);
}

void ok_button()
{
  lcd.setCursor(0,4);
  lcd.print("OK");
}

void call_button()
{
  lcd.setCursor(0,4);
  lcd.print("CALL");
}

void sms_button()
{
  lcd.setCursor(0,13);
  lcd.print("SMS");
}

void gsm_init()
{
  lcd.clear();
  lcd.print("Finding Module..");
  boolean at_flag=1;
  while(at_flag)
  {
    Serial1.println("AT");
    while(Serial1.available()>0)
    {
      if(Serial1.find("OK"))
      at_flag=0;
    }
    
    delay(1000);
  }

  lcd.clear();
  lcd.print("Module Connected..");
  delay(1000);
  lcd.clear();
  lcd.print("Disabling ECHO");
  boolean echo_flag=1;
  while(echo_flag)
  {
    Serial1.println("ATE1");
    while(Serial1.available()>0)
    {
      if(Serial1.find("OK"))
      echo_flag=0;
    }
    delay(1000);
  }

  lcd.clear();
  lcd.print("Echo OFF");
  delay(1000);
  lcd.clear();
  lcd.print("Finding Network..");
  boolean net_flag=1;
  while(net_flag)
  {
    Serial1.println("AT+CPIN?");
    while(Serial1.available()>0)
    {
      if(Serial1.find("+CPIN: READY"))
      net_flag=0;
    }
    delay(1000);
  }
  lcd.clear();
  lcd.print("Network Found..");
  delay(1000);
  lcd.clear();
}

void serialEvent()
{
  while(Serial1.available())
  {
    char ch=Serial1.read();
    instr+=ch;
    i++;

    if(instr[i-4] == 'R' && instr[i-3] == 'I' && instr[i-2] == 'N' && instr[i-1] == 'G' )
    {
       ring=1;
    }

    if(instr.indexOf("NO CARRIER")>=0)
    {
       ring=0;
       i=0;
    }
    if(instr.indexOf("+CMTI: \"SM\"")>=0)
    {
      sms_flag=1;
    }
  }
}

Added by kendhal on Sun, 20 Feb 2022 02:25:15 +0200