Arduino newbie here, so this question might sound too simple. I am trying to run a parallax continuous rotation servo using Arduino Uno. From the reference, I learned that the servo needs to be "attached" to one of digital pins with PWM capability, and the servo's speed can be controlled by write(0~180, 90=stop).

But I am having a hard time understanding the role of the PWM digital pin. If the servo's speed and direction of rotation is determined by write(), what is the role of attach(pin#)?

Thanks in advance!


attach tells the servo library which pin it should be writing to, this is stored somewhere in ram and write will look it up and communicate with the correct pin.

It's possible that you are using the other pwm capable pins for other purposes and suddenly having that library interfere with that is not a good thing.


First of all, you don't need to connect it to one of the PWM pins. Any digital output pin will work just fine.

Judging by the Arduino reference, attaching the servo to a pin is kind of like to declare a variable. By using myservo.attach(pin) you also give the servo a name (myservo in this case), and you can also add maximum and minimum values to the servo's rotation by adding two extra values to the end: myservo.attach(pin, min, max)

When you then use the myservo.write() function, the Arduino will "remember" what pin myservo is connected to, as well as the maximum/minimum settings for it.

  • Servos need the pwm on the control line, they compare the duty cycle value to the internal potentiometer and apply torque to correct any discrepancy Sep 2 '17 at 22:05
  • 1
    @ratchetfreak yes, but what I meant is that you don't have to use one of the pins with PWM labeled onto them. On the Arduino Uno, pin 3, 5, 6, 9, 10 and 11 are PWM pins, but you don't have to use one of these. Sep 3 '17 at 6:52

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.