GSM module Interfacing with PIC18F4550
SIM900A GSM module is a communication device which is used to make or receive calls, send or receive SMS, connecting to the internet over GPRS.
Overview of GSM
Global System for Mobile communication (GSM) is a digital cellular system used for mobile devices. It is an international standard for mobile which is widely used for long-distance communication.
There are various GSM modules available in market like SIM900, SIM700, SIM800, SIM808, SIM5320, etc.
SIM900A module allows users to send/receive data over GPRS, send/receive SMS, and make/receive voice calls.
It communicates serially with the devices like microcontroller, PC using AT commands.
To interface the SIM900A module with a cellular network, it requires a SIM card provided by a network operator.
For more information about Sim900A and how to use it, refer to the topic Sim900A GSM/GPRS Module in the sensors and modules section.
The GSM/GPRS module uses USART communication to communicate with the controller or PC terminal.
For information about USART in PIC18F4550 and how to use it, refer to the topic USART in PIC18F4550 in the PIC inside section.

Connection Diagram of SIM900A GSM Module to PIC18F4550

SIM900A GSM Code for PIC18F4550
/*
* SIM900A Interfacing with PIC18F4550
* http://www.electronicwings.com
*/
#include <pic18f4550.h>
#include "Configuration_Header_File.h"
#include "LCD_16x2_8-bit_Header_File.h"
#include "USART_Header_File.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void GSM_Init();
void GSM_Calling(char *);
void GSM_HangCall();
void GSM_Response();
void GSM_Response_Display();
void GSM_Msg_Read(int);
void GSM_Wait_for_Msg();
void GSM_Msg_Display();
void GSM_Msg_Delete(unsigned int);
void GSM_Send_Msg(const char* , const char*);
char buff[160]; /* buffer to store responses and messages */
volatile char status_flag; /* monitor to check for any new message */
volatile int a;
void main(void)
{
a=0;
OSCCON =0x72; /* set internal oscillator Freq = 8 MHz*/
LCD_Init(); /* initialize LCD */
INTCONbits.GIE=1; /* enable Global Interrupt */
INTCONbits.PEIE=1; /* enable Peripheral Interrupt */
PIE1bits.RCIE=1; /* enable Receive Interrupt */
MSdelay(100);
USART_Init(9600); /* initialize USART communication */
GSM_Init(); /* check GSM responses and initialize GSM */
LCD_Clear();
LCD_String("Sending SMS...");
MSdelay(3000);
GSM_Send_Msg("8983923089","TEST"); /*send sms on "mobile no."*/
MSdelay(3000);
LCD_Clear();
while(1)
{
/* waiting for message to receive */
status_flag=0;
if(status_flag)
{
LCD_Clear();
LCD_String_xy(0,0,"New msg arrive");
MSdelay(1000);
LCD_Clear();
GSM_Wait_for_Msg(); /*wait for new message to read */
status_flag = 0;
}
}
}
void GSM_Init()
{
while(1)
{
LCD_Command(0xc0);
USART_SendString("ATE0\r"); /* send AT to check module is ready or not*/
MSdelay(500);
if(strstr(buff,"OK"))
{
GSM_Response(); /* find response and display it on LCD16x2 */
memset(buff,0,160);
break;
}
else
{
LCD_String("Error");
}
}
MSdelay(2000);
LCD_Clear();
LCD_String_xy(0,0,"Text Mode");
LCD_Command(0xc0);
USART_SendString("AT+CMGF=1\r"); /* select message format as text */
GSM_Response();
MSdelay(3000);
LCD_Clear();
LCD_String_xy(0,0,"Mfd name");
LCD_Command(0xc0);
USART_SendString("AT+GMI\r"); /* identify manufacturer */
GSM_Response();
MSdelay(3000);
LCD_Clear();
LCD_String_xy(0,0," Model No.");
LCD_Command(0xc0);
USART_SendString("AT+GMM\r"); /* find model no. */
GSM_Response();
MSdelay(3000);
LCD_Clear();
LCD_String_xy(0,0," IMEI No. ");
LCD_Command(0xc0);
USART_SendString("AT+GSN\r"); /* find IMEI no. of module */
GSM_Response();
MSdelay(3000);
LCD_Clear();
LCD_String_xy(0,0,"Service Provider");
LCD_Command(0xc0);
USART_SendString("AT+CSPN?\r"); /* find service provider name */
GSM_Response();
MSdelay(3000);
}
void GSM_Msg_Delete(unsigned int position)
{
a=0;
char delete_cmd[20];
sprintf(delete_cmd,"AT+CMGD=%d\r",position); /* delete message at specified position */
USART_SendString(delete_cmd);
MSdelay(100);
memset(buff,0,strlen(buff));
}
void GSM_Wait_for_Msg()
{
char i,val[4];
int position;
LCD_Clear();
a=0;
while(1)
{
if(buff[a]==0x0d || buff[a]==0x0a) /*eliminate "\r \n" which is start of string */
{
a++;
}
else
break;
}
if(strstr(buff,"CMTI:")) /* check if any new message received */
{
while(buff[a]!=',')
{
a++;
}
a++;
i=0;
while(buff[a]!=0x0d)
{
val[i]=buff[a];
a++;
i++;
}
position = atoi(val);
if(position>20)
{
LCD_String_xy(0,0,"Msg mem full");
memset(buff,0,strlen(buff));
return;
}
memset(buff,0,strlen(buff));
a=0;
GSM_Msg_Read(position); /* read message which is recently arrived from position */
}
}
void interrupt ISR()
{
if(RCIF)
{
buff[a] = RCREG; /* read received byte from serial buffer */
a++;
if(RCSTAbits.OERR) /* check if any overrun occur due to continuous reception */
{
CREN = 0;
NOP();
CREN=1;
}
status_flag=1; /* use for new message arrival */
}
}
void GSM_Send_Msg(const char *num,const char *sms)
{
int i;
char sms_buffer[35];
a=0;
sprintf(sms_buffer,"AT+CMGS=\"%s\"\r",num);
USART_SendString(sms_buffer); /*send command AT+CMGS="Mobile No."\r */
MSdelay(200);
while(1)
{
if(buff[a]==0x3e) /* wait for '>' character*/
{
a=0;
memset(buff,0,strlen(buff));
USART_SendString(sms); /* send msg to given no. */
USART_TxChar(0x1a); /* send Ctrl+Z then only message will transmit*/
break;
}
a++;
}
MSdelay(300);
a=0;
memset(buff,0,strlen(buff));
memset(sms_buffer,0,strlen(sms_buffer));
}
void GSM_Calling(char *mobile)
{
char call[20];
sprintf(call,"ATD%s;\r",mobile); /* send command ATD8007xxxxxx; for calling*/
USART_SendString(call);
}
void GSM_HangCall()
{
USART_SendString("ATH\r"); /*send command ATH\r to hang call*/
}
void GSM_Response()
{
unsigned int timeout=0;
int CRLF_Found=0;
char CRLF_buff[2];
int Response_Length=0;
while(1)
{
if(timeout>=60000) /*if timeout occur then return */
return;
Response_Length = strlen(buff);
if(Response_Length)
{
MSdelay(1);
timeout++;
if(Response_Length==strlen(buff))
{
for(int i=0;i<Response_Length;i++)
{
memmove(CRLF_buff,CRLF_buff+1,1);
CRLF_buff[1]=buff[i];
if(strncmp(CRLF_buff,"\r\n",2))
{
if(CRLF_Found++==2) /* search for \r\n in string */
{
GSM_Response_Display();
return;
}
}
}
CRLF_Found =0;
}
}
MSdelay(1);
timeout++;
}
}
void GSM_Response_Display()
{
a=0;
while(1)
{
if(buff[a]==0x0d || buff[a]==0x0a)
{
a++;
}
else
break;
}
LCD_String_xy(1,0," ");
LCD_Command(0xc0);
while(buff[a]!=0x0d)
{
LCD_Char(buff[a]);
a++;
if(a==15)
LCD_Command(0x80);
}
a=0;
memset(buff,0,strlen(buff));
}
void GSM_Msg_Read(int position)
{
int i,k;
char flag,read_cmd[10];
i=0;
sprintf(read_cmd,"AT+CMGR=%d\r",position);
USART_SendString(read_cmd);
MSdelay(1000);
GSM_Msg_Display();
}
void GSM_Msg_Display()
{
if(!(strstr(buff,"+CMGR"))) /*check for +CMGR response */
{
LCD_String_xy(0,0,"No message");
}
else
{
a=0;
while(1)
{
if(buff[a]==0x0d || buff[a]==0x0a) /*wait till \r\n not over*/
{
a++;
}
else
break;
}
while(buff[a]!=0x3a) /*wait till string not equal to ':' */
{
a++;
}
do
{
a++;
}while(buff[a-1]!=0x0a);
LCD_Command(0xC0);
int i=0;
while(buff[a]!=0x0d && i<31)
{
LCD_Char(buff[a]);
a++;
i++;
if(i==16) /* if received message is greater than 16(for display message)*/
LCD_Command(0x80); /* resume display of message from 1st line */
}
a=0;
memset(buff,0,strlen(buff));
}
status_flag = 0;
}
Video of SMS Sending using GSM Modem with PIC18F4550
Components Used
Powered by

