Interfacing GSM/GPRS Modem with PIC Microcontroller – MikroC

Interfacing GSM Modem with PIC Microcontroller

Interfacing GSM/GPRS Modem with PIC Microcontroller – MikroC

Controlling and Monitoring devices with a Cellphone

                                   Figure 1: Controlling and Monitoring devices with a Cellphone

A GSM modem is a wireless modem that works with a GSM wireless network. GSM stands for Global System for Mobile communications, this architecture is used for mobile communication in most of the countries in the world. 

A wireless modem acts basically like the traditional dial-up modem, the main difference is that a dial-up modem sends and receives data through a fixed telephone line while a wireless modem sends and receives data through radio waves. Besides the dial-up connection, GSM modem can also be used for sending and receiving SMS which is also one of the key features of GSM modem.

Some modems are GSM/GPRS modems, these modems additionally support the GPRS technology for data transmission. GPRS stands for General Packet Radio Service. It is a packet-switched technology that is an extension of GSM. A key advantage of GPRS over GSM is that GPRS has a higher data transmission speed.

The operation of a GSM modem requires a SIM (Subscriber Identity Module) card just like mobile phones to identify a subscriber to the cellular network and to store the subscriber information. Also they have IMEI (International Mobile Equipment Identity) number similar to mobile phones for their identification. 


  • Remote System Monitoring: In a remote system monitoring application, a Microcontroller for example could be used to constantly monitor the status of a remote sensors, let say temperature or moisture sensors. If a certain condition is reached, the program will send an SMS to notify the situation, if these sensors are installed in a farm for example, the farmer could be notified of any situation happening in the farm about heat, moisture, drought, etc.
  • Home Alarm System: When a motion sensor detects a movement of an intruder, an SMS will be sent to a predefined number which could be you or your security reaction company and a siren or any other sound device could be triggered in the process as well.
  • Remote Controlling System: A mobile phone can be used to Send an SMS to switch ON/OFF remote devices. You could control your entire home devices, switching ON/OFF lights, fans, pool motor, garage motor and so on with just a simple SMS from a distance.
  • Security Access Control: A security access can be implemented at apartments or housing complexes. A security guard could inform by intercom a resident if he/she has a visitor at the main gate. with a replied SMS, the resident could open remotely the gate for his/her visitor.
  • Vehicle tracking: A GSM/GPRS module together with a GPS module can be used to create a real time vehicle tracking device. An SMS could also be sent to notify that the car has entered a restricted area, notify about the level of remaining fuel in the car etc. If the vehicle is stolen not only the system can indicate the location of the vehicle but an SMS could also be sent to switch OFF the engine.
  • SMS Gateway: To send and receive SMS for advertisements.
  • Pre-paid Electricity: The customer can recharge pre-paid electricity by sending an SMS with a recharge coupon to a pre-paid electricity recharge server.

GSM Connection

It is very easy to interface a GSM Modem to a PIC Microcontroller as most GSM modems have a serial interface. The USART serial input pin RX and TX of the microcontroller are connected to the TXD and RXD pins of the GSM Modem. Some GSM modems have PCMCIA Type II or USB interfaces. Figure 2 below shows a block diagram of a GSM module connected to USART module of a PIC Microcontroller.

GSM module connected to a PIC Microcontroller

Figure 2: GSM module connected to a PIC Microcontroller

NB: Depending from the type of serial port on the Microcontroller hardware, a level translator circuit may be needed to make the system work. If the Microcontroller USART voltage level is 5V as in most of the cases, most the GSM/GPRS modems USART voltage level is about 2.8V – 3V, you need a voltage level translator circuit. A simple diodes/resistors network could do the job as shown on figure 3 below.

Three diode in series are used to drop down voltage of TX pin of microcontroller to to 2.9 volt (each diode drops 0.7V) which is in acceptable range for RXD pin of gsm module. Similarly a diode, a resistor and 5 volt source is used to increase voltage of TXD pin of gsm module to 5 volt which is logic high for RX pin of pic microcontroller.

Figure 3: A simple diodes/resistors voltage level translator circuit

To learn more on Serial Communication read the article:

PIC Microcontroller Communication with RS232 Bus

There are GSM board on the market  that one can use to quickly interface to a PIC. The SmartGM862 Board from Mikroelekronika is one example of many boards. The SmartGM862 is a full-featured development tool for Telit GM862-QUAD GSM/GPRS module or the GM862-GPS version. It features GM862 module connector, voltage regulator, antenna holders, speaker and microphone screw terminals and more. DIP switch is provided for configuring UART communication lines with the target microcontroller. It can be connected to development boards via IDC10 connector.

