Playing with Arduino and INA226 (Current and Power Monitoring) - Page 1
Forum section: electronics / beginners

author: wykasz
author_rank: Contributor
timestamp: February 04, 2023, 04:16:45 pm
content: Hello,I'm trying to build a crude and simple dummy load for basic DC measurements, based on power resistors (i'm trying to avoid mosfets and designing circuit boards at the moment, that would be my next project).I don't need precise control over the load, i just need reliable measurement within, let's say, 1% error range for 0-30V and 1-20A, at few selected resistance values.I'm not expecting to have great resolution at 1A of course with such requirements.But my results are a bit disappointing so far.What i'm kindly asking for is to check if i'm thinking correctlyRight now i have a cheap CJMCU-226 INA226 module [attachment=1] connected over i2c to an arduino board.I'm using INA226_WE libraryhttps://github.com/wollewald/INA226_WEto control the device.I've desoldered original shunt resistor (0.1Ohm) and connected external shunt resistor 75mv/50A 0.0015Ohm, with Kelvin connection, on the low side of the device. I'm using in+ and in- pads on the board (and of course i've connected vbus and the rest)I'm powering the device with 5V from a MeanWell PSU 5V/5A.For testing i'm using my Rigol 832 as a power source and BM869s DMM for voltage measurements.Rigol is connected to my device with a pair of 11AWG 1m long copper cables.Device is configured to 10 Ohms + shunt's 0.0015 Ohm.So far it's working.I have pretty good voltage measurements.With 27.00V set on a second PSU channel, with 2.70A load (reading from Rigol) i'm reading 26.96V from INA226 and 26.951V with my DDMM connected to same point as INA226.So there is ~10mv of difference and it's pretty consistent over whole 1-30V range and it's way better then 1% (0.03% if i'm correct ?)However amperage readings are much worse.At 17.00V i'm reading 1,70A on Rigol but 1.723 to 1.745 on INA226 and it has a tendency to change within that range randomly over time (every second or so). That does not look like a temperature drift.I'm also measuring voltage on shunt with my DMM, and it's 2.55mv.(50A*(2.55/75)=1.70) - seems that my connection to shunt resistor is correct and shunt itself is OK.1.70/1.745 = 0.974 which is close to 3% error at 1.7A. That's not good.Also there is constant 0.02A to 0.05A reading at 0V with disabled PSU output.What i'm reading from INA226 datasheet1) There is 0.1% max gain error which corresponds to 0.002A error.2) I should expect 10uV bias, which can give me 0.006A error.That does not add up to 0.045A error.I'm well within shunt input voltage range (+/- 81,9175mv)With native 16-bit resolution of ADC i should have ~0.8mA steps with 50A shunt.I'm considering one of three possibilities:1) I don't understand this chip capabilities and limitations and it's simply incapable of what i'ma asking. Weel, for sure i'm not understanding it fully, but i thing my requirements are reasonable and this chip is capable of providing that.If so, can you see an obvious error in my thinking or is there something i'm missing here?2) The device itself is of poor quality or faulty. Seems legit, since the autor of INA226_WE library is mentioning this problem on the front page of his github repository.3) There is an error in library. I've selected this one because it seems to be well maintained. So i doubt it.I'd like to consider those options and i ask for advice, before i start to change thins randomlyOther than that device (both hardware and software) works really well, so i'm trying to hold on with rash decisionsRegards,

author: CountChocula
author_rank: Unknown
timestamp: February 04, 2023, 07:49:15 pm
content: I'm not super familiar with this chip, but it sounds like you changed the shunt resistor from 0.1Ω to 0.0015Ω, so two semi-random thoughts:Did you update the INA226's calibration register to account for the different setup? The value of the new sense resistor is 2 orders of magnitude smaller than the old one, so without calibration I would expect you to get incorrect values because the chip isn't aware of this change.With a smaller value, you're probably going to deal with a lot more noise, and the quality of your connections becomes much more important, so there might be something to consider there, particularly given the level of precision you're trying to reach.FWIW, you can get excellent results with a much simpler setup… I just made a PSU that uses a cheap 5% 0.1Ω sense resistor and the Arduino's internal ADC, and it's pretty accurate—it just requires a little bit of software to do oversampling and error correction. Cheers!—CC

