current sensing, current measurement

Anything not related to STM32
Post Reply
ag123
Posts: 179
Joined: Thu Mar 07, 2019 6:15 am
Location: Singapore
OS: linux
IDE: eclipse, arduino 1.8.5
Core: Roger's
Board: Maple mini, Bluepill

current sensing, current measurement

Post by ag123 » Thu Sep 12, 2019 8:59 pm

this is really off-topic, but i've been caught in a weird situation for what i think is an 'easy' problem.
the problem is about measuring currents with stm32.

i found a lot of good materials on the web, e.g. this article from TI
http://www.ti.com/lit/ml/slyy154/slyy154.pdf

so i started with low side current sensing, and i started with LMV358 op amps
http://www.ti.com/product/LMV358
and i'm thinking low costs and LMV358 can be used for many things other than current sensing, so i got 10 pieces of that.
then as it turns out what i really want to do is to make a small bench power supply and LMV358 can at best do *low side* sensing.
i.e. i try to bump up the ground with a resistor so that i can measure it. then as i keep thinking, it turns out that if i've got 2 outputs and 2 grounds (that is somewhat connected) the output from 1st dc out can be shorted to the other gnd (so my simple minded ground resistor register 0 currents) !
LMV358 is a lousy choice for current sensing though it is rather 'cheap', one of the problems is that for that price TI don''t assure 0 offset, and if there is an offset it is trouble especially that i'm trying to measure currents wrt ground, i learnt i'd need to fix that by funneling voltages into that -ve terminal to raise it above zero etc

and i started learning about common mode voltage, so it turns out it is a real pain to try any form of high side current measurements with LMV358, it simply can't take that common mode voltage.even this simple minded circuit requires a op amp that can handle high common mode voltages close to vcc or higher
https://electronics.stackexchange.com/q ... ned/451673
slowly as i mine the web for ideas, it turns out i'd need to do some kind of current mirror
https://www.edn.com/design/test-and-mea ... de-current

to do that i'd easily need to add a bunch of transistors and resistors, wiring etc just to make it happen, let alone tuning it.
i ended up looking for high side current sensing which for one thing cost more than that modest LMV358, and it is specialised.
i.e. current sensing only (mostly)

TI has a part called INA180
http://www.ti.com/product/INA180
it is rather low cost as based on list prices INA180A2IDBVR 0.15 each for 1ku (1000 pieces), but i don't need 1ku !

and the usual place to look for 1-5 pieces and cheapest possible *hopefully*, and not always true - aliexpress
https://www.aliexpress.com/wholesale?ca ... ext=ina180
then come an interesting finding, aliexpress is 'flooded' with very specific sku
and searching for INA180 ends up with lots of INA219, some and much fewer INA199 and nearly *zero* INA180
i literally run the TI part numbers
http://www.ti.com/amplifier-circuit/cur ... ducts.html
and it turn out many of other skus are either costly on aliexpress or they simply return a blank

i disliked the INA219 though i bought 2 pieces of modules. they are I2C devices, which means i'd need to hack up an interface to talk to it.
though it seem like a pretty 'hot' module with the conventional Arduino Uno style devices. i'd guess it is mainly that ATMega328p has a 10 bit ADC.
so it still makes sense to use that INA219. there are libraries and more for INA219

but stm32 has 12bits ADC and if i'd use INA219, stm32's ADC is 'wasted', grudge !
and analog is pretty much easier than interfacing another i2c device.

then the other thing i've always wondered is what worth are those 50milli ohm or 100milli ohm cheap shunt wires, u'd find them searching them out on aliexpress, ebay etc
https://www.aliexpress.com/wholesale?ca ... shunt+wire
my solder blob on the PCB just need to be a couple hundred microns off some 'perfect' specs and that will throw those milli ohms out the window
i decided to use cheap nichrome wire instead, i.e. forget about those 50milli ohm or 100mili ohm 'shunts' that little piece or nichrome or constantan wire does just as good a job as those advertised purpose build wires just without the mili ohms specified.
so how do you measure that mili ohms?