SIM900A is dual band GSM/GPRS 900/1800MHz module board used to utilize GSM and GPRS services around the globe. It is used to make/receive voice calls, send/receive text messages, connect to and access the internet over GPRS.
170-M031-D



Downloads
Comments35
Join the discussion — share a question or tip.
- TA
The AT commands are being sent with the SMS. How do you have only the text sent?
- RA
do you have this program in proteus
- MA
in this program,what function to receive msg and response to call automatically can you clear with me
- OT
can you link me plz the gsm library for proteus,so i can simulate it with pic18f ?
- NG
hi sir, i got 2 errors when i compile this program, please help me!! 2 errors are :" expected ';' after top level declarator" and "variable has incomplete type 'void'"
- DE
Can you please share the header files
- AL
can anyone please help me in interfacing sim808 with pic16f18877
- AN
Thanks for posting this article. I have one doubt about char buff[160], how and from where buff[ ] get the data. i am understand please help me.. . . Eg- if(strstr(buff,"OK"))???
- AN
Replying to @lokeshc
Thanks Mr. Lokesh your code is successful build in MPlab, but while simulating in proteus i'm getting error msg on LCD .... kindly guide me... Thank you
- NK
Replying to @lokeshc
HI Lokesh, I want to copy and send back to the received message. How to do that. In GSM_Msg_Display() function, Please help me. While I am doing that i am getting AT+CMGR = 0 ERROR.
- EP
make[2]: *** [build/default/production/GSM_Functions.p1] Error 1 make[2]: *** Waiting for unfinished jobs.... make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf make[1]: Entering directory 'D:/SIM900_GSM.X' make -f nbproject/Makefile-default.mk dist/default/production/SIM900_GSM.X.production.hex make[2]: Entering directory 'D:/SIM900_GSM.X' "C:\Program Files (x86)\Microchip\xc8\v2.00\bin\xc8-cc.exe" -mcpu=18F4550 -c -fno-short-double -fno-short-float -memi=wordwrite -fasmfile -maddrqual=ignore -xassembler-with-cpp -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/LCD_16x2.p1 LCD_16x2.c "C:\Program Files (x86)\Microchip\xc8\v2.00\bin\xc8-cc.exe" -mcpu=18F4550 -c -fno-short-double -fno-short-float -memi=wordwrite -fasmfile -maddrqual=ignore -xassembler-with-cpp -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/GSM_Functions.p1 GSM_Functions.c "C:\Program Files (x86)\Microchip\xc8\v2.00\bin\xc8-cc.exe" -mcpu=18F4550 -c -fno-short-double -fno-short-float -memi=wordwrite -fasmfile -maddrqual=ignore -xassembler-with-cpp -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto -o build/default/production/USART_Source_File.p1 USART_Source_File.c GSM_Functions.c:181:6: error: variable has incomplete type 'void' void interrupt ISR() ^ GSM_Functions.c:181:15: error: expected ';' after top level declarator void interrupt ISR() ^ ; 2 errors generated. (908) exit status = 1 nbproject/Makefile-default.mk:138: recipe for target 'build/default/production/GSM_Functions.p1' failed make[2]: Leaving directory 'D:/SIM900_GSM.X' nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed make[1]: Leaving directory 'D:/SIM900_GSM.X' nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2 BUILD FAILED (exit value 2, total time: 3s) How i can fix it??
- AU
i think you have messed with semicolon ?
- AU
Replying to @epasichnikk
i think you should refer example of "how to write interrupt routines in XC8" Link ==> http://microchipdeveloper.com/faq:31
- RI
I am facing the same issue.I tried removing the word "ISR",so it successfully got compiled,but message was not received.
- DA
hi, build successfully. but why getting, ":: advisory: (1234) * Corrupted fast interrupt shadow registers">>why this is corrupted how get rid off. the full compilation message: // LCD_16x2.c:62: warning: (373) implicit signed to unsigned conversion LCD_16x2.c:67: warning: (373) implicit signed to unsigned conversion USART_Source_File.c:14: warning: (373) implicit signed to unsigned conversion gsm.c:142: warning: (373) implicit signed to unsigned conversion gsm.c:142: warning: (373) implicit signed to unsigned conversion gsm.c:152: warning: (373) implicit signed to unsigned conversion gsm.c:161: warning: (373) implicit signed to unsigned conversion gsm.c:163: warning: (373) implicit signed to unsigned conversion gsm.c:185: warning: (373) implicit signed to unsigned conversion gsm.c:209: warning: (373) implicit signed to unsigned conversion gsm.c:252: warning: (373) implicit signed to unsigned conversion gsm.c:257: warning: (373) implicit signed to unsigned conversion gsm.c:283: warning: (373) implicit signed to unsigned conversion gsm.c:283: warning: (373) implicit signed to unsigned conversion gsm.c:292: warning: (373) implicit signed to unsigned conversion gsm.c:294: warning: (373) implicit signed to unsigned conversion gsm.c:328: warning: (373) implicit signed to unsigned conversion gsm.c:328: warning: (373) implicit signed to unsigned conversion gsm.c:335: warning: (373) implicit signed to unsigned conversion gsm.c:342: warning: (373) implicit signed to unsigned conversion gsm.c:346: warning: (373) implicit signed to unsigned conversion gsm.c:348: warning: (373) implicit signed to unsigned conversion "C:\Program Files (x86)\Microchip\xc8\v1.44\bin\xc8.exe" --chip=18F4550 -G -mdist/default/production/gsm.X.production.map --double=24 --float=24 --emi=wordwrite --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" --memorysummary dist/default/production/memoryfile.xml -odist/default/production/gsm.X.production.elf build/default/production/gsm.p1 build/default/production/LCD_16x2.p1 build/default/production/USART_Source_File.p1 Microchip MPLAB XC8 C Compiler (Free Mode) V1.44 Build date: Sep 13 2017 Part Support Version: 1.44 Copyright (C) 2017 Microchip Technology Inc. License type: Node Configuration :: advisory: (1233) Employing 18F4550 errata work-arounds: :: advisory: (1234) * Corrupted fast interrupt shadow registers C:\Program Files (x86)\Microchip\xc8\v1.44\sources\common\doprnt.c:538: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.44\sources\common\doprnt.c:541: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.44\sources\common\doprnt.c:1259: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.44\sources\common\doprnt.c:1305: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.44\sources\common\doprnt.c:1306: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.44\sources\common\doprnt.c:1489: warning: (373) implicit signed to unsigned conversion C:\Program Files (x86)\Microchip\xc8\v1.44\sources\common\doprnt.c:1524: warning: (373) implicit signed to unsigned conversion gsm.c:124: warning: (520) function "_GSM_Msg_Delete" is never called gsm.c:225: warning: (520) function "_GSM_Calling" is never called gsm.c:232: warning: (520) function "_GSM_HangCall" is never called USART_Source_File.c:26: warning: (520) function "_USART_RxChar" is never called Memory Summary: Program space used 1628h ( 5672) of 7800h bytes ( 18.5%) Data space used 10Dh ( 269) of 7FFh bytes ( 13.1%) Configuration bits used 7h ( 7) of 7h words (100.0%) EEPROM space used 0h ( 0) of 100h bytes ( 0.0%) ID Location space used 8h ( 8) of 8h bytes (100.0%) Data stack space used 0h ( 0) of 660h bytes ( 0.0%) You have compiled in FREE mode. Using Omniscient Code Generation that is available in PRO mode, you could have produced up to 60% smaller and 400% faster code. See http://www.microchip.com/MPLABXCcompilers for more information. make[2]: Leaving directory 'F:/0_ELEECTRONICS/3_0_electwing_gsm/gsm/gsm.X' make[1]: Leaving directory 'F:/0_ELEECTRONICS/3_0_electwing_gsm/gsm/gsm.X' BUILD SUCCESSFUL (total time: 5s) Loading code from F:/0_ELEECTRONICS/3_0_electwing_gsm/gsm/gsm.X/dist/default/production/gsm.X.production.hex... Loading completed /
- EP
Hello, can you give me your email, please?
- HE
Replying to @lokeshc
:0: error: (500) undefined symbols: _USART_TxChar(dist/default/production\heramb2.X.production.obj) _LCD_Char(dist/default/production\heramb2.X.production.obj) _LCD_Init(dist/default/production\heramb2.X.production.obj) _LCD_Clear(dist/default/production\heramb2.X.production.obj) _USART_SendString(dist/default/production\heramb2.X.production.obj) _LCD_String(dist/default/production\heramb2.X.production.obj) _MSdelay(dist/default/production\heramb2.X.production.obj) _USART_Init(dist/default/production\heramb2.X.production.obj) _LCD_String_xy(dist/default/production\heramb2.X.production.obj) _LCD_Command(dist/default/production\heramb2.X.production.obj) (908) exit status = 1 nbproject/Makefile-default.mk:131: recipe for target 'dist/default/production/heramb2.X.production.hex' failed make[2]: Leaving directory 'C:/Users/admin/Desktop/Heramb/Project/heramb2.X' nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed make[1]: Leaving directory 'C:/Users/admin/Desktop/Heramb/Project/heramb2.X' nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed BUILD FAILED (exit value 2, total time: 7s) This error i gotted. can you please help me for resolved it.
- AR
pls upload the hex file of program..
- AR
pls upload ur hex file
- SA
I test this code in my project hardware but this code is not working. showing error msg on display.what is problem. gsm not responding.
- LO
Replying to @samadhanshinde020
So just check for gsm module AT commands. Try AT Commands to test gsm module using serial communication with PC having same baud rate as GSM. If it works then test it with provided code by commenting other functions except AT command. And if everything works then integrate it in your application. And if nothing is working then let me know.
- SA
Replying to @lokeshc
i simply first tested gsm module on serial terminal through wire.it works fine.but problem is created in when it's connected to my project hardware.
- AR
when i trying to compile the code bellow massage given on the screen. ---------------------------------------------------------------------- Debug build of project `C:\Documents and Settings\User1\My Documents\HI TECH C\gsm\18f4550_gsm3.mcp' started. Language tool versions: MPASMWIN.exe v5.51, mplink.exe v4.49, mplib.exe v4.49 Preprocessor symbol `__DEBUG' is defined. Thu Dec 28 12:01:02 2017 ---------------------------------------------------------------------- Clean: Deleting intermediary and output files. Clean: Deleted file "C:\Documents and Settings\User1\My Documents\HI TECH C\gsm\18f4550_gsm3.mcs". Clean: Done. Build aborted. The project contains no source files. ---------------------------------------------------------------------- Debug build of project `C:\Documents and Settings\User1\My Documents\HI TECH C\gsm\18f4550_gsm3.mcp' failed. Language tool versions: MPASMWIN.exe v5.51, mplink.exe v4.49, mplib.exe v4.49 Preprocessor symbol `__DEBUG' is defined. Thu Dec 28 12:01:02 2017 ---------------------------------------------------------------------- BUILD FAILED what i will do .................. please give me your opinion............

