Measuring voltage in a circuit with PWM

I have a very simple circuit, a battery that is dissipating to a simple resistor, controlled by a MOSFET that I'm PWMing, and a shunt resistor to measure current. My problem is that I can't properly measure the voltage across the shunt resistor with an analog pin because the circuit is PWMing... does anyone know of an approach to solve this?

Use a smoothing capacitor.

Post your complete circuit diagram for suggestions on how to include this.

Looks like this, shunt resistor is 0.1 ohm

Thanks

IMG_0152 (1).JPG

Nothing attached/included there.

Sorry new to the forums and exceeded my post limit while attempting to attach a file that was too large

The voltage in a PWM circuit is constantly changing, so the only "proper" way to measure it is to use an oscilloscope. But then, you will find that it switches constantly between about Vcc and 0.

If you want to measure an average voltage, then a voltmeter that is very slow to respond (like an old analog needle-type voltmeter) is another useful way.

Whether your multimeter can accurately read the average current would depend on the multimeter.

jremington:
The voltage in a PWM circuit is constantly changing, so the only "proper" way to measure it is to use an oscilloscope. But then, you will find that it switches constantly between about Vcc and 0.

If you want to measure an average voltage, then a voltmeter that is very slow to respond (like an old analog needle-type voltmeter) is another useful way.

I'm currently using a voltmeter to measure it, the issue is though i need the arduino to measure it properly because I need to implement it into a feedback loop for a project

Then the suggestion in reply #1 is appropriate.

An n-channel MOSFET should be on the other side of the load. The way you have drawn it you need a p-channel.
schematic.png
Use a 10µF cap (polarised is fine in this case). Forgot to mark that value in the image.

If you indeed have an n-MOSFET you have to use the schematic to the left. Assuming the PWM is produced by the same Arduino that has to measure the voltage, I'm not sure how to connect this.

The circuit as you sketched needs a p-MOSFET, as on the right. Then the connection to the Arduino is straightforward.

Of course you measure the average current, not the instantaneous current.

wvmarle:
An n-channel MOSFET should be on the other side of the load. The way you have drawn it you need a p-channel.
schematic.png
Use a 10µF cap (polarised is fine in this case). Forgot to mark that value in the image.

If you indeed have an n-MOSFET you have to use the schematic to the left. Assuming the PWM is produced by the same Arduino that has to measure the voltage, I'm not sure how to connect this.

The circuit as you sketched needs a p-MOSFET, as on the right. Then the connection to the Arduino is straightforward.

Of course you measure the average current, not the instantaneous current.

Hey, thanks a lot! I'll give it a shot. Also I am using an n-channel and it is hooked up properly, I just didn't write it in the diagram properly. Also what program do you use to draw up your schematics?

Edit: The battery is also 4.2V and not 42V, do the values of the RC smoothing circuit change?

That's done using KiCAD.

Just looking at the schematics again: with a little rearranging you can connect the shunt to the Arduino's analog in. This way one side of the shunt is always at GND potential. Forgot to change the battery voltage; it has no effect on the shunt's smoothing. Those values are RC time based and are more related to the PWM frequency.

schematic.png

The voltage over the shunt will be very low this way - use the internal reference (then you have a 1V full scale, rather than 5V). At a 0.1Ω shunt and a maximum current of about 4A, you get up to 0.4V out of it. You may consider a 2x or 2.5x OpAmp amplifier.

Also with your shunt being 10% of your load, you're going to lose quite some voltage there. Better get a smaller value shunt - 0.01Ω or so, but then you'll definitely need an amplifier, 20x would be good.

wvmarle:
That's done using KiCAD.

Just looking at the schematics again: with a little rearranging you can connect the shunt to the Arduino's analog in. This way one side of the shunt is always at GND potential. Forgot to change the battery voltage; it has no effect on the shunt's smoothing. Those values are RC time based and are more related to the PWM frequency.

schematic.png

The voltage over the shunt will be very low this way - use the internal reference (then you have a 1V full scale, rather than 5V). At a 0.1Ω shunt and a maximum current of about 4A, you get up to 0.4V out of it. You may consider a 2x or 2.5x OpAmp amplifier.

Also with your shunt being 10% of your load, you're going to lose quite some voltage there. Better get a smaller value shunt - 0.01Ω or so, but then you'll definitely need an amplifier, 20x would be good.

Thanks so much, few questions:

  1. does 4k7 mean 4700 ohm or 47000 ohm?
  2. What do you mean bu internal reference using 1V scale rather than 5V scale?

If the load on the circuit is a resistor, and if you know the value of the resistor, the voltages for a PWM HIGH and LOW, and the duty cycle of the PWM signal then you can work out the average current mathematically.

...R

jdburke:

  1. does 4k7 mean 4700 ohm or 47000 ohm?

The k is always in place of the decimal point so 4k7 = 4700.

  1. What do you mean bu internal reference using 1V scale rather than 5V scale?

analogReference()

So I implemented the RC circuit, and it helped a lot, but I still see some choppiness in the bit readings in the Arduino. Example: I'll see 52,52,52,34,42,47,52,52,52 and so on.

Is there a way to maybe change the values of the RC circuit so that it is more smooth? Or another method?

Edit: Here's an example of the serial output.
Vbits Vshunt Current
18.00 0.09 0.58
30.00 0.15 1.17
30.00 0.15 1.17
18.00 0.09 0.58
19.00 0.09 0.63
27.00 0.13 1.02
31.00 0.15 1.22
30.00 0.15 1.17
21.00 0.10 0.73
18.00 0.09 0.58
27.00 0.13 1.02
29.00 0.14 1.12
31.00 0.15 1.22
32.00 0.16 1.26

What voltage is your input at that moment? What resistor values do you use? 52 is very close to the bottom of the scale.

One way to stabilise readings is to take a number of readings and then the average, or you can add a capacitor to the analog pin (typically 10nF - 10µF depending on how fast you want the signal to react).