measure dc current in pwm circuit

Thread Starter

mah

Joined Mar 15, 2010
393
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?
 

crutschow

Joined Mar 14, 2008
34,284
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.
 

Thread Starter

mah

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

crutschow

Joined Mar 14, 2008
34,284
it is very slow in simulation i don't know if it will be the same in practical. isn't there any other method.?
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. :rolleyes:
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.
 

MrAl

Joined Jun 17, 2014
11,389
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.
 

Thread Starter

mah

Joined Mar 15, 2010
393
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
 

Roderick Young

Joined Feb 22, 2015
408
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.
 

GopherT

Joined Nov 23, 2012
8,009
I don't want to measure it using oscilloscope , i want to use the atmega.
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:

Thread Starter

mah

Joined Mar 15, 2010
393
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.
 

MaxHeadRoom

Joined Jul 18, 2013
28,619
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.
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.
 

crutschow

Joined Mar 14, 2008
34,284
........................
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.
.......................
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:
Top