# Measuring current in a PWM signal.

#### MB107

Joined Jul 24, 2016
304
I have an application for an automotive radiator cooling fan. For the moment I am using a Arduino derivative called a Robo-Red. It is pretty much the same as an Uno but can handle higher voltage inputs and more power output. I have the system working fine on the bench, with a 5V PWM signal off pin 9 of the Robo-Red, driving a Mercedes W203 fan directly from pin 9 PWM output. The Mercedes Benz fan uses a 10Hz. 0 - 5V PWM signal. A deviation from that signal will cause the fan controller to run at full speed by taking 12V DC power directly from the ignition switch. The logic behind that is buried inside the Bosch fan motor. The fan motor itself is a 600W motor with two large 6 ga power cables coming into it. and two smaller cables for +12 emergency mode signal and 0 - 5V PWM signal.

But the output current of the PWM signals off any of the Arduino like products is limited and I would like to know how close I am to those limits. Therefore I want measure the PWM current. In an attempt to do this I tried to install a 1 Ohm shunt resistor in line with the fan. The fan still runs fine with the 1 Ohm resister in place but as soon as I connect the leads to the O scope the fan runs away in emergency mode. I can successfully run the fan with the shunt resister connected to a DC volt meter. With the DC voltmeter I am recording 13 mV which will equate to 13mA with the 1 Ohm resister. But the wave form is PWM so the voltmeter will only average. I would like to use the O scope to get the max current. The shunt resistor is shown on the bottom right of the diagram.

Anyone know whats going on her or how this measurement can be made.

#### Irving

Joined Jan 30, 2016
3,300
You need to use two probes and use the 'scopes A-B math function to give the differential voltage across the shunt. You were connecting scope ground to one end of the shunt which killed the PWM.

The RMS value of a PWM = √D where D = duty cycle...

#### MB107

Joined Jul 24, 2016
304
You need to use two probes and use the 'scopes A-B math function to give the differential voltage across the shunt. You were connecting scope ground to one end of the shunt which killed the PWM.

The RMS value of a PWM = √D where D = duty cycle...
Thanks for the reply.

I set the signal up to 5V PWM at 50% duty cycle. I tried that and it works, but I got a higher that expected value ~240mv = 240ma
I haven't figured out how to do math yet on the O scope but reading the cursors.

Input side of resister 5.28V high, 0.288V low
Output side of the resistor 5.52V high 0.528V low

Delta ~ 0.24V across 1 Ohm resistor. = ~ 240ma

Using the volt meter at 50% duty cycle 11.9mv, Sqrt(0.5) = 0.707

240ma seems kind of high but not unreasonable.
Not sure I understand what to do with the sqrt of D term 0.707.

And may I trouble you to answer why the scope has a problem and the volt meter does not. It seamed kind of weird to me just connecting the positive leads from the scope and letting the negatives get connected to nothing.

Does 240ma sound reasonable. I diden't think an Arduino could run that high.

I have a discrepancy between the voltmeter and the scope. It seams that for a 50% duty cycle PWM signal the RMS voltage should be exactly 1/2 of the peak voltage. That would give me about 24 mV peak about 1/10 of the scope readings. What am I doing wrong?

Last edited:

#### Irving

Joined Jan 30, 2016
3,300
240ma seems kind of high but not unreasonable.
Seems very high to me; a logic input would be <5v and less than a few milliamps. Maybe you're overdriving it; Are you sure its spec'd for a >5v input? and 5v relative to what?

Not sure I understand what to do with the sqrt of D term 0.707.
Vrms = √D x Vpk, ie a 5v square wave is ~3.53v rms - ie sqrt(0.5 x 0 + 0.5 x 5^2).
So 5v @ 25% PWM = sqrt(0 x 0.75 + 0.25 * 5^2) = 2.5v rms, and
5v @ 75% PWM = sqrt(0 x 0.25 + 0.75 * 5^2) = ~4.33v rms.

And may I trouble you to answer why the scope has a problem and the volt meter does not. It seamed kind of weird to me just connecting the positive leads from the scope and letting the negatives get connected to nothing.
That's because they aren't positive and negative, but signal and ground. The ground side, in an AC powered 'scope, is connected to mains protected earth as is the ground (-ve) side of your bench supply (or USB feed to the Arduino), so the ground connections of the probes are, near enough and at low frequencies, the same potential as your Arduino ground. That why your PMW stops when you use a single probe and its ground connection across that resistor. Its crucial to understand this ground aspect as connecting the ground side of the scope to something that isn't ground can be highly destructive and physically dangerous. A classic beginner's mistake is connecting a 'scope probe to the -ve DC side of rectified AC mains while testing the internals of a switch mode power supply (SMPS). But -ve DC isn't ground, its approx -150v below ground. So bang goes the scope probe and, if you're very unlucky, the input amp of the 'scope and possibly some components in the SMPS. This is an expensive mistake you only make once... To do ths sort of testing you need a 1:1 mains isolation transformer (so that the system under test can 'float' relative to ground) or a high-voltage differential probe, ideally both.

