You could, just for the fun of it, use an oscillator that makes low THD sine waves and then use an integrator to make your comparator compare with a value that gives the right duty cycle.
Here is one example of it:
Link to simulator.
- The left graph = the gate of the NMOS
- The second graph = the out = output of the top op-amp (comparator, PWM)
- The third graph = the output of the bottom op-amp (integrator)
- The fourth graph = the voltage across the rightmost capacitor
The fourth graph shows that the integrator correctly outputs a value that results in the correct duty cycle according to the input voltage.
The 10 kΩ resistor and the capacitor on the right is not part of the actual circuit, it is only there for the fourth graph.
You can make the 100 uH coil with a piece of copper wire and a pen. This coil together with the two capacitors will set the oscillating frequency, in other words the PWM frequency.
In the real world there's no ideal capacitors and inductors and mosfets, so you will probably want to make the two capacitors in the oscillator equal. This will increase the voltage at the gate of the oscillator. I am using 1 kΩ pull up and pull down on the oscillator, you might need lower like 100 Ω or larger. This greatly depends on the ESR of the capacitors and inductor.
Problems:
The integrator is a part of a typical PID, I am not using the P or D part as you can see. This means that if your input is going to vary fast, then you will get major overshoots and more or less an instable system. So this will work good if you are using a potentiometer with your fingers, but horrible if you are using it in some feedback loop without a P or D part.
It is important that the voltage at the gate of the mosfet is within the voltage range of the integrators output.
Make sure that the inductor is an air core inductor, just wire it yourself. If you use any metal core then too much energy in the oscillator will be lost due to eddy currents and the oscillator will halt.