 # How to make an op-amp output PWM with duty-cycle control?

We can use a generic op-amp like the LM4562NA. We build an oscillator from the first op-amp in the IC. This produces a triangle-wave. We feed this wave into the second op-amp. We also add a controllable voltage from a potentiometer. Compare this against the triangle wave and we have PWM with a controllable duty-cycle.

## Schematic

This is the circuit we are building today. At each step I will explain what the components do.

## Connecting op-amp to power and ground

The op-amp needs power in order to function. The voltages we feed it are also the limits to what it can output. If we for example, intend to drive a MOSFET with the output signal, we could give it 12 volts.

But since this is only a demonstration, I will give it 5 volts.

## Adding resistor and capacitor to negative input

The op-amp can change it’s output very rapidly, but we want a triangle wave, whose signals rise and fall slowly. So we need to slow it down somehow.

The output of the op-amp first goes through a resistor to the negative input, to limit the current in a predictable way. Then we have it charge a capacitor, which takes time to charge and discharge.

## Adding feedback resistor to positive input

We connect the output terminal through another resistor to the positive input. The positive input now sees the same voltage as the output.

On its own this resistor is not that important, but that will change in the next step.

In order to oscillate, the inputs have to be able the overtake each other in terms of voltage. But the positive input will now be either the absolute maximum voltage of the op-amp, or the absolute minimum. No room left on either end.

So we add an equal value resistor divider to the same input. This divider will normally output a voltage right in the middle of the maximum and minimum voltage, in this case 2.5 volts.

But it is also connected to the output through another resistor. This now causes the output voltage to be pulled a bit towards the middle voltage, leaving room on either end.

## What will happen in this part of the circuit?

1. Both inputs and output are low. Noise will eventually cause the positive input to be bigger than the negative and the output goes high.
2. The output is high, and thanks to the tree resistors, the positive input is less than 100% of the output. The capacitor is charging and voltage on the negative input is rising.
3. The negative input will eventually rise above the positive input. Causing the output to go low.
4. The output is low, the three resistors make the positive input a bit above ground. The capacitor is discharging and voltage on the negative input is falling.
5. The negative input drops below the positive input. The output now switches to high again.
6. Continue the loop at point 2.

## Oscilloscope, lets see the results

There are a few things to observe here.

First; the output signal which is a square-wave signal.

Second; the positive input which is also a square-wave signal.

If I overlap those two signals I would expect to see the input signal is slightly dampened compared to the output. But it seems the difference is to small for that. But it is needed, that I am sure of.

Third; the negative input signal which is a triangle-wave signal.

Now if I move the positive input signal over the negative one, we can see the triangle-wave signal change direction as soon as it touches the square-wave signal. Exactly what we wanted.

## Feed triangle-wave to second op-amp

Now that we have a triangle-wave signal, we can use it to create a square-wave signal. Therefore we connect the negative input of the first op-amp to the positive input of the second op-amp.

Now we add a potentiometer, one end to 5 volts and the other to ground. This makes it a variable resistor divider.

The middle connection is the wiper of the potentiometer and now provides a select-able voltage between 5 volts and ground.

We connect this voltage source to the negative input of the second op-amp.

## Oscilloscope, lets see the results

There are a few things to observe here.

First; the output signal which is a square-wave signal. If I turn the potentiometer you can see the duty-cycle change. It can even go full on and full off.

Second; the positive input which is the triangle-wave we saw earlier.

Third; the negative input which is the potentiometer. You can see it move up and down, but is has no wave form.

Now if I move all these signals to the same place, you can see that the output goes high as soon as the triangle-wave signal goes over the potentiometer value.

The lower the potentiometer value, the longer the output stays on.