# Help needed with PWM and Op-Amp (for Arduino)

Joined Feb 8, 2012
16
Hello All,
In my project I want 0 to 5 VDC output to generate 4-20 mA signal. This signal is finally triggering an Industrial thyristor unit.
I am using Arduino Mega2560(v3) (digital Pin9) to generate PWM signals. These signals are fed into an OpAmp (LM358) you can find the reference circuit at
http://henrys,bench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/
I have implemented the circuit and now checking with the outputs.
On 100% duty cycle I am getting around 3.8VDC output at Arduino Pin 9 and thus it carries over to rest of circuit resulting only 16.16mA signals instade of 20mA.
I checked with other duty cycles too but result is same.
(Please read colums in order of, Duty Cycle -- Actual Voltage o/p at Arduino Pin9 -- Actual Final current O/p -- Expected Arduino o/p V -- Expected Final Current O/p
10% -- 0.38VDC -- 1.60mA -- ~0.50V -- ~5.6mA
25% -- 0.95VDC -- 6.80mA -- ~1.25V -- ~8.0mA
50% -- 1.90VDC -- 9.12mA -- ~2.50V -- ~12mA

What I cant figure out is If I run the arduino without any load (or with small load of RC Filter with LED at output) I can read correct voltage on Pin9 but as I connect OpAmp circuit The allover there is a voltage drop of 36%.

#### #12

Joined Nov 30, 2010
18,210
First, it is impossible to measure a PWM signal as a DC voltage with a meter. You are better off trying to measure at the op-amp because your RC has integrated the PWM signal.
Second, you can add gain to the op-amp by adding a voltage divider in the negative feedback line.
Third, your load is not shown and you might be asking too much from the op-amp. The LM358 isn't guaranteed to get any closer to the supply voltage than Vcc-2V with a 20 ma load.
If the load isn't the problem, you can put a 4.7K resistor in the negative feedback line and add a resistor to ground from the inverting input pin of size 14388 ohms (use 15K).
I think the load vs power supply voltage is the problem.

#### Attachments

• 282 KB Views: 11

Joined Feb 8, 2012
16
First, it is impossible to measure a PWM signal as a DC voltage with a meter. You are better off trying to measure at the op-amp because your RC has integrated the PWM signal.
I am completely agree with you and have not doubts at all. But if you look towards the functionality of the circuit you will understand it just converts the PWM signals to variable dc voltage. As per my small understanding of Arduino documents It is very clear that at analogWrite(##,255) will generate a PWM signals at 100% duty cycle. That means you can measure +5VDC over a multi-meter. and analogWrite(##,128) will cause to effectively read +2.5VDC (w.r.t. ground). I think the OpAmp part of the circuit is just working fine, I am getting the exact value at output of OpAmp which I can read after the RC Filter part (the voltage follower application).
Second, you can add gain to the op-amp by adding a voltage divider in the negative feedback line.
I don't want to have gained signals as my load ( a 0-5V to 4-20 mA converter) will not allow that.

Third, your load is not shown and you might be asking too much from the op-amp. The LM358 isn't guaranteed to get any closer to the supply voltage than Vcc-2V with a 20 ma load.
My Vcc at LM538 is 12VDC and I am expecting 5VDC output against 5VDC input which are will in limits of LM358, nothing extra.

#### Attachments

• 14.5 KB Views: 13
Last edited:

Joined Feb 8, 2012
16
Board cutshot attached

#### Attachments

• 14.5 KB Views: 11

#### #12

Joined Nov 30, 2010
18,210
I want 0 to 5 VDC output to generate 4-20 mA signal.
On 100% duty cycle I am getting around 3.8VDC output
resulting only 16.16mA signals instead of 20mA.
you can put a 4.7K resistor in the negative feedback line and add a resistor to ground from the inverting input pin of size 14388 ohms (use 15K).
I don't want to have gained signals as my load ( a 0-5V to 4-20 mA converter) will not allow that.
Well...I'm stumped.
Apparently I am focusing on the wrong part of the problem.
I have a dead car to fix today.

#### dannyf

Joined Sep 13, 2015
2,197
What I cant figure out is If I run the arduino without any load (or with small load of RC Filter with LED at output) I can read correct voltage on Pin9 but as I connect OpAmp circuit The allover there is a voltage drop of 36%.
I am getting the exact value at output of OpAmp which I can read after the RC Filter part (the voltage follower application).
With that, it is hard to understand what problem you are trying to solve.

Joined Feb 8, 2012
16
With that, it is hard to understand what problem you are trying to solve.
On 100% duty cycle I am getting around 3.8VDC output at Arduino Pin 9 and thus it carries over to rest of circuit resulting only 16.16mA signals instade of 20mA.
I checked with other duty cycles too but result is same.
(Please read colums in order of, Duty Cycle -- Actual Voltage o/p at Arduino Pin9 -- Actual Final current O/p -- Expected Arduino o/p V -- Expected Final Current O/p
10% -- 0.38VDC -- 1.60mA -- ~0.50V -- ~5.6mA
25% -- 0.95VDC -- 6.80mA -- ~1.25V -- ~8.0mA
50% -- 1.90VDC -- 9.12mA -- ~2.50V -- ~12mA

My problem is there is always a voltage drop of 1.2V at any duty cycle.

Last edited:

#### dannyf

Joined Sep 13, 2015
2,197
On 100% duty cycle I am getting around 3.8VDC output at Arduino Pin 9
Why is that a problem?

It seems perfectly consistent with the rest of your measurements.

Joined Feb 8, 2012
16
I think this all related to max current o/p of arduino pin (40mA max)
will try to put one more OpAmp before RC Filter

Joined Feb 8, 2012
16
I think this all related to max current o/p of arduino pin (40mA max)
will try to put one more OpAmp before RC Filter
Why is that a problem?

It seems perfectly consistent with the rest of your measurements.

In my very first post I have already mentioned about the usage of OpAmp output. I need 0 to 5V output to generate 4-20mA signal.

#### dannyf

Joined Sep 13, 2015
2,197

Joined Feb 8, 2012
16
then amplify it.
@dannyf
With due respect to your experience, I will ignore all your posts as it seems you only have interest in increasing your message count.

#### BR-549

Joined Sep 22, 2013
4,938
"On 100% duty cycle I am getting around 3.8VDC output at Arduino Pin 9 and thus it carries over to rest of circuit resulting only 16.16mA signals instade of 20mA."

The zero reference for a 4-20 ma signal is 4 ma., if that helps.

So your current span is 16 ma. From 4-20 ma.

#### eetech00

Joined Jun 8, 2013
1,966
It sounds like the Adruino output is not 5vdc.

Disconnect R1 from the Adruino and apply +5vdc to the disconnected end of R1.
Measure the output of the opamp and see if the output is at 5vdc.

Also, what is the output frequency of the Adruino? It might be that C1 doesn't have enough time to charge to 5vdc. With the R1/C1 combination your using, the cap will need at least 235mS to charge to 5v.

Last edited:

#### ronv

Joined Nov 12, 2008
3,770
@dannyf
With due respect to your experience, I will ignore all your posts as it seems you only have interest in increasing your message count.
Do you have 12 volts to power the op amp like in the schematic?

Joined Feb 8, 2012
16
It sounds like the Adruino output is not 5vdc.

Disconnect R1 from the Adruino and apply +5vdc to the disconnected end of R1.
Measure the output of the opamp and see if the output is at 5vdc.
Tested that way, and the result is 5V out put as expected. That is what I said earlier, there is no problem at voltage follower circuit but Arduino.
As mentioned in my earlier post I made some changes (i.e. have introduced one more opamp before RC filter) but it resulted nothing positive.

Joined Feb 8, 2012
16
Do you have 12 volts to power the op amp like in the schematic?
Yep, I have

#### ronv

Joined Nov 12, 2008
3,770
Yep, I have
It almost sounds like a bad 358, but...
You might try grounding the unused input on the 358. Then set the duty to 100% and measure every pin on the 358.
Next option to try is 4.7 k from +5 to pin 3 of the 358 with nothing else connected to it.

#### eetech00

Joined Jun 8, 2013
1,966
Tested that way, and the result is 5V out put as expected. That is what I said earlier, there is no problem at voltage follower circuit but Arduino.
As mentioned in my earlier post I made some changes (i.e. have introduced one more opamp before RC filter) but it resulted nothing positive.
Check this sim. The Adruino output pulse width needs to be longer than 235mS.

#### Attachments

• 48.9 KB Views: 11

Joined Feb 8, 2012
16
Check this sim. The Adruino output pulse width needs to be longer than 235mS.
Arduino Mega2560 has default PWM frequency near about 490 Hz. It comes around 2mS. If you can do cap calculations you can find the cap will charge to 5V in roughly about 1 sec with 2mS pulse width and 100% Duty. It no where needed to charge the cap in one cycle.

It almost sounds like a bad 358, but...
You might try grounding the unused input on the 358. Then set the duty to 100% and measure every pin on the 358.
Next option to try is 4.7 k from +5 to pin 3 of the 358 with nothing else connected to it.
BAD 358? I doubt. I still try to check with your first suggestion to tie everything floating to ground cause it makes sense to me. I didn't understand the second one. Please elaborate.