Servo position (angle) control using PID controller

Hi every body.
I want to control the position (angle) of servo motor and I want to use the PID controller.
so I just used PID for velocity control of Dc motors and I don’t know how to use PID for angle control.
Do I need encoder?
How can I control the angel? does it have any dynamics of transfer function?
Is the input of the servo is PWM? so the PID generates PWM?
I’ll appreciate it if you help me.
Kind regards.

If you want to create an RC-type servo, you can learn a lot from the open servo project:


There are other open source servo projects as well (just do a Google search).
Ex: https://odriverobotics.com/

Q) Do I need encoder?

You need something to give you absolute positioning - normally a potentiometer or encoder

Q) How can I control the angle?

Your microcontroller will read the current angle, and (based on input) the desired angle. The PID algorithm within the microcontroller will effectively control the motor’s speed (the microcontroller will send velocity commands to the motor controller which controls the motor’s speed and direction).

Q) does it have any dynamics of transfer function?

Start with a PID and then refine it. You can get as complex as you’d like.

Q) Is the input of the servo is PWM?

If you’re creating a normal RC servo, then yes, the input is a timed 5V pulse which the microcontroller then interprets as a position.

Q) so the PID generates PWM?

The PID is used to provide speed and direction values to a motor controller. It does not have anything to do with the input (which only relates to the desired end position / angle).

Position Sensor -> Microcontroller (PID)
RC PWM Input -> Microcontroller (PID) -> Motor controller (PWM / Serial / I2C etc.) -> Motor

Hope this helps.

how programmming PID controller at servo UBTECH 12 HC with MPU6060?

thank you