I have a discrepancy between the voltmeter and the scope. It seams that for a 50% duty cycle PWM signal the RMS voltage should be exactly 1/2 of the peak voltage. That would give me about 24 mV peak about 1/10 of the scope readings. What am I doing wrong?
A true RMS test meter will read correctly whatever the waveform shape whereas a cheap one is only usually correct for sine waves. Its possible your test meter is not up to the job - what model is it? Try using the AC volts range?

#### MB107

Joined Jul 24, 2016
304
Seems very high to me; a logic input would be <5v and less than a few milliamps. Maybe you're overdriving it; Are you sure its spec'd for a >5v input? and 5v relative to what?

Vrms = √D x Vpk, ie a 5v square wave is ~3.53v rms - ie sqrt(0.5 x 0 + 0.5 x 5^2).
So 5v @ 25% PWM = sqrt(0 x 0.75 + 0.25 * 5^2) = 2.5v rms, and
5v @ 75% PWM = sqrt(0 x 0.25 + 0.75 * 5^2) = ~4.33v rms.
So I have to make some assumptions in order to get this equation to work. Main question is you have a 0 multiplier in there that doesn't seem to effect the results in this case. This is the way I understand it.

So that makes sense in that it is in the operational range of the Arduino outputs of 40ma max 20ma recommended. You feel this is a bit high but keep in mind this is a used fan pulled from a junk yard. The values of 240ma I got with the scope still do not make sense.

That's because they aren't positive and negative, but signal and ground. The ground side, in an AC powered 'scope, is connected to mains protected earth as is the ground (-ve) side of your bench supply (or USB feed to the Arduino), so the ground connections of the probes are, near enough and at low frequencies, the same potential as your Arduino ground. That why your PMW stops when you use a single probe and its ground connection across that resistor. Its crucial to understand this ground aspect as connecting the ground side of the scope to something that isn't ground can be highly destructive and physically dangerous. A classic beginner's mistake is connecting a 'scope probe to the -ve DC side of rectified AC mains while testing the internals of a switch mode power supply (SMPS). But -ve DC isn't ground, its approx -150v below ground. So bang goes the scope probe and, if you're very unlucky, the input amp of the 'scope and possibly some components in the SMPS. This is an expensive mistake you only make once... To do ths sort of testing you need a 1:1 mains isolation transformer (so that the system under test can 'float' relative to ground) or a high-voltage differential probe, ideally both.
Not quite sure I understand all this but it will give me an idea of where to research. I'm sure there are plenty of videos available on it.

A true RMS test meter will read correctly whatever the waveform shape whereas a cheap one is only usually correct for sine waves. Its possible your test meter is not up to the job - what model is it? Try using the AC volts range?

#### BobTPH

Joined Jun 5, 2013
6,268
Use channels 2 and 3 and press the math button and choose - This will show you the difference. Your scope almost certainly will scale for the probes. Not sure how to set it though.

Edit: If it is like my Rigol, press the CH button for the channel and you will get a menu that allows you to set the probe to 1X 10Xor 100X, among other settings.

Bob

Last edited:

#### MB107

Joined Jul 24, 2016
304
Were you using x1 probes on a x10 setting? You could try a bigger resistor. Use a 1k. If its a 'logic' input it'll still work. The other thing to check is what voltage and current, if any, comes out of the fan's PWM input when its not connected to the Arduino? If its pulled up to 12v, even via a big resistor, then that invalidates the scope reading and would also explain why its >5v anyway.
1K or 470Ω resisters will not work. 330Ω or smaller will work. With the 330Ω resister measured voltage across the resister is 2.936 VDC and 0.2VAC. No voltage comes out of the fan control PWM input signal with it disconnected from the Arduino.

I'd use an optocoupler anyway. Automotive environment are full of high transient voltages and spikes that are deadly to an MCU!
Yes on the opticoupler. My feelings exactly.

#### MB107

Joined Jul 24, 2016
304
Use channels 2 and 3 and press the math button and choose - This will show you the difference. Your scope almost certainly will scale for the probes. Not sure how to set it though.

