only one servo spins

Hello. I'm doing a project including servos, DC motors, and sensors. Almost everything else works, except the servos. Only one spins.

this is the code I was using before I modified part of it.

#include <Servo.h>

Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo

void setup() { 
  servoLeft.attach(10);  // Set left servo to digital pin 10
  servoRight.attach(9);  // Set right servo to digital pin 9
} 

void loop() {            // Loop through motion tests
  forward();             // Example: move forward
  delay(2000);           // Wait 2000 milliseconds (2 seconds)
  reverse();
  delay(2000);
  turnRight();
  delay(2000);
  turnLeft();
  delay(2000);
  stopRobot();
  delay(2000);
}

// Motion routines for forward, reverse, turns, and stop
void forward() {
  servoLeft.write(0);
  servoRight.write(180);
}

void reverse() {
  servoLeft.write(180);
  servoRight.write(0);
}

void turnRight() {
  servoLeft.write(180);
  servoRight.write(180);
}
void turnLeft() {
  servoLeft.write(0);
  servoRight.write(0);
}

void stopRobot() {
  servoLeft.write(90);
  servoRight.write(90);
}

since the servos I'm using aren't continuous rotation servos, I modified the code so it would be like this:

#include <Servo.h>

Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo

int pos = 0;

void setup() { 
  servoLeft.attach(1);  // Set left servo to digital pin 10
  servoRight.attach(2);  // Set right servo to digital pin 9
} 

void loop() {            // Loop through motion tests
  forward();             // Example: move forward
  delay(2000);           // Wait 2000 milliseconds (2 seconds)
  reverse();
  delay(2000);
  stopRobot();
  delay(2000);
}

// Motion routines for forward, reverse, turns, and stop
void forward() {
  servoLeft.write(0);
  servoRight.write(180);
}

void reverse() {
  servoLeft.write(180);
  servoRight.write(0);
}

void stopRobot() {
  servoLeft.write(90);
  servoRight.write(90);
}

the second code compiled and uploaded, but only one servo spun.
the schematic for the two servos is at this link: http://www.robotoid.com/appnotes/arduino-operating-two-servos.html

thank you for all your help.

Different pins between the two sketches.
Significant?
(Personally, I wouldn't use a serial pin)

Pin 1 is one of the serial comm pins (TX), what you had (9 & 10) should have worked. Are you sure both servos can move through the entire 0 ... 180 degree range? You're not trying to power 2 servos from the Arduino's 5V pin, are you?

Be sure to use a separate power supply for motors and servos, and don't forget to connect all the grounds.

Have you tried unplugging the working servo and plugging in the non-working servo where the working servo was plugged?

Hi there!

Usually the pin that the servo uses needs to be a PWM pin. Pins 1 and 2 are usually not PWM pins, which would indicate the issue.

There are some boards like the Arduino Mega that have PWM capabilities on these pins. Can you tell me what board you are using?

As others have said, it is usually not the greatest idea to use the Tx and Rx pins (1 and 0) as signals unless necessary, as it can add problems.

That being said, try returning to pins 9 and 10 for the two servo signal pins with your modified code and see what happens!

Good luck :slight_smile:

bos1714:
Usually the pin that the servo uses needs to be a PWM pin. Pins 1 and 2 are usually not PWM pins, which would indicate the issue.

I don't know where you got your ideas from but the Servo library does not need PWM pins. It works on any digital pin. That is at least in part because servos are NOT driven with standard PWM.

Steve

bos1714:
Usually the pin that the servo uses needs to be a PWM pin. Pins 1 and 2 are usually not PWM pins, which would indicate the issue.

You are confused, the Servo library works with any pin at all, analog(*) or digital (at least on the ATmega
Arduinos). Its an interrupt driven library, and is documented if you have a look...

Pin 1 is serial TX, it may be clashing due to this.

(*) For those analog pins that are also mapped to a I/O port.

Please show your schematics, cuz sketch is ok.

I made sure both grounds were connected, and it worked! Thank you for all the help :slight_smile:

I also changed the pins I was using for the servos from 1 and 2 to 9 and 10.