1

I have a motor controller controlling two motors, which works perfectly fine when I comment out the two servos in the below code. These two servos are not connected to the motor controller but simply being operated using other Arduino Uno pins, as seen in the code.

When the servo attach() functions are included in the code the motor controller only works with one of the motors and the other completely stops working.

I don't understand why the servos are interfering with the motor controller. What's going on here?

#include <Servo.h> 
Servo myServo1;  
Servo myServo2; 

int m1_ALI = 10; 
int m1_BLI = 9; 
int m2_ALI = 3;
int m2_BLI = 11; 

int m1_AHI = 12;
int m1_BHI = 4;
int m2_AHI = 6;
int m2_BHI = 5;

void setup() { 

  //myServo1.attach(7); 
  //myServo2.attach(8); 

  pinMode(m1_AHI, OUTPUT);
  pinMode(m1_ALI, OUTPUT);
  pinMode(m1_BLI, OUTPUT);
  pinMode(m1_BHI, OUTPUT);
  pinMode(m2_AHI, OUTPUT);
  pinMode(m2_ALI, OUTPUT);
  pinMode(m2_BLI, OUTPUT);
  pinMode(m2_BHI, OUTPUT); 
}

void loop() {

moveForward(30);

}//end loop

void moveForward(int x){ 

    digitalWrite(m1_AHI, LOW);
    digitalWrite(m1_BLI, LOW);
    digitalWrite(m1_BHI, HIGH);

    digitalWrite(m2_AHI, LOW);
    digitalWrite(m2_BLI, LOW);
    digitalWrite(m2_BHI, HIGH);

    analogWrite(m2_ALI, x);
    analogWrite(m1_ALI, x);
}//end moveForward
1

See Servo.cpp:

Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached. Timers are seized as needed in groups of 12 servos - 24 servos use two timers, 48 servos will use four.

Also Servo Library reference

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

You are using pins 9 and 10 for your motors.


analogWrite uses PWM, the servo library uses PWM, you have a conflict of resources here.

  • The servo's are not connected to PWM pins though. – Michael Rader Jul 26 '15 at 4:14
  • wow thanks that worked. My goodness I have been trying to solve this for a week. – Michael Rader Jul 26 '15 at 4:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.