While learning more about Servos, the main thing i have learned is that, Servos (SG90) need PWM signal with the frequency of 50Hz. And the duty cycle of the frequency is used to rotate the angle of the motor.

I know generating PWM with variable duty cycle is possible in PWM pins using Timers but i don't understand how the arduino Servo.h library makes digital pins act like PWM too , whenever i connect the servo to any one of the digital pins of the arduino and code accordingly.

So how does Arduino 'Servo.h' Library manage to make a digital pin act like a PWM and what should the native AVR C code for this action look like?

I am trying to avoid third party libraries as much as possible and trying to learn Embedded systems thoroughly.

Please do not suggest big banging PWM method.


  • 2
    The source code for the Arduino libraries is available, just go look there to find out how it does it.
    – Mat
    Dec 29, 2021 at 6:48
  • Please do not suggest big banging PWM method ... why do you say that if you do not know how the library does it?
    – jsotola
    Dec 29, 2021 at 17:59
  • @jsotola Sometimes people advise using alternatives when they too don't have a proper knowledge so, to them only, i would prefer not to be recommended bit banging method (alternative).
    – CoderBittu
    Dec 30, 2021 at 18:19
  • @CoderBittu you asked how the library works ... there is no recommendation to be made ... there are no alternatives ... you did not ask for suggestions about how you should do it
    – jsotola
    Dec 30, 2021 at 23:01

1 Answer 1


I believe the servo library uses one (or more depending on board architecture) inbuilt 16 bit timers (timer1 on the UNO) to produce PWM directly on a digital pin using a low level equivalent of a digital write. The direct use of the timer prevents the use of that timer for other purposes like analogWrite().

In comparison a frequently used, more user-friendly method of producing PWM on Arduino is to use analogWrite() on designated PWM pins only.

The servo library source code is on github here.

You may have already answered your own question, as you did mention the bit-banging PWM method. That's what it looks like to me, but I'm not an expert on lower level programming.

See also: How can the Arduino Uno support up to 12 servos if it only has 6 digital PWM pins?

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