Connecting the SmartGM862 Board to EasyPIC7 V7 Development Board

Figure 4: Connecting the SmartGM862 Board to EasyPIC7 V7 Development Board

Buy GSM Modem, PIC, Development board, Accessories from our Online Store

GSM Commands

The AT commands are used to control modems which are communicated through serial communication with a microcontroller or a PC. AT is the abbreviation for ATtention. These commands come from Hayes commands that were used by the Hayes smart modems in late 1970s. The Hayes commands started with AT to indicate the attention from the MODEM followed by a number of characters specifying the command tail.

AT commands with a GSM/GPRS MODEM or mobile phone can be used to check the modem settings, to change the modem settings, to issues commands like to send an SMS, read an SMS and so on.

Below is a short list of commonly used general purpose AT commands, you can use them to test your modem. The command should be followed by a carriage return :

  • ATI: Modem product information
  • ATE0: Disable echo so that characters typed by the user are not echoed back by the modem.
  • ATE1: Enable the echo mode
  • AT+GMI: Display the Modem Manufacturer’s ID
  • AT+GMM: Display Modem model number

If you would to send an SMS, here are a few AT commands that you can use:

  • AT+CMGF: Select SMS message format
  • AT+CMGS: Send SMS message
  • AT+CMGR: Read SMS message
  • AT+CSCA: Set the SMS service center number
  • AT+CPIN: To enter the SIM PIN number
  • AT+CMGD: Delete SMS message
  • AT+CMGDA: Delete all SMS messages

If you would like to make a voice call, here are a few AT commands that you can use:

  • AT+CHFA = 1: This command is used to select Audio channel (channel 1).
  • AT+CMIC = 1: This command is used to select gain of Audio. Its value can be between 0-15.
  • ATD0711111111: This command is used to dial the required mobile number. In this example we are dialing the number: 0711111111
  • ATH: This command is used to terminate call.
  • ATA: Answering the phone call
  • AT+CCVM: This command displays the voicemail number stored on the SIM card

For more info, you can check your modem datasheet or the AT Commands Reference Guide

Sending an SMS

Sending an SMS from a microcontroller is very easy, just follow these few steps, the PIC18F45K22 is used in these examples but any other PIC with a serial module and enough memory could also be used successfully.

Step 1: AT

The first command is to send “AT” then enter if you are using a computer or from a microcontroller, we should send the ASCII value for ENTER which is 0xD in Hex. The purpose of this command is to test the microcontroller and wait for a response which should be: OK. This command can be used to negotiate the baud rate, so that the GSM module can set itself to the same baud rate of the microcontroller.

To negotiate baud rate, send AT in a while(1) loop until OK is received, then you can proceed with other commands. To make things simpler is to send AT then wait for a short delay, then proceed as it expected that OK should have been received in that short delay.

Step 2: AT0

Disable echo so that characters sent by the microcontroller are not echoed back by the modem

Step 3: AT+CMGF=1

There are two modes of sending and receiving SMS messages with AT Commands: The Text mode and the PDU (Protocol Description Unit) mode. In PDU mode, the SMS is sent in a series of hexadecimal string of characters, at the receiving end these hexadecimal string of characters have to be decoded to extract the message in a readable format. In Text mode, the SMS is sent like the normal SMS we are used to with our cellphones. The text message can consist of alphanumeric characters with up to 160 characters long with 7-bit coding and up to 140 characters in 8-bit coding.

To set the mode to Text, the AT command is: AT+CMGF=1 and to set the mode to PDU the command is:AT+CMGF=0

Step 4: AT+CMGS=”0123456789″ + the Text message

Set the recipient mobile phone number and the text message to be sent. After the AT+CMGS=”0123456789″command (Replace the 0123456789 with a correct recipient cellphone number), the modem will respond with the character “>“, after receiving this “>” then the text message can be entered, to make things simpler, we are going to used again a short delay instead of waiting for the “>”.  At the end of the message, “Cntrl-Z” must be entered to send the message. “Cntrl-Z” ASCII value is 0x1A in Hex. Let us send “Hello World” SMS to 0123456789 number.

That all !!!

In the above example, it is assumed that the SMS service center number has already been programmed to the SIM Card (This is normally done with your network provider when a new SIM card is purchased). The AT Command to set the SMS service center number is: AT+CSCA

It is also assumed that the SIM card has no security PIN number associated with it, if the SIM has a PIN number, use the AT command AT+CPIN to enter the PIN number.

Share this post

You've just added this product to the cart:

Select your currency
ZAR South African rand
EUR Euro