I have 2 DC motors (well, 4; but each pair is controlled by the same "signals") connected via a L298N, which is connected to the Arduino. In addition, I have a servo motor connected to the Arduino.

When I run the following program (in reality, it's a bigger program; however, I've managed to chiseled it down to the following):

#include <Servo.h>

const int servoPin = 3;
Servo servo;

const int LMotorsSpeedPin = 5;
const int RMotorsSpeedPin = 10;
const int LMotorDirectionBit1Pin = 7;
const int LMotorDirectionBit2Pin = 6;
const int RMotorDirectionBit1Pin = 8;
const int RMotorDirectionBit2Pin = 9;
const int MotorSpeed = 150;

void setup()
  // 1
  pinMode(servoPin, OUTPUT);

  // 2
  pinMode(LMotorsSpeedPin, OUTPUT);
  pinMode(RMotorsSpeedPin, OUTPUT);
  pinMode(LMotorDirectionBit1Pin, OUTPUT);
  pinMode(LMotorDirectionBit2Pin, OUTPUT);
  pinMode(RMotorDirectionBit1Pin, OUTPUT);
  pinMode(RMotorDirectionBit2Pin, OUTPUT);

  // 3
  digitalWrite(LMotorDirectionBit1Pin, LOW);
  digitalWrite(LMotorDirectionBit1Pin, HIGH);
  analogWrite(LMotorsSpeedPin, MotorSpeed);

  // 4
  digitalWrite(RMotorDirectionBit1Pin, LOW);
  digitalWrite(RMotorDirectionBit1Pin, HIGH);
  analogWrite(RMotorsSpeedPin, MotorSpeed);

  // 5

void loop() {}
  • 1 Sets up servo.
  • 2 Sets up L298N pins.
  • 3 Sets up left motor data.
  • 4 Sets up right motor data.
  • 5 Tests the code above for 10 [s].

I assume that you'll need some experience with the L298N; however, here's the gist.

*MotorsSpeedPin sends each motor what speed (MotorSpeed 0-255) they must go (via PWM). Data into a pair of *MotorDirectionBit*Pin tell each motor what direction to go (i.e. back 01, stop 00 11, right 10).

When I run the program above, only the left motor runs. However, if I comment out 1 (which essentially renders the servo useless), both left and right motors start to work.

I find this problem quite odd. What does the servo code have to do with the motor code (and only one at that!)?

1 Answer 1


The Servo library uses Timer 1 to create the servo control signal. That means it can run the servo on any pin or combination of pins, and at the 50Hz speed that a servo likes best.

However, pins 9 and 10 also use Timer 1 to create the PWM signal.

It can't do both.

As soon as you start using the Servo library you lose PWM on pins 9 and 10.

So what can you do about it? Simply move your pins around to different pins. You can't use 9 and 10 for PWM, but you can use them for the servo. So put your servo on pin 10 and your right motor speed control on pin 3 and all should be happy.

  • What's "Timer 1"?
    – Fine Man
    Dec 2, 2016 at 3:57
  • A bit of hardware in the chip that counts real fast.
    – Majenko
    Dec 2, 2016 at 9:16
  • Thanks. Are you aware of any good sources that explain timers and other topics like it?
    – Fine Man
    Dec 2, 2016 at 20:00
  • The datasheet for the ATMega328p is the source of all the information you could ever need.
    – Majenko
    Dec 2, 2016 at 20:00

Your Answer

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

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