GSMSerial: A cell phone for your Arduino

This library is to help interface a GSM device with an Arduino using the Software Serial library included in the 1.0 version of the IDE. It was developed on a Motorola C168i but should work in theory on any other GSM protocol device (at least with minimal adjustments). This SMS Tutorial at Developer’s Home was very helpful in teaching me what I needed to know about AT Commands and the already existent SSerial2Mobile library inspired my cell phone selection.

There is no doubt more room for improvement (particularly in optimizing the delays), but for now, the library can send & read text messages, can check if the phone is charging, and check the battery and signal levels. Included is an example sketch to help you get started. Wiring up a C168i to your cell phone isn’t too much hassle: all you need is a 3/32 mini-jack, three lengths of wire, and something to shift logic down from the Arduino’s 5V to the cell phone’s 3.3V. I started out with a voltage divider (the following tutorial will demonstrate that way) but I recommend switching to a logic shifter if you need a more reliable connection like I did.

I used a SN74AHC125N which is pretty simple: connect the Arduino’s 3.3V to Vcc, connect Arduino’s GND to GND and to the OE of whatever gates you use, the TX of the Arduino on the A and the RX of the cell phone the Y.

You can easily find a C168i on Ebay for less than 10 dollars. The simplest way to get service for it is to buy a pay-as-you-go SIM card, at Radio Shack for example. While you’re there you can also pick up the 3/32 plug; unfortunately, they don’t have any information on the packaging but I’ve included a figure here for your reference (see left).

 

 

Here’s a step-by-step on how to make your “breakout wire” and plug it into your Arduino.

Tools: soldering iron, wire clippers, wire strippers, a soldering stand is helpful

Bill of Materials:

3/32 Mini-jack
Three lengths of wire
Two resistors to make a voltage divider. I used resistor values of R1=3.3kOhms and R2=5.6kOhms but plenty of other ratios will work (just be careful that you don’t reduce current too much).In general, for any R1, ideal R2 = (3.3/5*R1) / (1-3.3/5)

Steps

Solder one wire that will be the TX for the phone or the RX on the Arduino end to the lower-most part of the connector.Be careful! The connector is made of plastic so you’ll want to avoid the need to resolder.
And another right above it (RX for phone, TX for Arduino)
Finally add the ground wire to large metal tab
Twist the wires and make them look pretty (I like to first twist two and wrap additional wires one-by-one)
Plug things into your breadboard! Choose a resistor ratio such that you divide 3.3V out of the Arduino’s 5V.Note the resistors I used are 3.3kOhms (R1/Left/Horizontal) and 5.6kOhms (R2/Right/Vertical).

 

 

