Using AnalogWrite and Servo at the same time

Hi,

I have a simple code for driving the speed of a motor via a L298N shield and moving a servo motor.

I have the following issue:

  • When the code for the servo is commented, analogWrite is producing the expected pulse.
  • When the code for the servo is not commented, analogWrite is only setting the pin to HIGH when value is 255. (all other values set the pin to LOW all the time).
#include <Servo.h>

#define PIN_PAN 5

#define PIN_IN1 10

Servo pan;

void setup()
{
  pan.attach(PIN_PAN);  // analogWrite works as expected when commented
  pan.write(90);  // analogWrite works as expected when commented

  pinMode(PIN_IN1, OUTPUT);
}


void loop()
{
  analogWrite(PIN_IN1, 100);
  delay(1000);
  analogWrite(PIN_IN1, 255);
  delay(1000);

}

I've done my verification with a logic analyzer and pulseview pulseview - Album on Imgur

Any idea how to have both the Servo and analogWrite working as expected as the time ?

Thanks,

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. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.

It is working as expected.

Oups, I should have RTFM more carefully.

Thanks.