it brought me into a big detour into the land of kelvin bridges etc. which for that matter, i doubt i can make one to sufficient precision and accuracy that would offset all that errors. i'm measuring 50 millohms - 100 milliohms

so here is some thoughts:
1) i decided to go with INA199 (no I2C just analog !)
http://www.ti.com/product/INA199
TI seem to pretty much assure a gain of 50 for INA199x1: 50 V/V
that's about the only sku i found on aliexpress meeting those *analog* (no I2C) specs and is about the cheaper ones among the INAxxxx i found on aliexpress.

2) then we need a table of resistivity
https://www.thoughtco.com/table-of-elec ... ity-608499
i worked the numbers and it turns out for AWG 30 nichrome wire (0.01", 0.255mm diameter)
using nichrome resistivity as 1e-6 ohm.m (but, i read somewhere constantan has better temperature characteristics)
100 milliohm works out to about 4.9mm (~5mm or near 0.2" ~193 mils) of wire ! (about 2x 1/10" header spacing)
just that this isn't exact, the solder blob errors etc would change much of that

3) what i'd do next is this, i think i can get some rather high precision resistors e.g. 0.5% or less say 1 ohm, 10 ohm, 50 ohm, 100 ohm etc
the adc can probably measure *voltages* rather accurately, but i'd guess with 100 milli ohm, 50 milli ohm as shunts resistors in series, it would pretty much be the whole Vcc right across my 'high precision' series resistor which i use as a reference. then the other part of the equation is that gain of 50 that is sort of 'assured' by TI
INA199 will give me the voltages across the 100 m ohm or 50 m ohm shunt wire multiplied by 50, which stm32 adc can read to high precision.
i'd think working out both sets of data would let me estimate that mili ohms for that piece of bare nichrome or constantan wire across which i used as a shunt ! like to hear your thoughts
:lol:

mrburnette
Posts: 93
Joined: Fri Mar 29, 2019 2:22 am
Answers: 1
OS: Linux
IDE: Arduino
Core: Any
Board: Blue, MM, Black

Re: current sensing, current measurement

Post by mrburnette » Thu Sep 12, 2019 11:22 pm

ag123 wrote:
Thu Sep 12, 2019 8:59 pm
so how do you measure that mili ohms?
E=I*R :: R=E/I

Yes, of course you know this, but the math implies that the E/I (dividend/divisor) must be based on accurate references for R (quotient) to be accurate. Therefore, if you use an OpAmp, you amplify the quotient error too. And, if there is any AC ripple component, that too gets amplified unless extreme care is taken in filtering and shielding.

How EEVBlog did E/R:
https://www.eevblog.com/projects/ucurrent/

You do not specify the accuracy needed, so I suggest any design decisions are premature. And while you may think you know about precision, a short review is always wise:
https://chem.libretexts.org/Courses/Uni ... nt_Figures

Rather than use resistors, you could use precisely cut lengths of copper wire:
http://hyperphysics.phy-astr.gsu.edu/hb ... irega.html
The trick is to make the resistance a bit low, then use a pair of pliers to pinch sections of the overall length to increase the resistance to optimum. (One sees this in very inexpensive DVM.)

Note crimps in wire used for current shunt:
_IMG_000000_000000.jpg
_IMG_000000_000000.jpg (38.84 KiB) Viewed 388 times
Extra-credit reads:
https://picaxeforum.co.uk/threads/therm ... acy.21218/

https://picaxeforum.co.uk/threads/thoug ... les.30923/

Ray

ag123
Posts: 179
Joined: Thu Mar 07, 2019 6:15 am
Location: Singapore
OS: linux
IDE: eclipse, arduino 1.8.5
Core: Roger's
Board: Maple mini, Bluepill

