t r o n i x s t u f f

fun and learning with electronics

Tutorial: Arduino and GSM Cellular – Part One

Connect your Arduino to the cellular network with the SM5100 GSM module shield. 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.

Updated 18/03/2013

Introduction

The purpose of this tutorial is to have your Arduino to communicate over a GSM mobile telephone network using the SM5100B GSM Cellular Shield:

This is the first of several articles related to the SM5100B GSM shield. My goal is to illustrate various methods of interaction between an Arduino and the GSM cellular network using the shield, with which you can then use your existing knowledge to build upon those methods. Doing so isn’t easy – but it isn’t that difficult.

Stop! Please read first:

  • It is assumed that you have a solid understanding of how to program your Arduino. If not, start from chapter zero
  • Read the “boring stuff” section on my About page
  • Sending SMS messages and making phone calls cost real money, so it would be very wise to use a prepaid cellular account or one  that allows a fair amount of calls/SMS
  • The GSM shield only works with “2G” GSM mobile networks operating on the 850, 900 and PCS1800 MHz frequencies. If in doubt, ask your carrier first
  • Australians – you can use any carrier’s SIM card, except for “Three”
  • Canadians – this doesn’t work with Sasktel
  • North Americans – check with your cellular carrier first if you can use third-party hardware (i.e. the shield)
  • I cannot offer design advice for your project nor technical support for this article.
  • If you are working on a college/university project and need specific help – talk to your tutors or academic staff. They get paid to help you.
  • Please don’t make an auto-dialler…
Getting started

As mentioned previously, we’re using the Sparkfun GSM shield with the SM5100B module. When you order the shield, don’t forget to order the stacking header pin set as they’re not included with the shield, and you’ll need to solder them on yourself. Power -the GSM shield can often require up to 2A of current in short bursts – especially when turned on, reset, or initiating a call. However your Arduino board can only supply just under 1A. It is highly recommended that you use an external regulated 5V power supply capable of delivering 2A of current – from an AC adaptor, large battery with power regulator, etc. Otherwise there is a very strong probability of damaging your shield and Arduino.

Ignore this at your own risk

When connecting this supply DO NOT use the DC socket on the Arduino. Instead, connect the 5V (positive) from the supply to the 5V pin on the GSM shield, and the negative to the GND pin.

If you’re looking for a more permanent or easy-to-wire solution, get yourself a DFRobot power shield:

This shield sits on top of your GSM shield (which sits on top of your Arduino). Before use you need to set it up:

  1. The only jumpers that should be on the power shield are as shown in the image above;
  2. Connect a power supply of between 9 and 35V DC to the blue terminal block at the bottom-left of the shield;
  3. Connect a voltmeter/multimeter to the other blue terminal block at the top-left and adjust the potentiometer (blue thing between the terminal blocks) until the voltage measured is 5 volts; ignore the LEDs on the shield as they’re not that accurate;
  4. Run a wire from the positive power output to the 5V pin on the shield, and run another one from the negative power output to a GND pin on the shield;
  5. If you have the USB cable connected to your project while operating the GSM shield, remove the USB cable before turning off external power to the project.

Here’s what it looks like once assembled with the antenna:

Next – use an antenna! The wire hanging from the shield is not an antenna. YOU NEED THE ANTENNA! There are two choices. Either use the smaller one for areas where handheld mobile reception is acceptable, such as this one:

Or if you are in an area of weaker reception, use an external antenna such as that used on a motor vehicle. 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 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, download this document (.pdf). It contains all the AT and ERROR codes that will turn up when you least expect it. Please review it if you are presented with a code you are unsure about.

Wow – all those rules and warnings?

The sections above may sound a little authoritarian, however I want your project to be a success. With the previous iterations of the tutorial people just didn’t follow the instructions – so I hope you do :)

Are you using an Arduino Mega or Leonardo board?

Things are a little different for you. Those boards don’t support SoftwareSerial on digital pins 2 and 3 thus rendering the GSM shield a little trickier to use. Instead, bend back the D2 and D3 pins on the GSM shield as such (click image to enlarge):

Then run jumpers from D2 on the attached shield to D10 and another from D3 to D11. If you’re using the aforementioned power shield it would be on top of the GSM shield however the jumper wires would be the same. Finally in all the sketches, change the line SoftwareSerial cell(2,3);  to SoftwareSerial cell(10,11); . If you have a Leonardo, get a Uno.

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. Make sure your SIM card is set to not require a PIN when the phone is turned on. You can check and turn this requirement off with your cellphone. For our initial test, upload the following sketch:

Example 26.1

#include <SoftwareSerial.h>
char incoming_char=0; //Will hold the incoming character from the Serial Port.
SoftwareSerial 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.

Have your Arduino “call you”

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.

