Measuring current with a microcontroller - Page 1
Forum section: electronics / general

author: samu1214
author_rank: Contributor
timestamp: May 24, 2023, 03:37:01 pm
content: Hi!I'm doing a project that involves sensing current. I'm charging a battery and I need to control how much current and voltage I'm giving out.I thought that it would be okay to use an SCR like a 40tps controlled by an MOC3021 optocoupler.The part I have doubts about: I know because of common-mode voltage that current with a shunt and a simple operational amplifier has to be measured at the low side of the battery ( - ).I saw a lot of circuits that are especially made for sensing the current with a external shunt, but do I need this if I'm okay with an error of about 3%? I want to measure around 30 A max and 1 A min. I saw some shunts of 0.5 mΩ and 9 W; that will be fine.I have a lot of trouble to find and understand the dedicated amplifiers for that. Could you suggest a circuit with an LM358 for instance that will be good for my requirements or do I need a dedicated one?Edit: I saw that the LM358 has an input offset of 2mV, and if i have 1Amp trough the SHUNT it will be 1mV, so its not enough... I saw something about the ACS711, what do you think about?Thanks!

author: peter-h
author_rank: Super Contributor
timestamp: May 24, 2023, 03:46:56 pm
content: You can buy 4 terminal resistors down to milliohms. Needs to be 4 terminal although for applications where you can factory-calibrate the scale and don't care about the tempco (or have a temp sensor so could compensate) you could just use a copper track and it would save you using some weird component.Low side sensing is simpler, if you can arrange it. But high side is also easy if you can find a supply rail which is a few volts higher still (normally that needs a DC-DC converter, a diode-capacitor pump, or some such).LM358 does most jobs, senses below GND rail, and is an excellent commodity-grade part which will never become unobtainable, but as you can see you will have a tradeoff between the precision achievable, and how much the shunt needs to drop (which will eventually drive some temp rise on the shunt).There are loads of better op-amps. I currently use a TLV2333 which is super accurate (chopper stabilised). The TI version is better than the NXP version. But not cheap. Avoid parts from Maxim, LT, Analog. Go for something common as muck.What micro are you using (what ADC spec)?

author: coppice
author_rank: Super Contributor
timestamp: May 24, 2023, 03:55:07 pm
content: Quote from: peter-h on May 24, 2023, 03:46:56 pmWhen tellng people that, its important to point out the resistive temperature coefficient of copper 0.4%/C. Most people are unaware just how much copper changes.You can generally buy two terminal resistors down to 0.5 milliohms, and 4 terminal ones down to <100 microohms. Watch out when using those 2 terminal ones, to keep all copper out of the measurement path, or copper's horrible temperature coefficient will spoil the accuracy of the resistor. Seehttps://www.analog.com/en/analog-dialogue/articles/optimize-high-current-sensing-accuracy.htmlfor some good guidance.

author: samu1214
author_rank: Contributor
timestamp: May 24, 2023, 05:20:27 pm
content: Yes, but the problem its that the 4 terminal resistors are a little bit to expensive, and i need a basic circuit for a PIC16f15223 ( 10bits ADC ) that measure the current with no so high precision. So i need the less components possible, that no generate noise in the ground, and i can get the components everytime, that no goes off stock. I think also about the copper track in the pcb, but what op-amp i should use, what consideration when designing the copper traces i need? I know that the copper its horrible when it gets hot, but i need something thats not require too much components. What do you think?

author: peter-h
author_rank: Super Contributor
timestamp: May 24, 2023, 05:47:04 pm
content: QuoteYes, which is why I mentioned measuring Tamb.QuoteHow much do you want to spend? If you can drop say 100mV on the shunt, and are happy with say 3% accuracy, an LM358 will do. If you can do factory calibration, you can do a lot better because then you are down to tempco of the Vos of the op-amp.100mV at 30A is 3 watts though, which is a LOT, so you need to get that down 10x. Either a cheap amp and factory cal, or a decent amp.This is the basic ideaI see from the DS that Vref can be just over 1V, or multiples. You need to arrange the gain so that at 30A you still have a margin on the ADC range.