Re: current sensing, current measurement

Post by ag123 » Fri Sep 13, 2019 9:28 am

hi ray, thanks much for your response :)
these inputs are really valuable, i took a look at uCurrent and i realised there is something similar.
uCurrent uses MAX4239 “ultra-low offset/drift, low noise precision amplifier”
while
INA199 (ACTIVE) 26V, bi-directional, zero-drift, low-/high-side, voltage output current sense amplifier
http://www.ti.com/product/INA199
TI's description:
"The INA199 series of voltage-output, current-shunt monitors (also called current-sense amplifiers) are commonly used for overcurrent protection, precision-current measurement for system optimization, or in closed-loop feedback circuits"
i realise that much of what make the 'magic' works are the *precision* op amps, transistors even for that matter has different Hfe (beta) characteristics.
But to make those high end instrumentation amplifier op amps,
https://en.wikipedia.org/wiki/Instrumentation_amplifier
among the things that matter is that those resistors (and even possibly transistors) needs to be perfectly balanced to allow very high common mode voltages and zero offset without artifacts.
i'd guess it is why my naive attempts with lmv358 didn't get anywhere close to it. unless i made do with low side current sensing or i use 3-4 op amps in a instrumentation amplifier configuration and tune the resistors to a high precision. i read somewhere TI achieved that using laser trimmed resistors, i'd guess that's how TI made that 'magic' of high side sensing work which can't be achieved with the ordinary low cost op amps.

it seemed that my approach discussed is possibly feasible and thanks for the tip about using wires to make accurate resistances.
however, as copper has resistivity in the order of 1e-8, and nichrome has resistivity in the order of 1e-6, copper is 100 times more conductive than nichrome ! i'd think it might be possible to use a combination of both to get pretty accurate resistances. otherwise long stretches of copper is needed.
1km or 0.62 mile of AWG 24 copper wires is a mere 84 ohms !
http://hyperphysics.phy-astr.gsu.edu/hb ... irega.html

