The connection diagram for Interfacing GSM module with PIC microcontroller is shown below.
We have simply interfaced the Tx and Rx pins of the GSM module with the Rx and Tx pins of the PIC MCU PIC16F877A respectively. This will establish a Serial connection between both. Also, so do not forget to common ground both the GSM and PIC module. We have also used a LCD display to know the status of our GSM module. Once the connections are done your hardware will look like something below.
The PIC Perf board was made for our PIC tutorial series, in which we learnt how to use PIC microcontroller. You might want to go back to those PIC Microcontroller tutorials using MPLABX and XC8 if you do not know how to burn a program using Pickit 3, since I will be skipping all those basic information.
The complete program for this project can be found at the bottom of this tutorial. Here I will explain some important functions and pieces of code. This program also has a LCD codes which were from Interfacing LCD with PIC Microcontroller, you can visit that tutorial if you are curious to know how LCD can be used with PIC microcontroller.
As said earlier, we are going to communicate between PIC and GSM using AT commands through the Serial mode of communication. So, first we have to initialize the USART communication module in our PIC microcontroller by using the Initialize_SIM900(); function. Inside this function we declare the Tx and RX pins and initialize Asynchronous reception and transmission at 9600 baud rate and 8-bit mode.
Now we need to read and write information from/to our GSM module. For this we use the functions _SIM900_putch(), _SIM900_getch(), _SIM900_send_string(), _SIM900_print(). These functions use the Transmit and receive buffer register such as TXREG and RCREG to read or write data serially.
The above functions are universal and need not be changed for any applications. They were explained only to give a rough intro. You can dive deep into them if you want through understanding.
}while (!SIM900_isStarted()); //wait till the GSM to send back "OK"
If the module is not detected or if there is any connection problem then the LCD will show "Module not found", else it will show Module is detected and proceed to next step where, we check if the SIM card can be detected by the below line of code.
}while (!SIM900_isReady()); //wait till the GSM to send back "+CPIN: READY"
If a SIM card if found we will get SIM detected displayed on the LCD. Then, we can try placing a call by using the command "ATDmobilenumber;". Here as an example I have used my number as ATD93643159XX;. You have to replace your respective mobile number there.
}while (_SIM900_waitResponse() != SIM900_OK); //wait till the ESP send back "OK"
Once the call is placed the LCD will display Call Placed and you should receive an incoming call to that specified number.
When the GSM module detects a incoming call it will display Incoming call on the second line of the LCD module. The function _SIM900_waitResponse() will check for incoming data from the GSM module. When it receives SIM900_RING, which is equivalent to "RING" due to the waitResponce(), we will display the status "Incoming call".
You can create your own functions like this to perform almost all types of activates using GSM module. If you want to have things hardcoded, you can simply use the __SIM900_print() function to send any AT command like this below.
Remember that all you command should be followed with "\r\n" to indicate that the command is terminating.
Once you have understood how the program works you can try simulating and make changes to fit your needs. Simulation will save you a lot of time. The simulation is done using Proteus and it looks like below.
As you can see we have used virtual terminal option in Proteus to check if the Program is responding as expected. We can feed in the values through the pop-up dialog box. For example as soon as we hit run, a black dialog box like above will appear and display AT, this means that it has send the GSM module AT, now we can reply to PIC by typing in the box as "OK" and hit enter and the PIC will respond to it. Similarly we can try for all AT commands.
Once you understand how the code and hardware works, simply upload the below program to PIC and power on the module. Your LCD should display "Module Detected", "SIM detected" and "call Placed" if everything is working fine. Once you see "Call placed" you will get a incoming call to the number specified in the program.
You can also try calling to the number present in the GSM module and your LCD will display "Incoming call" to indicate the SIM is being called.
The complete working of the project is shown in the video below. Hope you understood the project and enjoyed doing it. If you have any problem in getting things work, post your question on the comment section or on our Forums and I will be happy to help you out.
i am getting 100 errors and 8 warnings for the given program while compiling.please help me.i am not able to figure out the problem.i have included all libraries also.
Help can be provided only if you show your error messages, paste them here or make a separate thread on the forum.
Realy interesting sir , I which to join an MQ2 ' gas sensor ' to this projet ... please for the code that will permit an impulse from 'MQ2' to the 'GSM ' passing via PIC 16F877A .
I am doing this project outside Asia. Will Sim900a work outside India?
What will be the SPBRG value for 8MHz crystal? do i have to brgh = 1? I have displayed reception on lcd.. but it shows blank. But proteus simulation runs ok!
"inline unsigned char " any Buddy explain me which type of "return type" it is?
In this gsm code, if i add ADC function then ADC not work continuously, only work first ONE time.
I´m running your code and check it out in Proteus, i don´t have any problem with compilation but, at the time I run in Proteus it doesn´t work, i show you an image, i hope you can help me soon.
Now, my question is, how should i work on this project for sending a message instead a call? Please help me!!!!!