Tutorial: Arduino and GSM Cellular – Part One

This is chapter twenty-six 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. Please note from November 1, 2010 files from tutorials will be found here.

Welcome back fellow arduidans!

Today we are going to start using our Arduino to communicate over a GSM mobile telephone network using the SM5100B Cellular Shield:

This will be the first of several articles related to the SM5100B GSM shield. The available documentation for this unit was abysmal, so please be patient.  My goal is to illustrate different ways of interacting using the shield, with which you can then use your existing knowledge to build upon those ways. For this article I want to get a few things out of the way first, which may sound a little harsh, but are necessary.

Firstly – telephone calls and text messages (SMS) can cost real money, so if your sketch goes bonkers and blasts out a few hundred text messages while you’re in the kitchen having tea, you will have to pay for them. It may be prudent to use a prepaid cellular account for testing purposes.

Secondly, the current draw. Sending an SMS (a tiny burst of data) will generally draw around less than 400 mA, which is within our normal Arduino specification. If you are using your shield to make or receive actual telephone calls, the shield can draw up to two amps of current. The actual current draw will vary depending on the strength of network reception in your location. For example, I live in direct line-of-sight of a network tower, use a carphone aerial and have full reception. When using the shield to make a call the current draw is around 420 mA. If in doubt, use the ammeter function of a multimeter to monitor current draw.

However if I was 25km east of Sydney Harbour**, the shield would use maximum transmit/receive power and pull the two amps. Therefore if communicating in this way you cannot run your Arduino and this shield from the USB port or the DC socket. And don’t be lazy by trying it anyway. Instead, power your project with a high-capacity 5V power supply to the 5V and GND pins on your shield.

** If I were 25km out to sea, it would be on the Queen Mary II and Arduino would be the last thing on my mind…

Thirdly, you will need an aerial. These can range from the small “rubber ducky” types, to full carphone models. If you are using the larger vehicle-style aerial, you might find that the plug will not fit to the shield’s connector. For example, consider the following:

On the left is the end of the lead from the carphone aerial, the right is the lead from the GSM shield. Problem! The solution is in the centre: an FME male to SMA male adaptor. This one came from Farnell/element-14, part number 1826209 (it is a Multicomp R23-014-00-002611000). Furthermore, care needs to be taken with your GSM shield with regards to the aerial lead-module connection, it is very fragile:

And finally, please do not try to recreate the following:

<img class=" aligncenter" title="From http://simpsons.wikia.com/wiki/User:Jihem [CC by-sa]” src=”http://images1.wikia.nocookie.net/__cb20091128195239/simpsons/images/1/1a/AutoDialer_AT5000.jpg&#8221; height=”288″ alt=”” style=”display: block; margin-left: auto; margin-right: auto; border: 1px solid #333333;” width=”384″ />

From a software perspective we will need the NewSoftSerial Arduino library, so please download and install that before moving on.

Now, on with the show…

Why would you want to connect your Arduino to the mobile phone network anyway? To send and receive data across the mobile network, and to transmit or receive instructions or commands. More specifically, within the scope of this article we will do so mainly using text or SMS messages.

When shipped as new, our shield does not include the 6-pin and 8-pin headers (though it should for the price – cheapskates). So you will need to source your own and solder them in by inserting them onto the pins of another shield and drop the shield on top, then solder the shield in nicely.

Initial check – does it work?

This may sound like a silly question, but considering the cost of the shield and the variables involved, it is a good idea to check if your setup is functioning correctly before moving on. From a hardware perspective for this article, you will need your Arduino board, the GSM shield with activated SIM card and an aerial, and a range of previously used components. For our initial test, upload the following sketch :

Example 26.1

/* SparkFun Cellular Shield - Pass-Through Sample Sketch
SparkFun Electronics Written by Ryan Owens CC by v3.0 3/8/10
Thanks to Ryan Owens and Sparkfun for sketch */
#include <NewSoftSerial.h>  //Include the NewSoftSerial library to send serial commands to the cellular module.
#include <string.h>         //Used for string manipulations
char incoming_char=0;      //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.
void setup()
{
  //Initialize serial ports for communication.
Serial.begin(9600);
cell.begin(9600);
Serial.println("Starting SM5100B Communication...");
}
void loop()
{
//If a character comes in from the cellular module...
if(cell.available() >0)
{
incoming_char=cell.read();    //Get the character from the cellular serial port.
Serial.print(incoming_char);  //Print the incoming character to the terminal.
}
//If a character is coming from the terminal to the Arduino...
if(Serial.available() >0)
{
incoming_char=Serial.read();  //Get the character coming from the terminal
cell.print(incoming_char);    //Send the character to the cellular module.
}
}