author: wykasz
author_rank: Contributor
timestamp: February 04, 2023, 09:17:26 pm
content: Thanks for reply.QuoteI've followed your advice and checked it.The default value of the calibration register is 0 and it has to be set during initialization.I'm using library method "void INA226_WE::setResistorRange(float resistor, float current_range)" to set correct value.Changing shunt resistance to 0.015 without changing the resistor results in readings also changing by a factor of 10 (0.173A) but there is still an error. So the register is being set quite correctly.I've also checked how the library is calculating the register value, it seems to be correct. It follow both the datasheet and author's description.Since it's using floating point with small fractions i double check calculated values - they look ok.What I've found inspired by You, is that shunt voltage measured by INA226 is incorrect.At 17V / 1.7A it reads 2.59mv instead of 2.55mV.A 0V it reads 0.04mV = 40uVKind of consistent, but nothing like that in datasheet.The shunt voltage register is read-only and seems to be unaffected by calibration value, so it likes like there lays the proboblem.I'm not sure if this is related somehow to the pcb design, my application of it or the TI INA226 chip is counterfeit?QuoteThanks, i've checked it, I'm measuring 2.55mv straight at the PCB of the INA226 module.I cant check it directly on INA226 pins, to smallQuoteI was hoping outsource all the engineering related to measurements to the designers of the chip and module and also author of the libraryInitially I was planning to use 0.1Ohm shunt, and i have 200W one at my desk. However at 20A i should expect 2V drop on the shunt and 40W of power dissipating on it.I wrote that i'm expecting 0-20A range which is ok for me, but i hope to be able to work with up to 40A for short periods of time if neccesary. Then it would be 160W. The rest of the equipment is prepared for it.Anyway, those cheap power resistors are made of wire and are not good at keeping their values with changing temperature.But i had proper shunt resistor rated for 50A at my disposal, so i decided to use the proper one.For now i'll try to just adjust the shunt voltage reading and manually calculate current and power ignoring the register values.But it's not a permanent solution nor the right answer.

author: wykasz
author_rank: Contributor
timestamp: February 04, 2023, 09:33:40 pm
content: With 0.045mV correction the readings are within 1%range over 3-30V at 0.3-3A (load R=10).Below 3V, it's a bit worse - i'm seeing 0.003A noise, so for 1V/0.1A it's going to be 3%, so i'm going to accept that for now.However i'm not happy with this magic 0.045 constant in my code.

author: CountChocula
author_rank: Unknown
timestamp: February 04, 2023, 09:41:02 pm
content: Quote from: wykasz on February 04, 2023, 09:17:26 pmPossibly a silly question, so apologies in advance, but… how do you know that it's the INA226 that's wrong? With a difference of about 40µV, we're talking about tiny amounts, so it could just be that your DMM is reporting an incorrect value, and not the chipI guess my point is that you're really starting to get in the noise here. Measuring such small voltages requires precision equipment and probably really good technique (that certainly goes beyond my knowledge, so someone else might be able to give you better advice)… I would argue that what's most important is consistency—as long as the offset is always the same (or varies within an acceptable tolerance) as you deal with different currents, you can probably just account for it as a calibration step in your software.—CC

author: wykasz
author_rank: Contributor
timestamp: February 04, 2023, 09:50:15 pm
content: QuoteGood questions.BM869s precision for DC voltage at 500mV range is 0.02% + 2 digits.At 2.55 mV I could expect uncertainty of +/- 0.02mV (this 0.02% is negligible i suppose).So if the real value is 2.57 You may be correct. But that would be at the worse end od my DMM precision, i hope it's not.Please correct me if i'm wrong.

author: wykasz
author_rank: Contributor
timestamp: February 04, 2023, 09:56:55 pm
content: Unfortunately this DMM is the best i have. But if that's the case, then i guess, the problem lies somewhere elseI should consider a different method of measurements I thinkI'm not familiar with shunt's that much. If I remember correctly the proper ones are made of different alloys to have low tempco, and are standardized for this 75mv per shunt's range value.I need to look for a different one, with a resistance an order of magnitude bigger I guess, but i'm not sure if such shunts exists for 50A range.

author: wykasz
author_rank: Contributor
timestamp: February 04, 2023, 10:07:26 pm
content: I've also checked BM869s's 500000 count mode.If i'm correct in this mode the 0.02%+2d formula for precision still applies.In this case expected error is += 0.0025mV.The reading is 2.562V with last digit fluctuating a bit between 2.560V and 2.562V, within expected margin of error.Regards

