2

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.

Thanks...

CoderBittu
  • 21
  • 4

1 Answers1

0

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?

RowanP
  • 869
  • 6
  • 21