Then connect the GSM shield, aerial, insert the SIM card and apply power. Open the serial monitor box in the Arduino IDE and you should be presented with the following:

It will take around fifteen to thirty seconds for the text above to appear in full. What you are being presented with is a log of the GSM module’s actions. But what do they all mean?

  • +SIND: 1 means the SIM card has been inserted;
  • the +SIND: 10 line shows the status of the in-module phone book. Nothing to worry about there for us at the moment;
  • +SIND: 11 means the module has registered with the cellular network
  • +SIND: 3 means the module is partially ready to communicate
  • and +SIND: 4 means the module is registered on the network, and ready to communicate

If your terminal returned a +SIND 8 instead of 4, that is OK, we’ll sort that out in a moment.

From this point on, we will need to use a different terminal program, as the Arduino IDE’s serial monitor box isn’t made for full two-way communications. You will need a terminal program that can offer full two-way com port/serial communication. For those running MS Windows, an excellent option is available here. It’s free, however consider donating for the use of it. For other operating systems, people say this works well.

So now let’s try it out with the terminal software. Close your Arduino IDE serial monitor box if still open, then run your terminal, set it to look at the same serial port as the Arduino IDE was. Ensure the settings are 9600, 8, N, 1. Then reset your Arduino and the following should appear:

The next step is to tell the GSM module which network frequency(ies) to use. Please download this document (.pdf), and view page 127. There is a range of frequency choices that our module can use. If you don’t know which one to use, contact the telephone company that your SIM card came from. Australia – use option 4. Choose your option, then enter

AT+SBAND=x

(where X is the value matching your required frequency) into the terminal software and click SEND. Then press reset on the Arduino and watch the terminal display. You should hopefully be presented with the same text as above, ending with +SIND: 4. If your module returns +SIND: 4, we’re ready to move forward.

Our next test is to call our shield. So, pick up a phone and call it. Your shield will return data to the terminal window, for example:

As you can see, the module returns what is happening. I let the originating phone “ring” twice, and the module received the caller ID data (sorry, blacked it out). Some telephone subscribers’ accounts don’t send caller ID data, so if you don’t see your number, no problem. “NO CARRIER” occurred when I ended the call. +SIND: 6,1 means the call ended and the SIM is ready.

The document (.pdf) we downloaded earlier contains a list of AT commands – consider this a guide to the language with which we instruct the GSM module to do things. Let’s try out some more commands before completing our initial test. The first one is:

ATDxxxxxx

which dials a telephone number xxxxxx. For example, to call (212)-8675309 use

ATD2128675309

The next one is

ATH

which “hangs up” or ends the call. So, let’s reach out and touch someone. In the terminal software, enter your ATDxxxxxxxx command, then hit send. Let your phone ring. Then enter ATH to end the call. If you are experimenting and want to hang up in a hurry, you can also hit reset on the Arduino and it will end the call as well as resetting the system. So by now you should realise the GSM module is controlled by these AT commands.

To use an AT command in a sketch, we use the function

cell.println()

for example, to dial a phone number, we would use

cell.println("ATD2128675309");

To demonstrate this in a sketch, consider:

Example 26.2

A simple sketch to dial a telephone number, wait, then hang up. Replace xxxxxxxx with the number you wish to call.

/*  Example 26.2  GSM shield making and ending a telephone call
http://tronixstuff.com/tutorials > chapter 26 */ #include <NewSoftSerial.h>  //Include the NewSoftSerial library to send serial commands to the cellular module.
NewSoftSerial cell(2,3);  //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
void setup()
{  //Initialize serial ports for communication.
cell.begin(9600);
delay(25000); // give the GSM module time to initialise, locate network etc.
// this delay time varies. Use example 26.1 sketch to measure the amount
// of time from board reset to SIND: 4, then add five seconds just in case
}
void loop()
{
cell.println("ATDxxxxxxxxx"); // dial the phone number xxxxxxxxx
// change xxxxxxx to your desired phone number (with area code)
delay(20000); // wait 20 seconds.
cell.println("ATH"); // end call
do // remove this loop at your peril
{    delay(1);  }
while (1>0);
}

The sketch in example 26.2 assumes that all is well with regards to the GSM module, that is the SIM card is ok, there is reception, etc. The delay function in void setup() is used to allow time for the module to wake up and get connected to the network. Later on we will read the messages from the GSM module to allow our sketches to deal with errors and so on. However, you can see how we can simply dial a telephone. You could now have a home alarm system that can call you upon an event happening, etc.

Another popular function is the SMS or short message service, or text messaging.

Sending text messages from our Arduino…

