1

I have a following code

  #include <Servo.h> //library for servo drive
  int MotorAPinA = 9;   //pin number for DC drive
  int MotorAPinB = 10;  //pin number for DC drive
  Servo myservo; //create object myservo, class servo from Servo library

void setup() {
  // put your setup code here, to run once:
  pinMode(MotorAPinA, OUTPUT);
  pinMode(MotorAPinB, OUTPUT);
  //myservo.attach(6); //pin number for servo output
  myservo.write(65);
}

void loop() {
  analogWrite(MotorAPinA, 240);
}

This very code works only partly.
If I comment line

myservo.attach(6)

then I got my DC drive working.
If I uncomment this line, my Servo goings crazy (effect looks like myservo.write(0) constantly), until I comment line

analogWrite(MotorAPinA, 240);

and then I got my servo working properly. While myservo.attach(6) is uncommented, DC drive doesn't work (with uncommented analogWrite).

So, basically, I can have or PWM or Servo working simultaneously, but not both

Any ideas? Arduino Uno R3, Arduino 1.8.1.

1

Well, your PWM outputs are both on 16b Timer/Counter 1 and the servo is attached to the 8b Timer/Counter 0. So my guess is that 8bit counter has too low resolution for driving the servo and it uses Timer/Counter 1 in full 16b mode for better precision instead (PWM uses just 8bits from 16). Also Timer/Counter 0 is used for counting millis, so its settings can't be changed anyway.

For the confirmation you can either:

  • use pins 5 and 6 (TC0) or/and pins 3 and 11 (TC2) and for the servo use pin 9 or 10
  • You can read the values of TC1 configuration registers and according to the datasheet you can figure out what mode is currently used.

EDIT: So on the Uno board is used Timer/Counter 1 for timing purposes:

  if(timer == _timer1) {
    TCCR1A = 0;             // normal counting mode
    TCCR1B = _BV(CS11);     // set prescaler of 8
    TCNT1 = 0;              // clear the timer count
    TIFR |= _BV(OCF1A);      // clear any pending interrupts;
    TIMSK |=  _BV(OCIE1A) ;  // enable the output compare interrupt
    timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service);
  }

So no PWM on the OC1A nor OC1B in normal mode and the channel A is taken by Servo.

2
  • ` #include <Servo.h> //library for servo drive int MotorAPinA = 6; //pin number for DC drive int MotorAPinB = 5; //pin number for DC drive Servo myservo; //create object myservo, class servo from Servo library void setup() { // put your setup code here, to run once: pinMode(MotorAPinA, OUTPUT); pinMode(MotorAPinB, OUTPUT); myservo.attach(9); //pin number for servo output myservo.write(65); } void loop() { analogWrite(MotorAPinA, 240); //analogWrite(MotorAPinB, 0); } ` somehow `` doesn't code code
    – Areso
    Feb 12 '17 at 13:27
  • Now it works. Servo on Timer/Counter1 16 bit (10 and 9 pins), PWMs on Timer/Counter0 0 bit (6 and 5 pins), and there are still free 11 and 3 pins of Timer/Counter2
    – Areso
    Feb 12 '17 at 13:30

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.