Author Topic: Measuring current with a microcontroller  (Read 2638 times)

0 Members and 1 Guest are viewing this topic.

Offline samu1214Topic starter

  • Contributor
  • Posts: 10
  • Country: ar
Measuring current with a microcontroller
« on: May 24, 2023, 03:37:01 pm »
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! ;D
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 3947
  • Country: gb
  • Doing electronics since the 1960s...
Re: Measuring current with a microcontroller
« Reply #1 on: May 24, 2023, 03:46:56 pm »
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)?
« Last Edit: May 24, 2023, 03:53:35 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 
The following users thanked this post: samu1214

Offline coppice

  • Super Contributor
  • ***
  • Posts: 9210
  • Country: gb
Re: Measuring current with a microcontroller
« Reply #2 on: May 24, 2023, 03:55:07 pm »
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.
When 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. See https://www.analog.com/en/analog-dialogue/articles/optimize-high-current-sensing-accuracy.html for some good guidance.
 
The following users thanked this post: samu1214

Offline samu1214Topic starter

  • Contributor
  • Posts: 10
  • Country: ar
Re: Measuring current with a microcontroller
« Reply #3 on: May 24, 2023, 05:20:27 pm »
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?
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 3947
  • Country: gb
  • Doing electronics since the 1960s...
Re: Measuring current with a microcontroller
« Reply #4 on: May 24, 2023, 05:47:04 pm »
Quote
resistive temperature coefficient of copper 0.4%/C

Yes, which is why I mentioned measuring Tamb.

Quote
but what op-amp i should use

How 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 idea



I 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.

Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 
The following users thanked this post: samu1214

Offline samu1214Topic starter

  • Contributor
  • Posts: 10
  • Country: ar
Re: Measuring current with a microcontroller
« Reply #5 on: May 24, 2023, 06:04:42 pm »
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?
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 3947
  • Country: gb
  • Doing electronics since the 1960s...
Re: Measuring current with a microcontroller
« Reply #6 on: May 24, 2023, 06:08:12 pm »
An op-amp has a gain of many millions. You set the circuit voltage gain with resistors, as above (100x shown).
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 
The following users thanked this post: samu1214

Offline samu1214Topic starter

  • Contributor
  • Posts: 10
  • Country: ar
Re: Measuring current with a microcontroller
« Reply #7 on: May 24, 2023, 06:10:00 pm »
Yes but the problem with the lm358 it will be the offset voltage i think
 

Offline schmitt trigger

  • Super Contributor
  • ***
  • Posts: 2302
  • Country: mx
Re: Measuring current with a microcontroller
« Reply #8 on: May 24, 2023, 06:11:58 pm »
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.
 
The following users thanked this post: samu1214

Offline samu1214Topic starter

  • Contributor
  • Posts: 10
  • Country: ar
Re: Measuring current with a microcontroller
« Reply #9 on: May 24, 2023, 06:23:19 pm »
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
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 3947
  • Country: gb
  • Doing electronics since the 1960s...
Re: Measuring current with a microcontroller
« Reply #10 on: May 24, 2023, 06:53:23 pm »
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.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 
The following users thanked this post: samu1214

Offline samu1214Topic starter

  • Contributor
  • Posts: 10
  • Country: ar
Re: Measuring current with a microcontroller
« Reply #11 on: May 24, 2023, 10:55:55 pm »
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?
« Last Edit: May 24, 2023, 10:58:11 pm by samu1214 »
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 27580
  • Country: nl
    • NCT Developments
Re: Measuring current with a microcontroller
« Reply #12 on: May 24, 2023, 11:06:12 pm »
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.
When 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 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.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 
The following users thanked this post: samu1214

Offline AVI-crak

  • Regular Contributor
  • *
  • Posts: 132
  • Country: ru
    • Rtos
Re: Measuring current with a microcontroller
« Reply #13 on: May 24, 2023, 11:10:59 pm »
ZXCT1009, no thanks.
 
The following users thanked this post: samu1214

Offline coppice

  • Super Contributor
  • ***
  • Posts: 9210
  • Country: gb
Re: Measuring current with a microcontroller
« Reply #14 on: May 24, 2023, 11:41:15 pm »
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.
When 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 of course 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.
You 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.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15095
  • Country: fr
Re: Measuring current with a microcontroller
« Reply #15 on: May 24, 2023, 11:49:12 pm »
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%3D
and call it a day.
 
The following users thanked this post: samu1214

Offline PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 1745
  • Country: au
Re: Measuring current with a microcontroller
« Reply #16 on: May 25, 2023, 02:16:36 am »
..... I saw something about the ACS711, what do you think about?
That is a very custom solution, tho a nice part with modest isolation. Likely an overkill for what you need, and looks single-sourced.

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
Those are simple dedicated current sense amplifiers, ideal if you are forced to sense on the high side of up to 26V
The INA18x/199 etc  series are multisourced and cheaper than TSC210

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.
See 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 like  3.5W 1mΩ ±25ppm/℃ ±1% from lcsc for ~ 7c/150
https://www.lcsc.com/product-detail/Current-Sense-Resistors-Shunt-Resistors_Milliohm-HoLR2512D-3-5W-1mR-1_C5123666.html
and there is also 0.5mΩ 2W 25ppm/℃~+50ppm/℃ ±1%

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
As you found the LM358 is ancient, and not great at the millivolts stuff.
 
The following users thanked this post: samu1214

Offline samu1214Topic starter

  • Contributor
  • Posts: 10
  • Country: ar
Re: Measuring current with a microcontroller
« Reply #17 on: May 25, 2023, 03:49:35 am »
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%3D
and call it a day.

Yes, 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 )
 

Offline samu1214Topic starter

  • Contributor
  • Posts: 10
  • Country: ar
Re: Measuring current with a microcontroller
« Reply #18 on: May 25, 2023, 03:54:09 am »
..... I saw something about the ACS711, what do you think about?
That is a very custom solution, tho a nice part with modest isolation. Likely an overkill for what you need, and looks single-sourced.

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
Those are simple dedicated current sense amplifiers, ideal if you are forced to sense on the high side of up to 26V
The INA18x/199 etc  series are multisourced and cheaper than TSC210


Yes, 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
 

Offline PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 1745
  • Country: au
Re: Measuring current with a microcontroller
« Reply #19 on: May 25, 2023, 04:23:43 am »
Yes, 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
You 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.500V
Other options depend on the range choices of your MCU
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 3947
  • Country: gb
  • Doing electronics since the 1960s...
Re: Measuring current with a microcontroller
« Reply #20 on: May 25, 2023, 07:33:42 am »
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.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 
The following users thanked this post: samu1214

Online Avelino Sampaio

  • Frequent Contributor
  • **
  • Posts: 264
  • Country: br
Re: Measuring current with a microcontroller
« Reply #21 on: May 25, 2023, 08:46:53 am »
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

 
The following users thanked this post: samu1214

Offline samu1214Topic starter

  • Contributor
  • Posts: 10
  • Country: ar
Re: Measuring current with a microcontroller
« Reply #22 on: May 26, 2023, 09:52:33 pm »
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!
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 27580
  • Country: nl
    • NCT Developments
Re: Measuring current with a microcontroller
« Reply #23 on: May 26, 2023, 09:57:01 pm »
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.
« Last Edit: May 27, 2023, 03:01:38 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 12336
  • Country: ch
Re: Measuring current with a microcontroller
« Reply #24 on: May 27, 2023, 02:41:35 pm »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf