I am writing my own instructions for a FPGA processor that I have made and I have a question about how to best produce a PWM signal from a PID control loop. I am confused how I should use the PID output signal to change the PWM duty cycle. I will explain how I plan on doing this and you can let me know if I am on the right track. Let's assume these inputs are 8 bits in size for simplicity.

So I have an analog input that will be connected to the FPGA through an ADC chip for the user to set the speed setpoint. The actual speed is measured via another analog signal and is also connected to the FPGA through another ADC chip. This gives binary values for setpoint and actual speeds somewhere between 0 and 255.

From these binary values, I can calculate the speed setpoint and the actual speed setpoint in my measurement term, which is RPM in this case.

As I will only use PI control, I can calculate the error value, find the integral value and finally calculate the output signal which will be in RPM.

Now I have a PWM generator block which has a triangular like waveform (basically a free running counter). I plan on feeding my calculated PI output signal into this PWM generator block and comparing the values cyclically in my program. If the output signal is less than triangular waveform, PWM signal is high etc. As the comparison value of one of the signals is changed, this will produce a changing PWM signal. (I know to cap the max PWM signal to whatever 100% when saturation occurs and to only change comparison value when counter is at max value to prevent false PWM signals).

Is there better ways to implement a PWM signal that is controlled via a PI control loop? I am interested to hear your thoughts?

Thanks guys

  • \$\begingroup\$ Questions here need to be able to have specific answers and at present yours cannot. Apart from the odd mention of a "triangular waveform" (a PWM signal is NOT an analog voltage, but digital with only two states!) what you are describing sounds generally sensible. You might have a look at the PWM modes of various MCU timer peripherals, since that's essentially what you are re-creating. \$\endgroup\$ Nov 12, 2020 at 16:57
  • \$\begingroup\$ @Chris Stratton that was kind of what I was asking. I am trying to find out if there are any other methods apart from using a triangular waveform and a comparator. (I do know a PWM is digital by the way). \$\endgroup\$
    – David777
    Nov 12, 2020 at 17:40

2 Answers 2


Sounds ok to me. I've implemented PID too for a power device using PWM. Ensure you're resetting the PWM at zero crossing. Also you might want to consider how frequently you can update the PWM value. I mean , the comparison in FPGA would be much faster than the loop. Consider soft-starting if you need.


Your plan seems sensible in general. I have constructed similar things and it works like you are describing.


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.