author: CountChocula
author_rank: Unknown
timestamp: February 04, 2023, 10:31:18 pm
content: Quote from: wykasz on February 04, 2023, 10:07:26 pmThat only tells you that the reading is stable (that is, that the resolution is good), not that it is absolutely correct (that is, that the precision is correct), however. Think about this: if for whatever reason your DMM is out of calibration by 20µV—a tiny, tiny amount—you are now halfway to the reading your chip is making!In any case, I am just a beginner and don't have experience working with either such small voltages or high currents, so again I hope someone else can provide more insight. Thinking about this from first principles, though, I wonder if you're approaching the problem from the right angle.For example, do you really need this much precision across such a wide range of currents? If you were to use a larger shunt for smaller currents, for example, you could get better results at the low end (where it's probably more critical) and trade precision for range when you're reading higher currents. If you think about it, that's the way multimeters work, too… there's probably a reason why 6 and 7-digit DMMs cost hundreds, or even thousands of dollarsGood luck!—CC

author: dobsonr741
author_rank: Frequent Contributor
timestamp: February 04, 2023, 10:43:02 pm
content: How much ripple/noise is present on the load when measuring? Switching power supply can give you 100mV ripple easily, as well significant common mode noise.If you like to be in the single digit percentage accuracy I suggest characterizing your INA + Arduino on battery power to eliminate ripple and common mode disturbances first. Possibly with a higher R shunt. It will give you the confidence in your code and the in ADC so that you know the INA is accurate and not a factory reject with out of range specs.

author: wykasz
author_rank: Contributor
timestamp: February 04, 2023, 10:57:32 pm
content: @CountChoculaI agree. I was hoping to avoid that approach since it is complicating my design. I hope to push this INA226 chip to the limits, the only thing is that i'm not sure what is the limit, due to my poor understanding of this topic.You are right about precision, i'm not used to working with that kind of precision, usually cheap aneng dmm and +-0.1V is ok in my applications, so i guess i'm making a lot of mistakes now. Thanks for help with that.What keeps me thinking about this is fact that INA266 chip was designed to work with this 0-80mV shunt voltage and with 16bit adc. So i think they (designers?) were aware of challenges involved.They even increased adc resolution from 12bits in ina219 to 16bit in ina226. And even with 12 bits the resolution still is better than 0.04mV error I'm facing. So why would they bother to increase resolution if expected error is an order of magnitude bigger?That's the question. Am i doing it wrong and that's why i'm getting wrong results, or everything is ok and working just as expected and it's just me not seeing it in a datasheet?@dobsonr741Thanks for reply. Good questions, did not think about it.Rigol 832 is a linear PSU. I'm not expecting 100mV ripple.I'll try to measure it. Also need to check ripple on 5V meanwell psu. That is SMPS, i guest 100mv is expected there.Thanks for idea with battery. I guess i could use batteries for both: powering my circuit and sourcing power for the load.

author: dobsonr741
author_rank: Frequent Contributor
timestamp: February 04, 2023, 11:39:48 pm
content: On accuracy: you are chasing 40uV. It's tough, much dependent on noise, pcb layout, biases, CMR and so on.ADC resolution is not the only factor, you need to add up all the error causing factors cited on the data sheet: gain error, temperature coeff of gain, bus voltage gain error (CMRR), bus voltage gain error coeff of temperature, differential nonlinearity, offset error, noise vs conversion time tradeoffs.And be aware, the exact data sheet specs are only reproducible in a high accuracy lab environment. The data sheet is a marketing material, and the engineers were pushed hard by marketing to produce something that sells, yet still true, to show the product in the best possible angle in a competitive environment. When you try to reproduce results close to the data sheet's specs you are up against the industry's best equipped test lab.

author: wykasz
author_rank: Contributor
timestamp: February 12, 2023, 06:40:33 pm
content: HelloA short follow upRipple measurements.I've decided to measure ripple on both my psu. The device has two separated meanwell PSU: 5V for logic, 12V for fans and relays.There is one exception that i've missed before - arduino is powered from 12V through it's linear regulator, due to fact the the barrel plug does not accept 5V. Somehow i forgot about it.INA226 is powered from 5V supply.Measurements.Oscilloscope: Siglent 1104x-eOEM probe i 10x mode (I know, but i don't think it matters in this case).AC coupling, 20MHz bandwidth limit.12V PSU <15mv ripple measured5V PSU < 6ms ripple measuredBoth PSU were under load (dummy load was enabled and connected to Rigol 832 PSU and set to 10W).It doesn't look bad in my opinion. I was expecting worse.Please if you see any error/mistake in my measurement point me towards themBatteryI've decided not to do measurements with battery right now.First i need to power DL with 12V and 5V and i do not have batteries with precisely that voltages. And i don't want to use SMPS to adjust the voltage like i usually do with batteriesI need to build linear regulatorsSecond, i don't want to tear apart DL right know. I bought few more INA226 modules and i'll try to characterize them in the future, also i'll try to do it in controlled environment first, without other DL elements.2. INA226I've take a picture of mine. Unfortunately I don't have a genuine one to compare with, nor i couldn't find any pictures at TI site.It's impossible to acquire INA226 in Poland from any reputable seller. Also it's not available at mousers or digikey. So i couldn't order a genuine one with reasonable shipping costs.However last known price is comparable to the cost of my whole module, so i'm not expecting to have a good one.ResultsI've also rewritten the library to use integers and decided to remove support for current/power registers. It's all calculated in software now.After applying 45uV correction i have good enough results,So far for 1-10A, 2-30V range i'm getting precision better than 1% (compared to DMM/Rigols readings).For 0-1A at 0-2V it's more like 5%.Having better precision was never the plan, and for 0-1A/0-1V i can use my DMM, so that's ok.I'm happy with that.What's next?I'm planning to do few more measurements. I'd like to figure out how much ripple/noise from my DL is injected into measured voltage/current.I'm also working on second DL based on classing opamp + mosfet design.However i have a lot to learn about op-amps so i'm waiting for my copy of "The Art of Electronics" to arriveThanks for help.EditSomehow i was able to change probe settings from 10x to 1x. I don't know how, when i was starting measurements it was set to 10x, but here it's 1x on every screenshot.However, that means that real measurements are more like:12V PSU <150mV ripple measured5V PSU < 60mV ripple measuredThat's more realistic