Servo Triggering by GSM

Hi guys,

I’m trying to trigger a micro servo by simply sending a sms via GSM shield.

To do that, I’ve added a simple “security verification” which is by setting the output depending on the sms received.

For example,

  • If the sms received is “1ao”, an LED indicator will be turn on, and the servo will turn repeatedly. (in a sequence of right, left & middle)

  • Else, if the sms received is “1af”, indicator will be turn off, and no movement should be perform by the servo.

The code is written as shown as below:

#include <SoftwareSerial.h>
#include <Servo.h>

Servo servo;

char inchar; // Will hold the incoming character from the GSM shield

SoftwareSerial SIM900(2, 3);
int led1 = 12; //Signal Indicator

void setup()
  // set up the digital pins to control

  servo.attach(9);   //The servo is attached to pin 9
  pinMode(led1, OUTPUT);
  digitalWrite(led1, LOW); //Initiate the signal indicator

  // wake up the GSM shield
  delay(20000);  // give time to log on to network.
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  Serial.println("GSM is now ready :)");
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
  digitalWrite(9, HIGH);
  digitalWrite(9, LOW);
void loop()
  //If a character comes in from the cellular module...
  if(SIM900.available() >0)
    if (inchar=='1')
      if (inchar=='a')
        if (inchar=='f') //If the sms received is "1af" ...
          digitalWrite(led1, LOW); //To indicate that no signal is received.
        else if (inchar=='o') //If the sms received is "1ao" ...
          digitalWrite(led1, HIGH); //To indicate that signal is received.
          servo.write(0); //Turn servo to right.
          servo.write(160); //Turn servo to left.
          servo.write(90); //Face servo to the front.
          SIM900.println("AT+CMGD=1,4"); // delete all SMS

After carry out the testing, I’ve noticed that when the sms is received (1ao), the servo is able to rotate in the desired sequence (right, left, then middle).

But, it can be able to perform that for one time only, as the servo seems to be not responding after that anymore. It seems like the program is not looping, although I’ve written these under the void loop()

Is there any mistake that I did in the program ? Thanks for helping :slight_smile:

7 below shows how to post code:,148850.0.html

It also mentions something about NOT CROSS-POSTING.

Duplicate deleted.

Hello no22ben,

If you are using the UNO, the problem comes from SoftwareSerial library. I can’t tell in details what is really the problem, but in summary, it is a problem of interrupts and timers. Why? Because SoftwareSerial use same timer as the PWM outputs or something like that.

Here is a solution.

Use AltSoftSerial library and not more SoftwareSerial. Use pins 9 and 8
AltSoftSerial mySerial( 9, 8 )

Use ServoTimer2 with output 5

Both are in zip attached.

Sorry, I can’t explain more about this issue. I found the solution with google and a lot of tries. Generally, try to not use SoftwareSerial and buy a Mega with a dedicated Serial hardware. (14.4 KB)