How to measure current through a mosfet with just a comparator? - Page 1
Forum section: electronics / beginners

author: mikewax
author_rank: Contributor
timestamp: November 01, 2020, 02:14:19 am
content: Here's a circuit i've designed for measuring approximate current flowing through a mosfet. I have a uC that has no ADC, it only has a comparator and an internal bandgap reference voltage. So i can't just connect the drain to an ADC input and divide the voltage by Rmosfet.So my (theoretical) measurement works as follows:initial: DIOc output low, comparator disabled, DIOd output low, DIOm output low.1) DIOd = Hi-Z,counter= 02) DIOm = high3) DIOc = input with pullup4) wait 4uS5) DIOc = Hi-Z6) enable comparator, if Vmosfet + Vdiode + Vcap > 1.1V reference, comparator will trigger7) check comparator status, disable comparator-8) If it didn't trigger, thencounter++ and goto step 3. If it did, DIOm = low, checkcounterand proceed with calculationVcap = Vsupply * (1 - e(-t/RC)) soVcap = (5 - Vdrain - Vdiode)(1 - e(-t/33k*1uF)) andt =counter* 4uS soVcap = (4.3 - Vdrain)(1 - e(-counter*4u/0.033)) soVcap = (4.3 - Vdrain)(1 - 1/e(counter*4/33k))this is where i'm kinda stuck. At this point I know the value ofcounter, but i still have two unknowns here.So i figure that i can just set DIOd = output low, and then just keep charging Vcap until the comparator triggers again. Then i can resolve the last equation and find Vdrain.So that's my theory. Does it hold water?

author: bob91343
author_rank: Super Contributor
timestamp: November 01, 2020, 04:24:22 am
content: I am finding it hard to follow your explanation. However, in my experience, comparators draw significant current so that might corrupt your scheme.

author: mikewax
author_rank: Contributor
timestamp: November 01, 2020, 04:45:51 am
content: Quote from: bob91343 on November 01, 2020, 04:24:22 amwell the datasheet for the uC i'm using doesn't say the input impedance of the comparator, but it does say that the "Analog Comparator Input Leakage Current" is less than 50 nA. Maybe that's the same thing.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 01, 2020, 04:48:42 am
content: Does it have an op-amp, or any sort of gain stage, or any sort of divider from reference to comparator?I think you'll find it's much easier to perform sigma-delta with a comparator, and maybe some external gain if nothing else is available (which could potentially be as simple as a single transistor, but an op-amp will give more precision and if you're trying to save FET dissipation, that may be necessary, i.e. because of the small voltage drop).Regarding your proposal:1. Why disable the comparator every time, then, effectively, strobe it just to make a check? How long does it take to start up? Often quite some time; during which the output may be unstable, or the offset may not be in spec, or something.2. Why not start a timer ticking, and sample it when the comparator triggers (whether read by polling or by interrupt)?3. If you can generate PWM, you can use that as a crude DAC, and with a voltage divider, sense the drain voltage; a successive approximation algorithm will take quite a long time to perform given the ample filtering a PWMDAC needs. But maybe that's good enough for a power limiting purpose.I think you will find the errors in the capacitor charging are too large to measure the tiny drain voltage under it. The diode also has a tempco, and also has capacitance and reverse recovery that transmit rough switching edges directly into port pins as shown.Tim

author: Doctorandus_P
author_rank: Super Contributor
timestamp: November 01, 2020, 05:41:48 am
content: I think you're making an error.In step 6). there is no current through the diode, so there is also no voltage over it.I think you'll have more success with a different circuit, at least if both pins of the comparator are acessible, instead of comparing to a fixed reference voltage.First replace the diode with a resistor to connect the Drain to the comparator input.Resistance should be high enough to to limit the current to a mA or so, and let the ESD diodes of your uC suck up a bit of current.This method is even used for some application notes for 0 crossing detection of mains voltage.(Or add a skottky diode to +5V if you do not trust it)During measurement there wil be no current through the resistor, so no voltage differential.Then use the capacitor between GND and the other comparator input.Then thoroughly discharge the capacitor, then use the pullup resistor to charge the capacitor until the comparator flips.The pullup resistor may have a high tolerance, an external resistor is probably better.Also: In step 8 you go back to 3. Is this a software loop?It would be a lot better if you let the circuit do it's thing with a running timer and then let the comparator trigger a capture event.What is the voltage range you expect over Rds_on?Rds_on is also not a well defined parameter. It changes with each MOSfet and also depends on temperature and Ugs. Maybe add a resistor to get a bit higher signal to measure?What microcontroller are you using? (And what are the capabilities of it's comparator?With the circuit you are using, a better method is probably:1). Turn the MOSfet on.2). Both DIO_D and DIO_c low to discharge the capactior.3). DIO_D High_Z and DIO_c High to start charging the capacitor and start a timer.4). Enable comparator, and let it's hardware trigger a capture event.5). Your timer value will get shorter with more current through the MOSfet.Note there is a "critical section". Your software may not be interrupted between steps 2 and 3.