The AT commands for sending text messages are a little more involved, but nothing to worry about. First, we need to tell the module our SMS will be ASCII text, this is done with:

AT+CMGF=1

Then we need to give the mobile phone number to send the message to:

AT+CMGS="xxxxxxxxx"

At this stage, there needs to be a small delay, around half a second. Then the module will expect the contents of the message (maximum 160 characters) followed by the ASCII equivalent of Ctrl-Z. After sending the message, the module needs around fifteen seconds to finalise the procedure, so if sending multiple messages, allow for this with a delay() function.

Example 26.3

So let’s send a text message. I have tried to simplify the process as much as possible, so the following sketch is my simple “Send an SMS” example:

/*  Example 26.3 GSM shield sending a SMS text message
http://tronixstuff.com/tutorials > chapter 26 */
#include <NewSoftSerial.h>
NewSoftSerial cell(2,3);  // We need to create a serial port on D2/D3 to talk to the GSM module
char mobilenumber[] = "xxxxxxxxx";  // Replace xxxxxxxx with the recipient's mobile number
void setup()
{  //Initialize serial ports for communication.
cell.begin(9600);
delay(35000); // give the GSM module time to initialise, locate network etc.
// this delay time varies. Use example 26.1 sketch to measure the amount
// of time from board reset to SIND: 4, then add five seconds just in case
}
void loop()
{
cell.println("AT+CMGF=1"); // set SMS mode to text
cell.print("AT+CMGS=");  // now send message...
cell.print(34,BYTE); // ASCII equivalent of "
cell.print(mobilenumber);
cell.println(34,BYTE);  // ASCII equivalent of "
delay(500); // give the module some thinking time
cell.print("They call me the count... because I like to count! Ah ha ha ha");   // our message to send
cell.println(26,BYTE);  // ASCII equivalent of Ctrl-Z
delay(15000); // the SMS module needs time to return to OK status
do // You don't want to send out multiple SMSs.... or do you?
{
delay(1);
}
while (1>0);
}

Now we can consider sending data and a fixed text message. To do so, we need to split the procedure of sending a text message into three parts: the sending data, the message and any data, and ending the procedure. This is done in the following example. The sketch generates a random number between 0 and 99. Then it counts from 0 to 99, and when the count reaches the random number, it sends the text message containing an initial note and the random number.

Example 26.4

/*  Example 26.4  Send a text message upon an event occurring
http://tronixstuff.com/tutorials > chapter 26 */
#include <NewSoftSerial.h>
NewSoftSerial cell(2,3);  // We need to create a serial port on D2/D3 to talk to the GSM module
char mobilenumber[]="xxxxxxxx";  // Replace xxxxxxxx with the recipient's mobile numberchar
textmessage[160]; // used to hold contents of message to send
int b=0;
void setup()
{  //Initialize serial ports for communication.
cell.begin(9600);
delay(35000); // give the GSM module time to initialise, locate network etc.
// this delay time varies. Use example 26.1 sketch to measure the amount
// of time from board reset to SIND: 4, then add five seconds just in case
pinMode(13, OUTPUT); // use the LED for status
randomSeed(analogRead(0));
}
void startSMS()
// function to send a text message
{
digitalWrite(13, HIGH);
cell.println("AT+CMGF=1"); // set SMS mode to text
cell.print("AT+CMGS=");
cell.print(34,BYTE); // ASCII equivalent of "
cell.print(mobilenumber);
cell.println(34,BYTE);  // ASCII equivalent of "
delay(500); // give the module some thinking time
}
void endSMS()
{
cell.println(26,BYTE);  // ASCII equivalent of Ctrl-Z
delay(15000); // the SMS module needs time to return to OK status
digitalWrite(13, LOW);
}
void loop()
{
b=random(0, 99);
for (int a=0; a<100; a++)
{
if (a==b)
{
startSMS();
cell.print("The random number is: ");
cell.print(b);
endSMS();
}
}
do
{
delay(1);
} while (1>0);
}

The result is shown in this quick video:

So now you can see the basic procedure for sending mixed data in a text message. For the purpose of the video, and to save my sanity – the GSM shield’s telephone number is stored as a phone book entry, that is why the sender is shown as Arduino GSM.

Well that is all we have time for in this instalment. Thank you for your patience in awaiting this series of articles. Although getting it together was a little tricky, it was a lot of fun. I hope you enjoyed reading it as much as I did writing it for you.

If you have any questions about the processes or details in this article, please ask in our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, there is the odd competition or give-away –  and we can all learn something.

Otherwise, have fun, stay safe, be good to each other – and make something!

Advertisements
By John Boxall

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s