Edit: If it is like my Rigol, press the CH button for the channel and you will get a menu that allows you to set the probe to 1X 10Xor 100X, among other settings.

Bob
I found them they are set to 1X

I think I figured out the math looks like with the 330Ω resister in place at 50% Im getting 2.64V with the signal high and 4V when the signal is low. so 4.00/330 = 0.012 = 12ma. That's much closer to the 15.4 ma derived using the DVM. I bet if I tried to check the DVM with the 330Ω, I might come closer.

#### Irving

Joined Jan 30, 2016
3,300
You need to set, when disconnected, the 0v level of both CH1 (yellow) and CH2 (blue) to be the same and also the volts/div at 2v/div and then connect to the load resistor,#1 to the Arduino end. As far as I can see the yellow trace (#1) zero level is set to be at 1.6v on screen and 2v/div, giving 0 - 4.4v, and the blue trace (#2) zero level is off the bottom of the screen and at 1v/div its apparently varying between approx 3.3v and 7v (assuming 0v is just off screen). Assuming #1 is drive side and #2 is the fan side that suggests there is a pull-up inside the fan and the Arduino is sinking >= 3.3/330 or 10mA when pulling down and is being pulled up by a (12-3.30)/0.01 = 870ohm approx resistor internal to the fan.

#### MB107

Joined Jul 24, 2016
304
You need to set, when disconnected, the 0v level of both CH1 (yellow) and CH2 (blue) to be the same and also the volts/div at 2v/div and then connect to the load resistor,#1 to the Arduino end. As far as I can see the yellow trace (#1) zero level is set to be at 1.6v on screen and 2v/div, giving 0 - 4.4v, and the blue trace (#2) zero level is off the bottom of the screen and at 1v/div its apparently varying between approx 3.3v and 7v (assuming 0v is just off screen). Assuming #1 is drive side and #2 is the fan side that suggests there is a pull-up inside the fan and the Arduino is sinking >= 3.3/330 or 10mA when pulling down and is being pulled up by a (12-3.30)/0.01 = 870ohm approx resistor internal to the fan.
I think I got it. All scales the same at 2V/div and zeroed.

#### Irving

Joined Jan 30, 2016
3,300
OK that's much better.

So now we can see what's happening...

When the Arduino (yellow trace) pulls down to 0.25bv approx we have the fan input (blue trace) at 4v approx, and when its high, slightly over 5v, the fan trace is around 7.5v. Now an Arduino output cannot normally rise above 5v unless an external circuit pulls it up and thats exactly what you're seeing on the scope. The likely equivalent circuit is shown below; simple trial and error gives 680ohm, a standard value, as the pull-up resistor. Now this makes a lot of sense. In the automotive world very few circuits work by voltage levels; the vast majority use current sensing as its more immune to noise AND its fail-safe - an open circuit is easy to detect. Almost all signalling is done by 'pull down to ground' with a switch, a transistor or a MOSFET. The red trace shows the current in the pull-up resistor and I'll bet this is what the PWM circuit in the fan actually detects - which is also why it needs something below a 1k resistor to work.

If I'm right, then the circuit below will work perfectly...

#### MB107

Joined Jul 24, 2016
304
OK that's much better.

So now we can see what's happening...

When the Arduino (yellow trace) pulls down to 0.25bv approx we have the fan input (blue trace) at 4v approx, and when its high, slightly over 5v, the fan trace is around 7.5v. Now an Arduino output cannot normally rise above 5v unless an external circuit pulls it up and thats exactly what you're seeing on the scope. The likely equivalent circuit is shown below; simple trial and error gives 680ohm, a standard value, as the pull-up resistor. Now this makes a lot of sense. In the automotive world very few circuits work by voltage levels; the vast majority use current sensing as its more immune to noise AND its fail-safe - an open circuit is easy to detect. Almost all signalling is done by 'pull down to ground' with a switch, a transistor or a MOSFET. The red trace shows the current in the pull-up resistor and I'll bet this is what the PWM circuit in the fan actually detects - which is also why it needs something below a 1k resistor to work.

If I'm right, then the circuit below will work perfectly...
Hello Irving

I see you must have found my how to make the opticoupler work thread. I just came here to ask if you wouldn't mind joining that discussion but here you are. Anything you can do to help would be appreciated.

Once again many thanks.

I tried your suggestion in a building block approach. I tried the baseline configuration with no resistors. Then I added the 330Ω resistor to the input side only. Then another 330Ω resistor to the output side followed by a second 330Ω resister to the output side. I had to use 2 330's to get as close as I can to 680.