author: mikewax
author_rank: Contributor
timestamp: November 01, 2020, 06:43:37 pm
content: well i'm using an ATMEGA16u2, no ADCs, what it does have is:so yeah i had kind of a senior moment, there isn't any reason to turn the comparator off and on. I can just start a timer and wait for it to trigger.Quote from: T3sl4co1l on November 01, 2020, 04:48:42 amyes i have to get a 0.1% cap, and i have a thermometer. I'm also going to calibrate the circuit by making several measurements with different controlled temp and Imosfet values.Quote from: T3sl4co1l on November 01, 2020, 04:48:42 amnever thought of that. It's possible but yeah time is definitely a factor. i'll look into that option

author: mikewax
author_rank: Contributor
timestamp: November 01, 2020, 09:19:36 pm
content: Quote from: Doctorandus_P on November 01, 2020, 05:41:48 amDOH that's true. And it's obvious to me now that i don't need to have a looping function and can do what you described. But i don't get the point ofQuotewhy is that?

author: mikewax
author_rank: Contributor
timestamp: November 01, 2020, 09:49:54 pm
content: OK this (i think this is what you meant):does look like a much better option. But i don't get this part:"let the ESD diodes of your uC suck up a bit of current" How do ESD diodes work?Of course it is a fairly fine measurement, since Vdrain will be somewhere between 33 and 350 mV, but i am going to do a series of measurements controlling for temp and Idrain in order to calibrate the circuit and measure the exact C and Rpullup and Rds and maybe the Ktemp for Rds and C as well."Rds depends on Ugs." dunno what that is either. Please pardon my learning curve.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 01, 2020, 09:56:59 pm
content: Ah yeah, some kind of signal into AIN0 then, could be a ramp for example. You can RC filter from GPIOx to AIN0, and set its value always complementary to the comparator output (using a comparator change interrupt I suppose). Whenever the comparator goes high, reset a timer. Whenever it goes low, sample the timer count. The ratio gives the PWM%. Or if the timers can do PWM capture directly then do that. (Don't think the simple MEGAs have that? XMEGA and the newer MEGAxxxx I think, have PWM capture, and an event system to wire comparator to timer so it all works in hardware.)As mentioned, other interrupts will interfere with the measurement; you may want to add a filter to the sampled values, a median filter for example.Or just go to a whatever has ADC, like a 16u4. It's not like they're expensive.Quote from: mikewax on November 01, 2020, 09:49:54 pmFig. 4 for example:https://www.onsemi.com/pub/Collateral/NDT3055L-D.PDF'U' being European convention for 'V'.Tim

author: Doctorandus_P
author_rank: Super Contributor
timestamp: November 01, 2020, 10:35:43 pm
content: So most uncertainties of my post you already (guessed?) right or have been answered by others.Except the ESD diode part.As you are using an AVR processor, have a look at:http://ww1.microchip.com/downloads/en/AppNotes/Atmel-2508-Zero-Cross-Detector_ApplicationNote_AVR182.pdfThen combine that with the assumption that your MOSfet is switching a 12V load. In that case there will be a current of around 1.5mA through the 4k7 resistor during the time that the MOSfet is off, and it's drain is pulled high through the internal resistance of the load.

author: mikewax
author_rank: Contributor
timestamp: November 01, 2020, 10:49:50 pm
content: Quote from: T3sl4co1l on November 01, 2020, 09:56:59 pmYeah that's another idea that i would never have thought of. And yes the timers can do PWM capture. As for the ATMEGA16u4, yes it has ADC but it's a QFN44. There were several reasons why we chose the 16u2, that was 6 months ago and long before the PCB layout was finalized. And that's why i'm here asking for advice, because there's little PCB space left and i gotta do the current measurement with fewer than 4 components. And now it looks like you and Doctorandus have solved my problem so MUCH OBLIGED fellas

author: mikewax
author_rank: Contributor
timestamp: November 01, 2020, 11:08:48 pm
content: Quote from: Doctorandus_P on November 01, 2020, 10:35:43 pmOK i'll read that doc but i think i got your point. ESDs clamp the overvoltage.But i should've said that the load is only 5V so do i really need that resister at all?

author: Doctorandus_P
author_rank: Super Contributor
timestamp: November 02, 2020, 01:08:06 am
content: Maybe you can get away withhout the resistor, but then you're eating into your safety margins.If you create a direct connection between fast switching high currents and weak uC inputs there are a lot of things that can potentially go wrong, and even with the resistor you're still within your 4 acceptable parts.For pretty much the same reason I prefer to use a series resistor in the Gate to the Fet. It makes the Fet switch a bit slower (Better for EMI and less problems with (track) inductance, and in case the gate of the MOSfet gets damaged it's an extra layer of protection for the uC.Things like that may be the difference between something that works during development on your desk, but fails in the field versus a reliable product.Out of curiousity:What kind of accuracy are you aiming for?You've got a lot of variables that introduce errors.For example how do you want to estimate the Fet resistance when it heats up due to the current flowing through it? A "nearby" thermometer probably does not measure the temperature of the Fet silicon.Make a list of all those variables, and try to estimate the spread of those parameters.

author: mikewax
author_rank: Contributor
timestamp: November 02, 2020, 01:55:45 am
content: i actually do have a gate resistor it's just not included in the diagram.and i don't require much accuracy. +-5% is fine for what i'm doing. Plus the measurement routine only executes once upon power up. and it only takes like a millisecond.it measures before it ships and stores the result in eeprom. then it measures every power up. and it also could keep track of it's own operation and collect statistics i suppose.But i'll have a whole slew of test protocols to "profile" each unit to determine the exact input pullup resistence, capacitance, Rdson, series resistor, with controls for temp, exact voltage, etc. I can figure out coeficients and everything for each one. temp hysteresis, whatever.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: November 03, 2020, 07:38:36 am
content: 5% seems pretty ambitious for a setup like this. Something between 10% and 20% is probably more realistic.A part of your problem is that you are using the MOSfet in a way that is not well specified in the datasheet.Part-by-part calibration will improve your accuracy, but you still have to cope with changing channel resistance with temperature, and unpredictable parameters such as aging of the FET. With a Drain voltage starting from 33mV (your quote) you already have a weak signal to start your measurement. Measuring this with 5% accuracy with a simple ADC such as this is already doubtful, and that is without the other parameters changing.

author: mikewax
author_rank: Contributor
timestamp: November 04, 2020, 02:18:55 am
content: Quote from: Doctorandus_P on November 03, 2020, 07:38:36 amhmmm... that's better than nuthin although in the end it might not be good enough. Well i'll just have to do my best with it and time will tell.it might eventually be necessary to do a revision of the whole PCB with more parts.much obliged guys for the assistance