#include <SoftwareSerial.h>
SoftwareSerial cell(2,3); 
void setup()
{ 
 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.

Send an SMS from your Arduino

Another popular function is the SMS or short message service, or text messaging. Before moving forward, download and install Meir Michanie’s SerialGSM Arduino library from here.

Sending a text message is incredibly simple – consider the following sketch:

Example 26.3

#include <SerialGSM.h>
#include <SoftwareSerial.h>
SerialGSM cell(2,3);
void setup()
{ 
 delay(30000); // wait for GSM module
 cell.begin(9600);
}
void sendSMS()
{
 cell.Verbose(true); // used for debugging
 cell.Boot(); 
 cell.FwdSMS2Serial();
 cell.Rcpt("+xxxxxxxxx"); // replace xxxxxxxxx with the recipient's cellular number
 cell.Message("Contents of your text message");
 cell.SendSMS();
}
void loop()
{
 sendSMS();
 do // remove this loop at your peril
 { 
 delay(1); 
 }
 while (1>0);
}

It’s super-simple – just change the phone number to send the text message, and of course the message you want to send. The phone numbers must be in international format, e.g. Australia 0418 123456 is +61418123456 or USA (609) 8675309 is +16098675309.

Well that is all we have time for in this instalment.  Now you can move onto GSM and Arduino – part two.

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, 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.

January 19, 2011 - Posted by | arduino, CEL-00675, CEL-09607, cellphone hacking, cellular, GSM, SMS, tronixstuff, tutorial | , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

14 Comments »

  1. Thanks for this tutorial, John, I found it very helpful. I have a few questions I’m hoping you (or someone) might be able to answer: 1. When I first ran example 26.1 I got all the right SIND messages, but after a few weeks of running variations of the code the module now only returns 11 (and occasionally 7), even on the original code. Do you know why the other messages are no longer appearing? 2. The AT Command manual notes that the SIND messages are stored in flash memory. Does this mean that they are accessible by means other than cell.read()? 3. When I sent AT+ADCM it return the value 3979. Is this 3.979 V and does it correspond to the range 3.3 to 4.2 referred to in the SM5100B manual (Table 1)? Thanks for your help.

    Comment by Bruce | March 8, 2013 | Reply

    • Hi Bruce
      1) If you’re getting 11 there’s something on the network side that’s stopping full registration (e.g. account issues). 7 means you can’t register on your SIM’s network, but the module is finding another network. Check your account, SIM, antenna, reception etc. Or the module is playing up – it happens. (2) You can read SIND status by sending “AT+SIND?” so yeah, either cell.read or your own code to monitor what’s coming back from the module via serial. (3) AT+ADCM is reading the ADC on the module – page 32 of AT command guide says it varies between 0~5000.

      Comment by John Boxall | March 8, 2013 | Reply

  2. Man..when i tried example 26.3 i got SerialGSM does not a name type error..please help

    Comment by Phil | March 26, 2013 | Reply

  3. Hello, John.

    First I want to thank for this Tutorial which is very explained, but, I just I have any doubt, if the GSM cell can perform a conventional calling vois to vois since my personal cell phone to the GSM Cell and If I can conect a speaker to this shield.?

    Comment by Carlos | March 28, 2013 | Reply

  4. Hi John,
    Man I’m loving your tutorials, they are everything that I need in one place…thankyou!!!
    With example 26.3 I’m assuming that it is a typo on line two where there is nothing appended to your #include statement.
    I added the header file SoftwareSerial and everything seemed to work fine. Is that what is supposed to be there?

    Comment by David | April 17, 2013 | Reply

    • Yup, bloody WordPress. :)
      #include

      Comment by John Boxall | April 18, 2013 | Reply

  5. why do i get this message and it does not connecting to network?? what is +stin :9 ??

    Comment by Bogdan Nilă | April 23, 2013 | Reply

  6. Hi, than you for such wonderful tutorials. I have a SIM900 gsm shield and an arduino Dueminalove, I tried to make the shield send a text message to my cellphone but nothing seems to be working. I bought an AT&T sim card which is unlocked and mounted my shield on top of the arduino and I downloaded the GSM library. I used the code bellow for the message but it doesn’t do anything. Would you please help me? thank you.

    The code i used :

    #include
    #include
    SerialGSM cell(7,8);
    void setup()
    {
    delay(30000); // wait for GSM module
    cell.begin(9600);
    }
    void sendSMS()
    {
    cell.Verbose(true); // used for debugging
    cell.Boot();
    cell.FwdSMS2Serial();
    cell.Rcpt(“+14136452001″); // replace xxxxxxxxx with the recipient’s cellular number
    cell.Message(“Contents of your text message”);
    cell.SendSMS();
    }
    void loop()
    {
    sendSMS();
    do // remove this loop at your peril
    {
    delay(1);
    }
    while (1>0);
    }

    Comment by lulubelule | May 7, 2013 | Reply

    • Afraid I don’t have any experience with the SIM900 module – it’s different to the one used in the tutorial. Try contacting where you purchased it from.

      Comment by John Boxall | May 7, 2013 | Reply

  7. If you have a mega do you still need the extra 5v power supply or will it be able to provide enough to power the shield ?

    Comment by threelegs | May 8, 2013 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 3,836 other followers

%d bloggers like this: