Sending SMS Text Message using PIC Microcontroller – Flowcode
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. In this article we are going to learn how to send an SMS text Message from a PIC Microcontroller.
- 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, a liquid level sensor or a moisture sensor. 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.
- SMS Gateway: To send and receive SMS for advertisements.
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.
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 GSMmodule 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: 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 and the E-blocks GSM board from Matrix Technology Solutions are just a few examples 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.
Figure 3: Connecting the SmartGM862 Board to EasyPIC7 V7 Development Board
The E-blocks GSM board allows easy access to the mobile phone GSM/GPRS networks. The on board GSM/GPRS module can be used to make or receive wireless voice calls and is also capable of sending and receiving SMS text messages. The GSM/GPRS E-block comes complete with on board PCB antenna, SIM card socket, 2.5mm audio jacks for MIC and Headphone audio connection and LED to display the network connection status.
Figure 4: E-blocks GSM board
The AT commands are used to control modems which are communicating 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. 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+CMGF: Select SMS message format
- AT+CMGS: Send SMS message
- AT+CMGR: Read SMS message
For more info, you can check your modem datasheet or the AT Commands Reference Guide
In the Interfacing GSM/GPRS Modem with PIC Microcontroller using Flowcode tutorial, we have learnt how to use the Flowcode GSM component to send/receive AT Commands. provides a component for GSM communication. The GSM component is designed to be used with any GSM device that accepts standard GSM AT commands.
Sending an SMS from a microcontroller is very easy, just follow these few steps:
Step 1: Initialize GSM
Call the Initialize GSM macro. This macro Initializes the GSM component ready for use. It is recommended that you place the Initialize macro at the start of a program to ensure the component is initialized before being used.
Step 2: CheckNetworkStatus
Call the CheckNetworkStatus macro. This macro checks to see if the GSM module is correctly registered on the network. This macro could be used in a while loop to wait until the GSM module has correctly registered to the network before sending other commands. This macro will return 1 if the GSM is connected to the network or will returns 0 if the GSM is not connected to the network.
Step 3: Set SMS mode to Text Mode
Send the command to set the SMS mode to Text Mode. 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. The modem should respond with “OK”.
Figure 6 below shows the use of SendCommand macro to send the “AT+CMGF=1” command to the modem.
Step 4: Set the Recipient mobile number and Text Message to be sent
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. At the end of the message, “Cntrl-Z” must be entered to send the message. “Cntrl-Z” ASCII value is 0x1A in Hexadecimal value. After few seconds, the modem will respond with the message ID of the message, indicating that the message has been sent correctly.
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
Flowcode has a macro that simplify this step. instead of using the AT Commands, you can simply use the SendTextMessage macro
This macro is used to send out an SMS text message.
Number – Phone number string to send message to.
Message – Contents of the message you wish to send. This macro will returns 0 if the command was accepted or will returns 255 if the command was rejected for example if the GSM unit is not connected to the network.
Figure 7 below shows the SendTextMessage macro sending a “Hello World!” message to 0123456789 phone number.
- In these 4 steps, it is 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.
- It is also 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
- If more than 1 SMS has to be sent, step 1 to step 3. should be used only once when sending the first message. From the second message, we only send from step 4.
- When we send any command to the GSM, it will respond back. For example, if we send the command “AT”, it will respond back with “OK”. In the code below, we have neglected these responses that is why we have used short delays after every command.
Figure 8 below is the complete code to send an SMS Text Message to the number specified in Cell_Number Variable. The SMS Message is in SMS_Message Variable.
Figure 8: complete code