Tutorial: Arduino and GSM Cellular – Part Two
This is the revised, 2012 edition of chapter twenty-seven of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.
Welcome back fellow arduidans!
Assumed understanding for this article is found in part one. If you have not already done so, please read and understand it.
Today we are going to harness the awesome power of the telephone network to control an Arduino board via a SM5100B Cellular Shield:
Are you using an Arduino Mega board?
Mega users – things will be slightly different for you. Please make sure the TX and RX pins of your GSM shield DO NOT plug into your Mega. Furthermore, run a jumper wire from GSM shield pin D2 to Mega pin 19, and a jumper from GSM shield pin D3 to Mega pin 18, as shown below:
Finally, the example sketches will be different. Mega will not use the NewSoftSerial library, instead we use the Serial1 port. Please use the following Mega-equivalent sketches for this article: Example 27.1 and 27.2.
Reach out and control something
First we will discuss how to make something happen by a simple telephone call. And the best thing is that we don’t need the the GSM module to answer the telephone call (thereby saving money) – just let the module ring a few times. How is this possible? Very easily. Recall example 26.1 – we monitored the activity of the GSM module by using our terminal software. In this case what we need to do is have our Arduino examine the text coming in from the serial output of the GSM module, and look for a particular string of characters.
When we telephone the GSM module from another number, the module returns the text as shown in the image below (click to enlarge):