the idea is that using accurate resistors (in the range of 1ohm - 10ohm - 100ohm as reference and that using stm32 to measure the voltage and with the 100 milli ohm or 50 milli ohm shunt in series, i can estimate the current flowing through both resistors. But i think even with 10 ohms vs 0.1 ohm that is 100 times the difference, it would be difficult to be precise about it. here is where the INA199 comes in, as it can amplify that voltage across that 0.1 ohm resistor 50 times (hopefully accurately) and using that 12 bits precision on stm32 adc it can measure to 1 in 4096 parts and amplified 50 times by INA199 and using multiple different measurements with different standard resistors i'd be able to calibrate that piece of (nichrome or constantan) shunt wire to a resistance value. once that shunt resistance is known, INA199 with that same shunt in place can now work as a current sensor, ammeter. it may well be pretty accurate and precise. i've had a feeling it may even be able to measure currents to the hundreds of microamps. about 1/5 of a milliamp

if we take stm32 adc 3.3v / 4096, so for that lsb it is 805 uV, dividing that by 50 from the INA199 we get 16 uv, so I = V / R = 16 uV / 100 m Ohm ~ 161 uA. it seemed we can achieve even finer granularity with higher shunt resistances but we'd trade off the max currents we can measure

TI has parts that has higher amplification like 100 v/v and 200 v/v but aliexpress, ebay vendors it seemed don't stock them or want a big premium for those same parts. but the usual places digikey, mouser, element14 etc would have them

mrburnette
Posts: 93
Joined: Fri Mar 29, 2019 2:22 am
Answers: 1
OS: Linux
IDE: Arduino
Core: Any
Board: Blue, MM, Black

Re: current sensing, current measurement

Post by mrburnette » Fri Sep 13, 2019 2:54 pm

ag123 wrote:
Fri Sep 13, 2019 9:28 am
..
if we take stm32 adc 3.3v / 4096, so for that lsb it is 805 uV, dividing that by 50 from the INA199 we get 16 uv, so I = V / R = 16 uV / 100 m Ohm ~ 161 uA. it seemed we can achieve even finer granularity with higher shunt resistances but we'd trade off the max currents we can measure
...
https://masteringelectronicsdesign.com/ ... t-bit-lsb/
ag123 wrote:
Fri Sep 13, 2019 9:28 am
the idea is that using accurate resistors (in the range of 1ohm - 10ohm - 100ohm as reference and that using stm32 to measure the voltage and with the 100 milli ohm or 50 milli ohm shunt in series, i can estimate the current flowing through both resistors.
Not to be argumentative, but you are guessing and not designing. Too much "noise" in your thoughts - you are jumping all around the topic. Step #1 is requirements: what to you wish to measure and what accuracy is needed.

Is this a one-off project? Precision resistors are in the $1+ each range. Also, precision resistors DO NOT always come in standard values... In SMT precision listing, Newark's lowest value is 4.99 Ohms.
Resistance 4.99ohm
Product Range RP73 Series
Voltage Rating 200V
Resistor Element Type Thin Film
Power Rating 250mW
Resistance Tolerance ± 0.1%
Temperature Coefficient ± 15ppm/°C
Inserting any resistance into a test circuit WILL change the steady-state parameters of the test circuit. (Then there are leads and clips, and other connection resistances.) Therefore, shunt resistances must be very low for 2-wire in-circuit traditional measurements. Look at that pix of the $5 digital meter shunt again ...

In designing a precision current or resistance meter, the mpu is near the end of the design process.

While I "could" easily design my own meter, parts procurement and construction would be more intense. Sometimes, just playing the role of a consumer and purchasing a decent instrument is appropriate - and as a benefit, you likely get additional functionality.
Ex: https://www.amazon.com/dp/B000NI69YA/ref=emc_b_5_t
But, decent meters can be purchased under $50.


Ray

ag123
Posts: 179
Joined: Thu Mar 07, 2019 6:15 am
Location: Singapore
OS: linux
IDE: eclipse, arduino 1.8.5
Core: Roger's
Board: Maple mini, Bluepill

Re: current sensing, current measurement

Post by ag123 » Fri Sep 13, 2019 4:22 pm

thanks ray, yup precision resistors are expensive !

but for what is worth i think they are worth more than those 'shunt wires' where the manufacturers simply cut constantan or nichrome wires into strips, bend them into nice U or [ shapes, state 50 milli ohm and sell them :lol:

the real ones cost more than just a few cents a piece and that there is simply too many possible errors when dealing with 0.025 - 0.1 ohm resistances, a solder blob that stain the surface a little more could change what is 0.1ohm to much lower. so i'd guess i'm still left with having to use some kind of 'reference' to compare with. wires is a good way but that literally, i'm comparing with my multimeter in an attempt to measure and make those 'standard' resistors, so in a way they are no 'standard' at all, i doubt cheap multimeters can deliver even one of those precision resistors when it comes to accuracy. but resistor standards are way out of reach and too expensive, precision resistors is a close bet and adequate to try to calibrate something that isn't too esoteric.

for what is worth, i tend to find only very occasionally i need to measure currents. usually if the load is known, my guess is if all other things remain unchanged, e.g. just like resistance, one could infer its current drawn just measuring the voltage and I = V / R.

the situation change when for things like i want to make a bench power supply and i want it to have a current read out and the load is indeterminate. it seemed as well from time to time that being able to measure currents digitally has some actual use cases, just that for the convenience, i just use my multimeter for those 'static' cases.

the notion that I = V / R has me underestimating how difficult it can be to get that I as there are so many things that can influence measuring that V and R.
and one of those things that i did not know before hand is that high side current measurements these days isn't the same as low side measurements. in the 'old' days it is just a coil ammeter whether you connect at the top or at the bottom makes little difference. fast forward to today, op amps and digital electronics changed the whole picture. even things like kelvin bridges while they still works, there are digital and microelectronics provided alternatives such as those precision op amps, voltage references etc which becomes the new analogue for a ruler.

for what is worth, the 'project' is that i'd like a ampere readout from a bench power supply i'm trying to make, but i started realizing measuring the amps (especially the milliamps and microamps isn't as simple as I = V / R) :lol:

mrburnette
Posts: 93
Joined: Fri Mar 29, 2019 2:22 am
Answers: 1
OS: Linux
IDE: Arduino
Core: Any
Board: Blue, MM, Black

Re: current sensing, current measurement

Post by mrburnette » Fri Sep 13, 2019 7:14 pm

ag123 wrote:
Fri Sep 13, 2019 4:22 pm
...
for what is worth, the 'project' is that i'd like a ampere readout from a bench power supply i'm trying to make, but i started realizing measuring the amps (especially the milliamps and microamps isn't as simple as I = V / R)
I'm not a salesperson for Dave Jones, but at $89 AU, his product is almost flawless...
Image

I use my Fluke for most things, it is old but does a decent job.

Did you know that there are several DVM that have RS232 output? Great for logging...
https://www.sparkfun.com/products/12967

AN2834 STM Analog Guide

If you live somewhere near a major (public) university, it is likely you can arrange a short visit with a EE grad student and run a few tests on your selected shunt. Once you know the value and the temperature characteristics, you are home free to develop whatever you need.

Good luck,

Ray

ag123
Posts: 179
Joined: Thu Mar 07, 2019 6:15 am
Location: Singapore
OS: linux
IDE: eclipse, arduino 1.8.5
Core: Roger's
Board: Maple mini, Bluepill

Re: current sensing, current measurement

Post by ag123 » Sat Sep 14, 2019 4:43 pm

thanks ray, i'd consider it.
while trying to figure out a good shunt resistance value to use for the current readout from my intended bench power supply
i found out that there is a compromise between precision vs the smallest current i want to measure

if i use a 100 milli ohm shunt
so based on the lsb scenario: 3.3v / 4096 / 50 / 0.1 ~ 161 micro amps per 1 in 4096 tick on the stm32 adc
max current it can measure 161 micro amps * 4096 = 0.66 amps

if i use a 50 milli ohm shunt
so based on the lsb scenario: 3.3v / 4096 / 50 / 0.05 ~ 322 micro amps per 1 in 4096 tick on the stm32 adc
max current it can measure 322 micro amps * 4096 = 1.32 amps

i think the way it is solved is to simply use current sense amplifiers with programmable gains
so i started searching TI part numbers again
and i found INA225
http://www.ti.com/product/INA225
the nice thing is that the gain is programmable via gpio while the voltage output is still analog
the trouble is this part number is non-existent on aliexpress or possibly premium priced !

so i'm back to INA219 that I2C current sensor again
http://www.ti.com/product/INA219
i'd guess its versatility is probably why you'd only find INA219 at low costs and large volumes on aliexpress
https://www.aliexpress.com/wholesale?ca ... ext=ina219
so now to measure currents with lots of versatility, u'd need to speak i2c as well in addition to I = V / R, no more simply analog !

maybe i've a way out of this i still have my LMV358
http://www.ti.com/product/LMV358
maybe i can hack out a programmable gain amp after all, or maybe after all, all i need is just resistor dividers
:lol:

fredbox
Posts: 4
Joined: Fri Jul 26, 2019 2:27 pm

Re: current sensing, current measurement

Post by fredbox » Sat Sep 14, 2019 6:12 pm

A MAX471 may do what you want - see http://henrysbench.capnfatz.com/henrys- ... surements/. These are readily available from the usual places.

ag123
Posts: 179
Joined: Thu Mar 07, 2019 6:15 am
Location: Singapore
OS: linux
IDE: eclipse, arduino 1.8.5
Core: Roger's
Board: Maple mini, Bluepill

Re: current sensing, current measurement

Post by ag123 » Sat Sep 14, 2019 7:02 pm

thanks and yup, max471 is one of my candidate solution
another one that i literally considered is MAX4172
https://www.maximintegrated.com/en/prod ... X4172.html
but in a fit i've gotten some pieces of INA199A1 50 x gain fixed.
so i may make do with what i've got, apparently INA199A* is mass produced and seemed rather widely used in the industry.
i sometimes bought parts from rs online
https://www.rs-online.com/
and they list orders for NCS199A* (in quantities of 3000 pieces per lot) from on semi which apparently is the sku equivalent of INA199A*
https://www.onsemi.com/products/amplifi ... amplifiers
https://www.onsemi.com/products/amplifi ... /ncs199a1r
but i don't need large number of pieces and ended up buying from aliexpress again at around 30c a piece for INA199

the 'project' is very much in an exploratory stage. i've got 2 pieces of those chinese buck converters. but i'm literally thinking of not using them.
instead, i may turn my stm32f103 (blue pill) into the buck converter itself.

i did some rather interesting spice simulations to understand how buck converters work:
https://forum.allaboutcircuits.com/thre ... ds.162950/
i'd think the easy one is to simply fix the pwm duty cycle but that regulation would be poor

i still have to figure out how to do a pid control, it seemed the common buck converters do some kind of proportional control. i'd gradually try to figure that out.

i think there are some merits of making a power supply using stm32f103 as literally the controller and pwm generator for the buck converter.
for one thing this would literally be a first digital power supply project i attempted. i.e. the output voltages and currents can be controlled digitally by sending commands to the stm32. this is hardly anything new, all the PC motherboards, cpus use such buck converters so that they can vary the voltage supplied to the cpu and other components. the standard solutions are prevalent and much cheaper than my novice attempt at doing the same

of course being a power supply one would want voltage and current readouts
i think i figured out a means of making a shunt resistor by calibrating them against precision resistors as references.
it seem viable and literally buying off the shelf shunts is very little different from buying segments of wires just that the manufacturers cut them to the lengths and labelled them according to the milli ohms buyers sought.

and i don't mean to compare to the 'precision shunts' from the brand name distributors.the difference between 'precision shunts' vs perhaps the 'cheap' shunts is the effort to ensure the precision.

otherwise the resistance of a shunt is simply length x resistivity / cross section area of wire, i.e. just take ordinary nichrome or better constantan wires and cut them to the lengths required, that is the number of milli ohms which'd be similar to the 'cheap' shunt resistors

and here is the python code

Code: Select all

#!/usr/bin/python3.6
import math

resistivity = { 'nichrome' : 1.1E-6, \
                'constantan': 4.9E-7}

# mat material
# d diameter mm
# R needed resistance milli ohm
def calcres(mat, d, R):
    print(mat)
    print ('diameter : ' + str(d) + ' mm')
    r1 = d * 1E-3 / 2.0
    a = math.pi * r1 * r1
    print ('resistivity : ' + str(resistivity[mat]) + ' ohm.m')
    print ('resistance : ' + str(R) + ' milli ohm')
    l = R * a / resistivity[mat]
    print ('length : ' + str(l) + ' mm')

#50 milliohm
ohms_needed = 50
#wire dimeter mm
d = 0.5
calcres('nichrome', d, ohms_needed)
print('')
calcres('constantan', d, ohms_needed)
and sample run

Code: Select all

nichrome
diameter : 0.5 mm
resistivity : 1.1e-06 ohm.m
resistance : 50 milli ohm
length : 8.924979129516457 mm

constantan
diameter : 0.5 mm
resistivity : 4.9e-07 ohm.m
resistance : 50 milli ohm
length : 20.035667433608374 mm
:)

Post Reply