author: samu1214
author_rank: Contributor
timestamp: May 24, 2023, 06:04:42 pm
content: Lets say that i will spend in a better op-amp that has around 200 gain, and as low as 35uV of offset voltage ( TSC210 ), and i use a 0.5mOhm resistor, 1W. It will be okay? In the case of the TSC210, if i choose the one with 200 gain, and connect the Vref pin to ground, i will have in the output 0.1V ( 1 Amp sense current ), and 3V ( 30 Amp sense current ). Its correct?

author: peter-h
author_rank: Super Contributor
timestamp: May 24, 2023, 06:08:12 pm
content: An op-amp has a gain of many millions. You set the circuit voltage gain with resistors, as above (100x shown).

author: samu1214
author_rank: Contributor
timestamp: May 24, 2023, 06:10:00 pm
content: Yes but the problem with the lm358 it will be the offset voltage i think

author: schmitt trigger
author_rank: Super Contributor
timestamp: May 24, 2023, 06:11:58 pm
content: Linear Technologies, now Analog Devices, has an extensive, and I mean extensive, app note with all sorts of current sensing circuits. Whenever I have to do any type of current sensing, I reach it for inspiration from the masters.Strongly advise that you search for it. Sorry, I am using me telephone on a poor reception area, to perform a search.

author: samu1214
author_rank: Contributor
timestamp: May 24, 2023, 06:23:19 pm
content: I will read it. I searched some dedicated current amplifiers and i dont understand if they have a maximum current measurement, because if im not wrong, this will be the Common-mode Range, so lets say if its 0 - 26V, in my case that i want to sense a 30A current, trough a 0.5mOhm resistor, it will be more than fine the C.M. range. And also i have 2 that im looking on, the INA199 ( 200 gain ), and the TSC210 ( 200 gain ), anyone knows wichone performs better? or what its the difference between both? Thanks

author: peter-h
author_rank: Super Contributor
timestamp: May 24, 2023, 06:53:23 pm
content: Those are complicated ways of doing the job. I am proposing just a current sensing resistor and an op-amp to amplify the voltage so it can be measured with the ADC.

author: samu1214
author_rank: Contributor
timestamp: May 24, 2023, 10:55:55 pm
content: Ok, lets get back to the circuit you sent, why is the LO input in the adc? what i will have to do in the code with that?

author: nctnico
author_rank: Super Contributor
timestamp: May 24, 2023, 11:06:12 pm
content: Quote from: coppice on May 24, 2023, 03:55:07 pmYou can ofcourse compensate by having a temperature sensor (like an NTC) onto the trace. I have used this method for a high current application. You'll still need an opamp or better, a current sensing amplifier (these aren't super expensive and typically work better compared to an opamp). Depending on whether you know when there is current, you can do a zero-offset calibration at startup and/or whenever the current is supposed to be zero. If this is for a high volume product, you can save components by adding some complexity to the firmware.

author: AVI-crak
author_rank: Regular Contributor
timestamp: May 24, 2023, 11:10:59 pm
content: ZXCT1009, no thanks.

author: coppice
author_rank: Super Contributor
timestamp: May 24, 2023, 11:41:15 pm
content: Quote from: nctnico on May 24, 2023, 11:06:12 pmYou can compensate, but it isn't usually that easy. When the resistivity changes that fast you need a pretty tight measurement of the copper's temperature to achieve good correction. If you are going to add a sensor specifically to monitor the copper, perhaps spending that money on a sense resistor makes more sense.

author: SiliconWizard
author_rank: Super Contributor
timestamp: May 24, 2023, 11:49:12 pm
content: Yeah that's usually not worth the trouble.I've done that, but not with PCB traces. With pieces of copper for handling relatively large currents. The pieces themselves were large enough to 1/ heat up only moderately even at the highest current and 2/ easily accomodate a temperature sensor coupled to them with thermal paste, and the whole thing was then calibrated.For 30A max, I would use something like this:https://www.mouser.com/ProductDetail/Bussmann-Eaton/MSMA2512R0010FEN?qs=By6Nw2ByBD2%2F4JC2e%252B1t%2FQ%3D%3Dand call it a day.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: May 25, 2023, 02:16:36 am
content: Quote from: samu1214 on May 24, 2023, 03:37:01 pmThat is a very custom solution, tho a nice part with modest isolation. Likely an overkill for what you need, and looks single-sourced.Quote from: samu1214 on May 24, 2023, 03:37:01 pmThose are simple dedicated current sense amplifiers, ideal if you are forced to sense on the high side of up to 26VThe INA18x/199 etc series are multisourced and cheaper than TSC210Quote from: samu1214 on May 24, 2023, 03:37:01 pmSee other posts above.A shunt rated to 30A will be simplest and most production consistent, as PCBs also vary in thickness, as well as the copper tempco.It looks like 1mOhm is a common value, and you can get quite good specs like3.5W 1mΩ ±25ppm/℃ ±1%from lcsc for ~ 7c/150https://www.lcsc.com/product-detail/Current-Sense-Resistors-Shunt-Resistors_Milliohm-HoLR2512D-3-5W-1mR-1_C5123666.htmland there is also 0.5mΩ 2W 25ppm/℃~+50ppm/℃ ±1%Quote from: samu1214 on May 24, 2023, 03:37:01 pmAs you found the LM358 is ancient, and not great at the millivolts stuff.

