0

I have a problem using a servo and a GPS board at the same time on an Arduino Uno.

For the servo, I use the following code:

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

For the GPS module, I follow the guidance on this webpage describing the NEO-6M GPS Module

#include <SoftwareSerial.h>

// The serial connection to the GPS module
SoftwareSerial ss(4, 3);

void setup(){
  Serial.begin(9600);
  ss.begin(9600);
}

void loop(){
  while (ss.available() > 0){
    // get the byte data from the GPS
    byte gpsData = ss.read();
    Serial.write(gpsData);
  }
}

I combine these two code blocks into one *.ino file (combining the declarations, placing all the setup portions in the void setup(){...} function and the rest in the void loop(){...} part).

Here is the problem: Each block by itself runs fine. The "GPS block" returns GPS messages and the "Servo block" runs smoothly through its position sweep. However, when this code is combined the servo starts to act erratically and "jerks" back and forth. After some testing, it appears that starting the serial connection (ss.begin(9600);) changes the behavior of the servo control pin (here 9) and it looses its pulse width modulating (PWM) capability. The servo then starts to "jerk" to random positions.

Does anybody have insight into this phenomenon? Is there a way to operate the servo and the GPS module together?

Here is a functional code block that sweeps through the servo positions smoothly, by commenting out the line //ss.begin(GPSBaud); and replicates the erratic behavior by uncommenting the same line ss.begin(GPSBaud);.

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

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position


static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);


void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(GPSBaud);
  //ss.begin(GPSBaud);
}


void loop() {
  for (pos = 20; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 160; pos >= 20; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

  • Would really help if you said why the system acted unexpectedly. Guessing, I would first disconnect the servos to see if you have a power supply problem. – st2000 Jul 5 at 16:11
  • Thanks for editing your question. – st2000 Jul 5 at 16:14
  • Usually it is never a good idea to use a software implemented serial communications port. Especially when combining multiple Arduino libraries. Likely you will have to graduate to a more capable embedded processor to get around this problem. (You may be the 1st to combine these 2 libraries.) I still am concerned about the power (are you supplying power to the servos separately from the Arduino and GPS module?). – st2000 Jul 5 at 16:20
  • @st2000, thanks for your comments. 1) I only use the USB connection for power. I will check if I have an external power supply available. 2) You recommend using a more capable embedded processor. Do you have a suggestion on which one might be suitable? – c-type Jul 5 at 16:40
  • @st2000 I connected a 9V battery as external power source in addition to the USB connection. This does not change the behavior. – c-type Jul 5 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.