GSM module skipping AT commands when connected with servo motor

Hello Everyone,

Quickly briefing about my project.
Project is about controlling servo motor with sms.

Has 4 components - (sim800l v2 + arduino uno r3 + 5v relay + servo motor)
along woth the software to control servo.

When i test the software for different start stop parameters by connecting three components sim800l v2 + arduino + relay then all the test cases are pass. I am able to see this in serial monitor.

BUT
the moment i connect servo motor, the OFF parameter of the software and STATUS do not work. Sms light blinks on gsm module, sms is recoeved but does not the required work.

I am completely confused as to the same software passes all the parameters without connecting servo but the moment i connect servo ON and Start parameters goes haywire.
Only ON parameter works.

Any advice

if possible please attach your code to better understand whta is going on in your loop function.

// Include Software Serial library to communicate with GSM
#include <SoftwareSerial.h>
#include <Servo.h>

// Configure software serial port
SoftwareSerial SIM900(10, 11);

// Variable to store text message
String textMessage;

// Create a variable to store Motor state
String motorState = "INITIALLY OFF";

// Relay connected to pin 12
const int relay = 2; 

// Servo related parameters
int angle = 150;
int servoPin = 5;
Servo servo; 

void setup() {
  
  // Set relay as OUTPUT
  pinMode(relay, OUTPUT);

  // By default the relay is off
  digitalWrite(relay, HIGH);
  
  // Initializing serial commmunication
  Serial.begin(19200); 
  SIM900.begin(19200);

  // Attaching servopin
  servo.attach(servoPin);

  // Give time to your GSM shield log on to network
  delay(5000);
  Serial.print("SIM900 ready...");

  // AT command to set SIM900 to SMS mode
  SIM900.print("AT+CMGF=1\r"); 
  delay(100);
  // Set module to send SMS data to serial out upon receipt 
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
}

void loop(){
  if(SIM900.available()>0)
  {
    textMessage = SIM900.readString();
    Serial.print(textMessage); 
    Serial.println("Relay set to onetime");   
    delay(10);
  } 
        if(textMessage.indexOf("ON")>=0)
         {
            // Turn on relay and save current state
              digitalWrite(relay, LOW);
              motorState = "on";
              Serial.println("Relay set to ON");   
                  // 58 is the deep angle press
                  for(angle = 86; angle < 180; angle++)  
                     {                                  
                       servo.write(angle);              
                       delay(20);                  
                     }

                  delay (3000);

                 // 58 is the deep angle press
                 for(angle = 180; angle > 86; angle--)    
                    {                                
                        servo.write(angle);          
                         delay(20);      
                    }
  
  }
  if(textMessage.indexOf("OFF")>=0)
  {
    // Turn off relay and save current state
    digitalWrite(relay, HIGH);
    motorState = "off";
    Serial.println("Relay set to OFF");
    textMessage = ""; 
  }
  if(textMessage.indexOf("STATE")>=0){
    String message = "Motor is " + motorState;
    Serial.println("Motor state resquest");
    SIM900.println("AT+CMGF=1");
    delay(1000);
    SIM900.println("AT+CMGS= \"xxxxxxxxx\"\r");
    delay(1000);
    SIM900.println("Motor is "+ motorState);
    delay(100);
    SIM900.println((char)26);
  }
}  

1 Like

I have posted my code. Please suggest. If I test in serial window by sending sms (without attaching servo motor, it works fine, By all parameters On, OFF and STATE ) But the moment I attach servo only ON parameter works. After getting in ON state nothing works further. I cannot OFF it by sending OFF sms

you may visit this artivle and read carefully the cases in turning i=on and off the relay, here is the link. https://arduinogetstarted.com/tutorials/arduino-relay

also you can check if your wiring is exaclty the same as in the article it should work, can you send the schematic of your project?

No gadget here is running more than 5v.
Even servo motor itself is running on 3.5v.
(Sg-90 model)

true, but in his scenario if using relay is required on what motor is he using, it may be helpfull to read that article because relay is not so easy to work with, especially if you are new in arduino

SoftwareSerial makes heavy use of interrupts, which will interfere with the interrupts needed by the Servo library, and the extensive use of delay() in your code is going to risk overflowing the SoftwareSerial receive buffer unless the GSM messages are limited to 63 characters or less.

You can either drive the servo through a PWM output, or use a board with a 2nd hardware serial port (you could also use Serial for the GSM communications, if you don't need to send to/from the computer, but this also requires disconnecting the GSM during code uploads).

< edit >
On an UNO, I would also recommend not using String variables, use a char array instead.

Thats a great info, you have give..thanks..
However
One more thing, could I use the PWMservo.h instead of servo.h to solve this interrupt related details ??

Or may you can share some link on how to use PWMservo.h ??

Yes. It should be a drop in replacement as it uses the same commands.
It is available through the library manager.

On the UNO you will need to use pins 9 or 10 for the Servo.

Board                     SERVO_PIN_A   SERVO_PIN_B   SERVO_PIN_C
-----                     -----------   -----------   -----------
Arduino Uno, Duemilanove       9            10          (none)

So, I tried replacing the Servo.h with PWMServo.h. But still the output is same. Once inside the start block, as soon as servo starts working, GSM is not accepting the OFF or STATE commands.
Any other suggestions ?

What messages from the GSM are you seeing in the Serial monitor after the servo is operating?
How long is the longest message you expect to receive from the GSM? If it exceeds 63 characters (including any newline or carriage returns), then you need to get rid of all the delay() statements and use millis() for timing. You need to guarantee the entire loop will execute in less than 33mS (the time it takes to receive 63 characters), otherwise you will overflow the receive buffer.

It would help if you substantially increased the baud rate for Serial, using the same baud rate as
SoftwareSerial risks overflowing the SoftwareSerial receive buffer if you print enough to completely fill the Serial transmit buffer.

Can you post a diagram of how everything is connected, particularly how you are powering the servo?

Not sure about the first part though.

But for the second I am using 19200 baud rate in the software serial code.

Is software the only part where I can increase the baud rate ??
Wondering if increasing the baud can make GSM not to miss the messages.

This line to clear the received message is in the code after you receive "OFF".

What happens if you add this line to the cases where you receive "ON" and "STATE"?

If i write this in ON and STATE block... logic works fine. Motor will only run once and exit.
However I want that motor should run until OFF msg comes as sms.

Are you referring to the motor or the Servo?

When you get the "ON" message what wants to run until you get the "OFF" message.

I am refering to servo as motor.
ON means sServo motor should be turned ON till OFF sms is received.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.