# measure dc current in pwm circuit

Discussion in 'General Electronics Chat' started by mah, Dec 23, 2015.

1. ### mah Thread Starter Active Member

Mar 15, 2010
256
2
i control solenoid valve using pwm generated by MCU , and i used op-amp with shunt resistor to measure the current in solenoid. i will read the output of the op-amp by the MCU but the output isn't constant because of pwm so i want to average the reading . what are available solutions here?

2. ### dannyf Well-Known Member

Sep 13, 2015
1,607
317
Use a low-pass filter.

mah likes this.
3. ### crutschow Expert

Mar 14, 2008
12,538
3,072
More specifically, a simple 1st-order RC low-pass filter will give you a steady DC output proportional to the PWM average voltage.
For less than 1% ripple, use an RC time-constant of at least 100 times the PWM period.

4. ### mah Thread Starter Active Member

Mar 15, 2010
256
2
it is very slow in simulation i don't know if it will be the same in practical. isn't there any other method.?

5. ### crutschow Expert

Mar 14, 2008
12,538
3,072
A second or higher order filter will reduce the time for the simulation to settle for a given ripple output, but the time to do a simulation is not a good criteria for the design of a circuit.
The real-time for the first-order filter output to settle will be about 5 RC time constants of the filter, much shorter than the simulation time.

6. ### MrAl Well-Known Member

Jun 17, 2014
2,214
435
Hi,

Depending on the frequency of the PWM and the speed of the MCU you may be able to do it with a digitally (software) based filter. There are several ways to implement this though such as calculating the running average or starting with the Z transform of a good low pass filter, etc. With the Z transform method you can start with a regular frequency domain specified filter function and transform into a discrete digital version which would then be coded into the MCU.

A question you can also ask is how fast the PWM pulse widths change, not the frequency itself, and if they dont usually change that fast then you dont need a fast filter.

Since this is a solenoid and that will have some built in inductance, it is already part of a low pass filter if you are measuring current and exciting with a voltage. You might take advantage of that too.

You might want to state how fast you have to change the PWM level, and what the frequency of the PWM is, and if you know the normal running duty cycle if there is a normal duty cycle that is. Also maybe what the solenoid actually does in real life.

7. ### mah Thread Starter Active Member

Mar 15, 2010
256
2
PWM frequency is 5KHz and i use atmega 8 so how to take average in the code?.the solenoid is used in plastic blowing machines

8. ### Roderick Young Member

Feb 22, 2015
408
168
The low-pass RC filter is a good idea. However, may I suggest that if you have a known solenoid, that you can look at the voltage across your sense resistor with a scope, and thereafter will be able to blindly calculate the current from this profile. It will be an RL circuit, with the current ramping up and eventually leveling off if power is connected long enough (and it should be, if the solenoid is going to pull in). With a fast current sensing circuit, you might even be able to gather the data with the ATMega itself. Take a bunch of samples, and hopefully they will fall into two buckets - the ON current, and OFF current, with a few stragglers in-between. Mathematically, you could even develop the RL charging curve by analyzing the value and percentage of the stragglers.

Is the reason you want to know the current that you want to reduce power while the solenoid is holding? You could determine the PWM duty cycle needed to hold experimentally, then add a safety margin in implementation.

9. ### cmartinez AAC Fanatic!

Jan 17, 2007
3,523
2,342
This is my solution to the problem you're stating. Hope it helps.

10. ### mah Thread Starter Active Member

Mar 15, 2010
256
2
I don't want to measure it using oscilloscope , i want to use the atmega.

11. ### GopherT AAC Fanatic!

Nov 23, 2012
5,831
3,425
The benefit of using PWM for a motor, is higher torque and finer control. I imagine it is the same with a solenoid so I would not use a filter technique. As mentioned above, the coil in a motor or solenoid will act as a filter. Therefore, at 5 kHz, you can easily wait for the off-time in the duty cycle and measure the voltage in the back EMF of the coil with the on-board A/D converter. You may need a voltage divider (two resistors) to insure the voltage is in the range of your ADC. One warning, pick voltage divider resistors that balance the effect of too low of values draining back emf too quickly vs too high of resistance makes a high impedance voltage source and slow ADC acquisition.

There is a Microchip application note. I realize you are not using microchip but the technique is the same.

Last edited: Dec 27, 2015
cmartinez and Roderick Young like this.
12. ### mah Thread Starter Active Member

Mar 15, 2010
256
2
you mean to measure the EMF across the terminals of the valve? how will i select the off time ? if i could measure the emf , do you mean that it is proportional to the current so i can calculate the current? i think that will require knowing the constant.

Jul 18, 2013
10,251
2,265
The current wave form does not follow the voltage in a PWM controlled inductive device, it is more of a mean level saw tooth in nature.
Max.

14. ### crutschow Expert

Mar 14, 2008
12,538
3,072
The back EMF of the coil when the PWM signal is off will be the negative value of the forward voltage of the PWM free-wheel diode, so that is of little help in measuring the coil current.

Last edited: Dec 27, 2015