author: samu1214
author_rank: Contributor
timestamp: May 25, 2023, 03:49:35 am
content: Quote from: SiliconWizard on May 24, 2023, 11:49:12 pmYes, that kind of shunt its what i saw... But what dedicated amplifier you will use with that? The budget its about 3-4 USD, and i want that the maximum output is around 5v for an ADC ( pic16f15223 )

author: samu1214
author_rank: Contributor
timestamp: May 25, 2023, 03:54:09 am
content: Quote from: PCB.Wiz on May 25, 2023, 02:16:36 amYes, i saw the datasheet of the INA199, I would like to know what about connecting also the Vref pin to the ADC of the pic16f15223, for what would be that? is it better accuarcy? what i will have to do in the code? Thanks

author: PCB.Wiz
author_rank: Super Contributor
timestamp: May 25, 2023, 04:23:43 am
content: Quote from: samu1214 on May 25, 2023, 03:54:09 amYou mean the VREF pin of the INA199 ?Follow the data, that shows how to connect that if you want bi-directional current sense. For one way (charging only) you can GND the VREF.The standard current sense parts come in 50x 100x 200x gain choices, so you pick your shunt and MCU ADC ranges to suit that.eg you might select 2 x 1.5mOhms, in parallel, (based on price/availability) and that gives full scale of 30m*0.75*200 = 4.500VOther options depend on the range choices of your MCU

author: peter-h
author_rank: Super Contributor
timestamp: May 25, 2023, 07:33:42 am
content: I don't understand why anyone would use those single sourced and expensive Hall current probes, unless they want galvanic isolation, or don't care about costs or long term manufacturing.But in this case we have so little information on the OP's criteria.

author: Avelino Sampaio
author_rank: Frequent Contributor
timestamp: May 25, 2023, 08:46:53 am
content: The ACS712, 30A version, seems to serve its purpose very well. Its cost is 2$ at LCSC.https://datasheet.lcsc.com/lcsc/1811061532_Allegro-MicroSystems--LLC-ACS712ELCTR-30A-T_C9932.pdf

author: samu1214
author_rank: Contributor
timestamp: May 26, 2023, 09:52:33 pm
content: Thanks everyone, i decided to bought 2 resistor SMD, one is 4 terminal and the other 2. I will check how much this changes and if its convinient to buy a 4 terminal resistor. Then i bought 3 dedicated op amps, TSC210 ( 200 Gain ), ACS712ELCTR-30A-T ( 30A version ) and INA199C3 ( 200 gain ). With them and some LM358 that i have i will be testing the reliability and precision in all of them. I will be back after checking all of this. Thank you for you help!

author: nctnico
author_rank: Super Contributor
timestamp: May 26, 2023, 09:57:01 pm
content: You can't derive precision or reliability from just a few parts. For determining the resulting precision of your design, you'll need to work with the worst case specs (spread and temperature coefficients) in the datasheet and do the math.A problem with the ACS712ELCTR and related devices is that the output depends on the supply voltage so you'll need to power it using a voltage reference chip in order to achieve the specs from the datasheet. Normal regulators are 1% at best (typically 2% to 3%). Alternatively you can use the same supply as the reference for de ADC; the error will cancel in that case.

author: tooki
author_rank: Super Contributor
timestamp: May 27, 2023, 02:41:35 pm
content: Why not just use a digital current/voltage sensing chip like the INA219 or INA226. They’re better than the ADCs in a MCU.Or, if this is for lithium batteries, please just use a lithium charging chip with appropriate protection ICs.