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
asked Jul 26, 2015 at 3:34

1 Answer 1

2

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.

answered Jul 26, 2015 at 3:52
3
  • The servo's are not connected to PWM pins though. Commented Jul 26, 2015 at 4:14
  • wow thanks that worked. My goodness I have been trying to solve this for a week. Commented Jul 26, 2015 at 4:25
  • Strange with how many arduino robot car projects there are out there, how none I came across talked about this. I just happened to decide to 'upgrade' to controlling the speed, and ran into this (5 years after the original question) Commented Nov 15, 2020 at 19:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.