Results are as follows. Note I have the yellow cable of the scope on the Ardrino side and the blue cable on the fan side. Also not that in each trace the signal input to output is inverted.

Baseline No Resistors
Input 100mv to 4.98mv
Output 6.1V to 13.2V

330Ω resister on input only
Input 200mv to 4.38mv
Output 9.48V to 13.1V

330Ω resister on input and 330Ω on output
Input 300mv to 4.48mv
Output 9.6V to 13.1V

330Ω resister on input and two 330Ω resisters in series (660Ω total) on output
Input 200mv to 4.28mv
Output 9.61V to 13.1V

#### Attachments

• 2.8 MB Views: 2
• 2.8 MB Views: 2
• 2.8 MB Views: 2
• 2.8 MB Views: 2

#### Irving

Joined Jan 30, 2016
3,300
Ah, I think you missed my point. The pull-up resistor must already exist inside the fan, you don't need to add anything externally...

If you put a 330ohm resistor from fan PWM input to ground - nothing else connected - and power up the fan, what voltage do you measure across the resistor?

#### MB107

Joined Jul 24, 2016
304
Ah, I think you missed my point. The pull-up resistor must already exist inside the fan, you don't need to add anything externally...

If you put a 330ohm resistor from fan PWM input to ground - nothing else connected - and power up the fan, what voltage do you measure across the resistor?
If I'm understanding you right, you mean as I have done in the attached files.

Very similar to what I did to find the current through the biggest resistor that would run in series with the fan. So what I got was:

240mv to 5.16V on the input side (arduino side) of the resister (yellow)
3.84V to 7.48V on the output side (fan side) of the resistor (Blue).

Delta V low = Vin - Vout = -3.6V
Delta V high = Vin - Vout - 2.32V

Whats interesting about this is the voltage seen by the fan is 3.84V to 7.48V and the fan runs. If I just took my wave form generator and set it up to 3.84 to 7.48 square wave, I don't believe it would run.

#### Attachments

• 2.8 MB Views: 1
• 2.6 MB Views: 1

#### MB107

Joined Jul 24, 2016
304
Ah, I think you missed my point. The pull-up resistor must already exist inside the fan, you don't need to add anything externally...

If you put a 330ohm resistor from fan PWM input to ground - nothing else connected - and power up the fan, what voltage do you measure across the resistor?
Hello Irving

I find I need to apologies as I have mis led you on something. I believe the question was asked if I disconnect the fan and measure the voltage at the fan wire what is the voltage and I reported zero. It turns out it's 13.8V. Unfortunately I only discovered that when I was measure it with a scope and then discovered I had my DMV leads plugged in to the current sockets. This actually makes me feel a lot better about the opticoupler. When its off it's passing the fans normal 13.8V. When its on its pulling the fan voltage down to the 5V level of the source.

Sorry for the confusion.

#### Irving

Joined Jan 30, 2016
3,300
Hello Irving

I find I need to apologies as I have mis led you on something. I believe the question was asked if I disconnect the fan and measure the voltage at the fan wire what is the voltage and I reported zero. It turns out it's 13.8V. Unfortunately I only discovered that when I was measure it with a scope and then discovered I had my DMV leads plugged in to the current sockets. This actually makes me feel a lot better about the opticoupler. When its off it's passing the fans normal 13.8V. When its on its pulling the fan voltage down to the 5V level of the source.

Sorry for the confusion.
That's exactly what I was expecting. And if you do pull it down to ground using the current range on the multimeter what current do you get? I'm expecting about 20mA.

This is why its not a good idea to drive the fan input direct from the Arduino. When the Arduino output is high the fan is pulling it up to 13.8v and the only reason the Arduino output is just over 5v is the catch diode in the Arduino is clamping it to the 5v rail, as shown in the diagram below, which depicts a typical CMOS output stage driving the fan input. The danger is that if there's a big spike on the 13.8v rail that can have a detrimental impact on the internals of the Arduino. A transistor would remove that issue, but complete opto-isolation as per post #14 is the best protection.

#### BobTPH

Joined Jun 5, 2013
6,268
You need to connect the opto coupler collector to the blue wire and the emitter to ground.

and you need to connect the optocoupler anode to Arduino +5V and cathode to the Arduino GPIO.

Now, whe the GPIO is high, the opto is off and the blue wire is pulled up, so high. When the GPIO is low, opto is on so blue wire is pulled down.

This assumes the opto can sink the required current, which we still don't know.

Bob