PWM affecting other pins

Hello,

My setup is pins 6,9,10,11 driving 2 motors via PWM. Pin 8 controls a servo and Pin 7 controls/reads from a Ping ultrasonic range finder.

I was excited to get my motors working exactly how I wanted them to but as soon as I added the servo and activated the pin ( myservo.attach(8); ), one of the motors stopped working. I removed the section of code and the motor started working again.

I’m beginning to realize that I can’t do PWM on 6 and use pins 7 and 8 for servo control.

Does anyone have any suggestions of which pins to use for servo and ping?

Many thanks in advance,
Erik

The Servo library uses one of the timers (Timer 1 maybe?) that is used by some of the PWM pins.

There is an alternative library called ServoTimer2 that uses Timer 2 instead of the normal. This will also disable some PWM pins, but maybe not the ones you needed? Make sure you read the thread thoroughly.

Thanks for the advice.

I’m going to try turning the servo without the servo.h library:

 int servoPin = 7;     // Control pin for servo motor
 int minPulse = 544;   // Minimum servo position
 int maxPulse = 2400;  // Maximum servo position

 void setup() {
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
 }

 void loop() {
  turnServo(45);
  delay(3000);
  turnServo(90);
  delay(3000);
  turnServo(135);
 }
 
 void turnServo(int value)
{  
    if(value < 0) value = 0;
    if(value > 180) value = 180;
    value = map(value, 0, 180, minPulse,  maxPulse);      
    for(int i=0;i<25;i++) {
      digitalWrite(servoPin, HIGH);   // Turn the motor on
      delayMicroseconds(value);       // Length of the pulse sets the motor position
      digitalWrite(servoPin, LOW);    // Turn the motor off
      delay(20);
    }
}