11 Comments

  1. Alessio

    HI ! Compiling your code I get this error :

    /Users/alex89/Documents/Arduino/libraries/GSMSerial/GSMSerial.cpp: In member function ‘bool GSMSerial::hasTxt()’:
    /Users/alex89/Documents/Arduino/libraries/GSMSerial/GSMSerial.cpp:140: error: expected primary-expression before ‘{‘ token
    /Users/alex89/Documents/Arduino/libraries/GSMSerial/GSMSerial.cpp:140: error: expected `;’ before ‘{‘ token
    /Users/alex89/Documents/Arduino/libraries/GSMSerial/GSMSerial.cpp: In member function ‘bool GSMSerial::hasTxt()’:
    /Users/alex89/Documents/Arduino/libraries/GSMSerial/GSMSerial.cpp:140: error: expected primary-expression before ‘{‘ token
    /Users/alex89/Documents/Arduino/libraries/GSMSerial/GSMSerial.cpp:140: error: expected `;’ before ‘{‘ token

    • Louis (Author)

      Hi Alessio,

      I just uploaded a more recent version. Hopefully that will resolve the issue, which seems to be a forgotten semi-colon.

      Please let me know if that fixed it!

      • Alessio

        Hi Louis ! Thank you for your reply but I’m still having the same problems a few lines down:
        /Users/alex89/Documents/Arduino/libraries/GSMSerial/GSMSerial.cpp: In member function 'bool GSMSerial::hasTxt()':
        /Users/alex89/Documents/Arduino/libraries/GSMSerial/GSMSerial.cpp:145: error: expected primary-expression before '{' token
        /Users/alex89/Documents/Arduino/libraries/GSMSerial/GSMSerial.cpp:145: error: expected `;' before '{' token

        • Louis (Author)

          Hi Alessio – I think I figured it out so try the code on github again (only the .cpp file is different).

          You helped me find a useless bit of code so thanks for the heads up! The funny thing is that the compiler on the Linux version didn’t have any errors with it while on the Mac OS it did have a problem… weird.

          • Alessio

            I moved on just removing the content={}; line; I don’t know if the class is still working .. tomorrow I’ll check the new release .. Thank you for your time !

            Alessio

          • Louis (Author)

            Yep – that is the exact change I made. The code works fine without it – I think its a vestige of before I learned about terminal bytes!

  2. dalil

    first great project thank you

    question why i need 3.3 v

    • Louis (Author)

      The Motorola C168i communicates at 3.3V compared to the Arduino’s digital 5V.

      It will work for a little bit at 5V but will eventually burn out if you don’t shift it down to 3.3V.

  3. mugur

    connected the project and its working alryt. am kinda stuck somewhere in my program though.
    i want to read the content of the text message and store it in a variable to control some serial output. here is the code
    [quote]
          phone.[color=#CC6600]listen[/color]();

    [/quote]

    • mugur

      this is the code but its not working. somebody help out

      #include
      #include
      #include

      // initialize the library with the numbers of the interface pins
      LiquidCrystal lcd(8, 7, 5, 4, 3, 2);

      #define CONTACT “25472xxxxxxxxx”
      GSMSerial phone(10,11); //(RX) red,(TX) white

      void setup()
      {
      pinMode(48, OUTPUT);
      pinMode(49, OUTPUT);
      pinMode(50, OUTPUT);
      pinMode(51, OUTPUT);
      pinMode(52, OUTPUT);
      pinMode(53, OUTPUT);
      pinMode(46, OUTPUT);
      pinMode(47, OUTPUT);
      //pinMode(56, OUTPUT);

      lcd.begin(16, 2);
      lcd.print(“WELCOME TO KUCT!”);
      lcd.setCursor(0, 1);
      lcd.print(“DATE: 26th SEPT.”);

      Serial.begin(9600);
      phone.start();
      //easy method to send message
      phone.sendTxt(CONTACT, “Hello, Arduino World!”);
      //in case you want to do something more complicated
      phone.openTxt(CONTACT);
      phone.inTxt(“You like pi? “);
      double pi= 3.14;
      phone.inTxt(pi);
      phone.inTxt(“something New”);
      phone.inTxt(pi);
      phone.closeTxt();

      }

      void loop()
      {
      //make sure you run hasTxt before looking at
      //the class variables origin & content
      if(phone.hasTxt()){
      Serial.println(“Text message received”);
      lcd.setCursor(0, 0);
      lcd.print(“TEXT FROM:”);
      lcd.setCursor(10, 0);
      Serial.print(” origin: “);
      lcd.print(phone.origin);
      Serial.println(phone.origin);
      lcd.setCursor(0, 1);
      lcd.print(“CONTENT:”);
      lcd.setCursor(9, 1);
      lcd.print(phone.content);
      Serial.print(” content: “);
      Serial.println(phone.content);
      }
      Serial.println();
      if(phone.isCharging()){
      Serial.println(“The phone is charging”);
      lcd.setCursor(0, 0);
      lcd.print(“PHONE CHARGING!!”);
      }
      else
      {
      Serial.print(“Battery level is: “);
      Serial.println(phone.battery(),DEC);
      lcd.setCursor(0, 0);
      lcd.print(“BATT LEVEL=”);
      lcd.setCursor(11, 0);
      lcd.print(phone.battery());
      }
      Serial.println();
      Serial.print(“Signal strength is: “);
      lcd.setCursor(0, 1);
      lcd.print(“ntwk strngth:”);
      Serial.println(phone.signal());
      Serial.println();
      lcd.setCursor(13, 1);
      lcd.print(phone.signal());
      //delay(1000);
      if (Serial.available()) {

      //read serial as a character
      //phone.listen();
      char ser = Serial.read();

      //NOTE because the serial is read as “char” and not “int”, the read value must be compared to character numbers
      //hence the quotes around the numbers in the case statement
      switch (ser) {
      case ‘h’:
      triggerPin(46);
      break;
      case ‘e’:
      triggerPin(47);
      break;
      case ‘l’:
      triggerPin(48);
      break;
      case ‘o’:
      triggerPin(49);
      break;
      case ‘,’:
      triggerPin(50);
      break;
      case ‘ ‘:
      triggerPin(51);
      break;
      case ‘A’:
      triggerPin(52);
      break;
      case ‘R’:
      triggerPin(53);
      break;
      case ‘D’:
      triggerPin(54);
      break;
      //case ‘9’:
      //brreak;
      }
      }
      }

      void triggerPin(int pin){
      digitalWrite(pin, HIGH);
      delay(550);
      digitalWrite(pin, LOW);
      delay(250);

      }

    • Louis (Author)

      Does the example code included in the library work for you?

      What exactly isn’t working with your code?

Comments are now closed for this article.