We want to look for the text “RING”, as (obviously) this means that the GSM shield has recognised the ring signal from the exchange. Therefore need our Arduino to count the number of rings for the particular telephone call being made to the module. (Memories – Many years ago we would use public telephones to send messages to each other. For example, after arriving at a foreign destination we would call home and let the phone ring five times then hang up – which meant we had arrived safely). Finally, once the GSM shield has received a set number of rings, we want the Arduino to do something.
From a software perspective, we need to examine each character as it is returned from the GSM shield. Once an “R” is received, we examine the next character. If it is an “I”, we examine the next character. If it is an “N”, we examine the next character. If it is a “G”, we know an inbound call is being attempted, and one ring has occurred. We can set the number of rings to wait until out desired function is called. In the following example, when the shield is called, it will call the function doSomething() after three rings.
The function doSomething() controls two LEDs, one red, one green. Every time the GSM module is called for 3 rings, the Arduino alternately turns on or off the LEDs. Using this sketch as an example, you now have the ability to turn basically anything on or off, or call your own particular function. Another example would be to return some type of data, for example you could dial in and have the Arduino send you a text message containing temperature data.
Example 27.1 (Mega version)
/* Example 27.1 Turn something on or off by telephoning the GSM shield for a set number of rings
tronixstuff.com/tutorials > chapter 27 CC by-sa-nc
NOT for Arduino Mega */
#include <NewSoftSerial.h> //Include the NewSoftSerial library to send serial commands to the cellular module
char inchar; // Will hold the incoming character from the Serial Port
NewSoftSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin
int numring=0; // used to count the number of 'rings' received by the GSM module
int comring=3; // this is the number of rings to wait for until calling the function doSomething();
int onoff=0; // used to track LED status - 0 = off, 1 = on
void setup()
{
pinMode(12, OUTPUT);
pinMode(13, OUTPUT); // LEDs - off = red, on = green
digitalWrite(12, HIGH);
digitalWrite(13, LOW); //Initialize serial port for communication.
cell.begin(9600);
} void doSomething()
{
if (onoff==0)
{
onoff=1;
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
} else
if (onoff==1)
{
onoff=0;
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
}
} void loop()
{ //If a character comes in from the cellular module...
if(cell.available() >0)
{
inchar=cell.read();
if (inchar=='R')
{
delay(10);
inchar=cell.read();
if (inchar=='I')
{
delay(10);
inchar=cell.read();
if (inchar=='N')
{
delay(10);
inchar=cell.read();
if (inchar=='G')
{
delay(10);
// So the phone (our GSM shield) has 'rung' once, i.e. if it were a real phone
// it would have sounded 'ring-ring'
numring++;
if (numring==comring)
{
numring=0; // reset ring counter
doSomething();
}
}
}
}
}
}
}
And now for a quick video demonstration. The first call is made, and the LEDs go from red (off) to green (on). A second call is made, and the LEDs go from green (on) to red (off). Although this may seem like an over-simplified example, with your existing Ardiuno knowledge you now have the ability to run any function by calling your GSM shield.
Now although turning one thing on or off is convenient, how can we send more control information to our GSM module? For example, control four or more digital outputs at once? These sorts of commands can be achieved by the reception and analysis of text messages.
Doing so is similar to the method we used in example 27.1. Once again, we will analyse the characters being sent from the GSM module via its serial out. However, there are two AT commands we need to send to the GSM module before we can receive SMSs, and one afterwards. The first one you already know:
AT+CMGF=1
Which sets the SMS mode to text. The second command is:
AT+CNMI=3,3,0,0
This command tells the GSM module to immediately send any new SMS data to the serial out. An example of this is shown in the terminal capture below (click to enlarge):
Two text messages have been received since the module was turned on. You can see how the data is laid out. The blacked out number is the sender of the SMS. The number +61418706700 is the number for my carrier’s SMSC (short message service centre). Then we have the date and time. The next line is the contents of the text message – what we need to examine in our sketch.
The second text message in the example above is how we will structure our control SMS. Our sketch will wait for a # to come from the serial line, then consider the values after a, b, c and d – 0 for off, 1 for on. Finally, we need to send one more command to the GSM module after we have interpreted our SMS:
AT+CMGD=1,4
This deletes all the text messages from the SIM card. As there is a finite amount of storage space on the SIM, it is prudent to delete the incoming message after we have followed the instructions within. But now for our example. We will control four digital outputs, D9~12. For the sake of the exercise we are controlling an LED on each digital output, however you could do anything you like. Although the sketch may seem long and complex, it is not – just follow it through and you will see what is happening.
Example 27.2 (Mega version)
/* Example 27.2 Control four digital outputs via SMS
tronixstuff.com/tutorials > chapter 27 CC by-sa-nc
NOT for Arduino Mega */
#include <NewSoftSerial.h> //Include the NewSoftSerial library to send serial commands to the cellular module.
char inchar; //Will hold the incoming character from the Serial Port.
NewSoftSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup()
{ // prepare the digital output pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW); //Initialize GSM module serial port for communication.
cell.begin(9600);
delay(30000); // give time for GSM module to register on network etc.
cell.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
cell.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
}
void loop()
{ //If a character comes in from the cellular module...
if(cell.available() >0)
{
inchar=cell.read();
if (inchar=='#') // OK - the start of our command
{
delay(10);
inchar=cell.read();
if (inchar=='a')
{
delay(10);
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
inchar=cell.read();
if (inchar=='b')
{
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
inchar=cell.read();
if (inchar=='c')
{
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
inchar=cell.read();
if (inchar=='d')
{
delay(10);
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
}
}
cell.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
}
}
And now for a video demonstration:
So there you have it – controlling your Arduino digital outputs via a normal telephone or SMS. Now it is up to you and your imagination to find something to control, sensor data to return, or get up to other shenanigans.
If you enjoyed this article, you may find this of interest – controlling AC power outlets via SMS.
Have fun and keep checking into tronixstuff.com. Why not follow things on twitter, Google+, subscribe for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other – and we can all learn something.
Like this:
January 31, 2011 - Posted by John Boxall | arduino, cellphone hacking, cellular, GSM, hardware hacking, microcontrollers | arduino, call, card, CEL-09607, cellphone, cellular, control, electronics, GSM, guide, guides, hacking, lesson, lessons, message, mobile, monitor, network, project, projects, remote, shield, SIM, SMB5100B, SMS, sparkfun, spreadtrum, string, telephone, text, tutorial, tutorials
26 Comments
Sorry, the comment form is closed at this time.
YouTube
Visit tronixstuff on YouTube for our range of videosClock Projects
Arduino Tutorials
Click for Detailed Chapter Index
Chapters 0 1 2 3 4
Chapters 5 6 6a 7 8
Chapters 9 10 11 12 13
Ch. 14 - XBee
Ch. 15 - RFID
Ch. 16 - Ethernet
Ch. 17 - GPS part I
Ch. 18 - RGB matrix
Ch. 19 - GPS part II
Ch. 20 - I2C bus part I
Ch. 21 - I2C bus part II
Ch. 22 - AREF pin
Ch. 23 - Touch screen
Ch. 24 - Monochrome LCD
Ch. 25 - Analog buttons
Ch. 26 - Arduino + GSM - part I
Ch. 27 - Arduino + GSM - part II
Ch. 28 - Colour LCD
Ch. 29 - TFT LCD
Ch. 30 - Arduino + twitter
Ch. 31 - Inbuilt EEPROM
Ch. 32 - Infra-red control
Ch. 33 - Control AC via SMS
Ch. 34 - SPI bus part I
Ch. 35 - Video-out
Ch. 36 - SPI bus part II
Ch. 37 - Timing with millis()
Ch. 38 - Thermal Printer
Ch. 39 - NXP SAA1064
Ch. 40 - Push wheel switches
Ch. 40a - Wheel switches II
Ch. 41 - More digital I/O
Ch. 42 - Numeric keypads
Ch. 42a - Keypads II
Ch. 43 - Port Manipulation
Ch. 44 - ATtiny+Arduino
Ch. 45 - Ultrasonic Sensor
Ch. 46 - Analog + buttons IISearch
RSS Feeds
Categories
Previous posts
Archives
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
Contact information
email - john at tronixstuff dot com
Google Group
Australian Electronics!
Buy and support Silicon Chip - Australia's only Electronics Magazine.Creative Commons
All the original material in this website, unless noted otherwise, is covered under a Creative Commons Attribution-Non Commercial-Share Alike v3.0 license. Please email me if you see any mis-attributions or would like to use my content in different circumstances.on twitter…
- Congratulations @telstra - activated the 1000th LTE base station: bit.ly/LFUjRk 1 day ago
-
Flickr Photos



More Photos Find me on…
Interesting Sites
David L. Jones' eev blog
Freetronics Arduino Geniuses!
Little Bird Electronics Service Powered Electronics!
Silicon Chip magazine Always a great read!
Amazing Arduino Shield Directory
The Amp Hour podcast
EEWeb Elec Engineering Forum
Superhouse.tv High-tech home renovation
Mr Dick Smith OANuclear weapons = global suicide
In a war with nuclear weapons, everybody loses. Please check these out:
Count Down to Zero
The War Game
Threads




This board lets us imagine tons and tons of fun projects and/or enhancements for already existing devs.
Thank you once again for this lesson John!
Keep on
Gilles
Thanks for this and all other articles!
Your welcome Jim, thank you for your comment
cheers
John
Hi, How would one use DTMF codes to control the arduino ?
Well it could be done – need the GSM module to answer a voice call (easy) and then pass the signal to a DTMF decoder IC (e.g. http://www.siongboon.com/projects/2005-07-18_dtmf_circuits/)
cheers
john
Hi, I think these tutorials are great. I would really like to see a GSM Cellular – Part 3 Tutorial on sending data (like a picture or other file) from a SD card via MMS or GPRS to web.
Cheers,
Paddy
Hi Paddy
I have already started part 3, it is about GPS module and GSM, for tracking and so on.
However your idea is a good one. I’ll look into it.
cheers
john
Hey John,
Any update on your GPS/GSM tutorial? I am stuck and cannot seem to make a connection between my SM5100B and my server. I am using the TMobile network and continually only get CSQs of 15 or 16. Is this too low for GSM transmission? Any help you could provide would be great.
Hello Jesse
At the moment I have lent my GSM shield to someone else, so will be a month or so before the next update.
That CSQ is a bit low. If you are not already doing so, you might want to try a full-size aerial such as the type used on a motor vehicle.
cheers
john
Hey John
I bought a GSM shield and a Arduino duemilanove.
Can you help me? How I connect the shield and the Arduino?
Thanks
It should just plug in. Please read GSM tutorial part one as well.
http://tronixstuff.wordpress.com/2011/01/19/tutorial-arduino-and-gsm-cellular-part-one/
Hello i’m from Mexico your description is very complete. i use pic microcontrollers. your web page is very helpful. thanks for the informations and congratulations for your web page.
Hello Alex
Thank you for your feedback, I really appreciate it
cheers
John
Hi
Great tutorial. When will part 3 be ready?
Thank you. Not too sure, am rather busy with other things at the moment. Is there anything in particular you would like to see?
Cheers
John
I would love to learn how to read data from various sensors and send it to a web page, twitter etc…. with the gprs/gsm shield.
Thanks
Trond
OK, I’ll put it on my to-do list – but can’t guarantee when it will happen
cheers
john
Thanks
Looking forward to it…..
Trond
Hello John, Firstly great insight into gsm basics. Could i trouble you for an example of sending a data stream via the system such as binary data collected from a monitoring device. I don’t want the full tutorial just a basic example of how to send the data.
Cheers Pete
Could you please email me with a bit more detail, e.g. the monitoring device, and how you want to send the data (e.g. wireless, GSM, etc)? john at tronixstuff dot com.
Thanks
john
Hi, great tutorial, well explained!!
Do you know if it’s possible (and do you have any examples), of the exact same type of thing, only NOT asking the GSM module to output to the serial port immediately, but rather keeping the information on the GSM module until requested by the host uC? The reason I’m asking, is that we have a limited buffer length on our uC, and a relatively large amount of code doing other things, and we run the risk of losing information due to buffer overrun if our “loop()” doesn’t get round and check the serial port quickly enough. I guess it means using AT commands to ask for status and as for data, but I’ve been looking at this now for a total of about 3 hours, so any pointers in the right direction would be great as I’m totally new to it!
Also (sorry to be so demanding), do you have any examples on ways to manage the error handling?
Very satisfying getting code to send a message to my phone…!!
Peter
Hello Peter
Thanks for your feedback. No, I haven’t seen a module that keeps the data to itself. Perhaps you could have one microcontroller board (such as an Arduino Nano) controlling the GSM module and storing relevant data, and the host microcontroller would be separate from that. With regards to error handling some people have been using string functions to receive and work with the text coming from the GSM module, however I have not done this myself as I have moved on from this area of work.
cheers
john
Hi John,
How could I go about getting the system to only respond to a certain mobile number?
Thanks
Jules
Lazy answer – don’t let anyone find out the number attached to the SIM card. Sorry, couldn’t help myself
Seriously – when an SMS is received the sender’s number comes with it. You can have the sketch examine the number (as we do with the SMS commands) and check it matches your allowed number(s) – or if no CLI comes in, ignore it. Etc.
cheers
john
Hey John,
is there any chance to show me a sample code on how to receive sms to my gsm shield. I’m using the same sheild as you.
Cheers Feda
Not at the moment – I don’t have this shield anymore. When I get another one I will revisit this for you.