Arduino digital ammeter with a 75mV current shunt? - Page 1
Forum section: electronics / beginners

author: ice595
author_rank: Regular Contributor
timestamp: August 06, 2014, 02:50:59 am
content: Im planning to make a battery capacity meter using an Arduino.The plan is to use a 10A 75mV current shunt as a current sense resistor then use an opamp to amplify the voltage across the shunt resistor then use the Arduino's onboard ADC to measure the voltage then calculate current using ohm's law.Here are my calculations:10A full scale = 75mV across the shunt resistor.1A = 7.5mV0.1A = 750uVIf I want my Arduino ammeter to read 5A max then I would need to set the opamp's gain to 133x5A = 37.5mV37.5mV * 133 = 4.988V (within the ADC's input range)But if I want my ammeter to have 0.1A precision, then there will only 750uV across the shunt resistor when theres 0.1A through it.The questions is, can such opamp circuit be easily made to take 750uV input then amplify by a factor of 133 then feed it to the ADC.

author: VK5RC
author_rank: Unknown
timestamp: August 06, 2014, 04:03:34 am
content: Suggest google high side current sense amplifier, several purpose built units for not crazy money, I used ltc6101 which works well despite a lot of rf around the place.

author: Rick Law
author_rank: Super Contributor
timestamp: August 06, 2014, 07:03:27 am
content: Quote from: ice595 on August 06, 2014, 02:50:59 amI had some experience using Arduino (ATMEGA328) ADC. I did some base line data gathering when I was doing current measurement using shunt. Bear in mind, the ADC is not exactly linear, at very low voltage (small current), the error is sky high:You will have very good accuracy > 2V.at 1V (2A with your 5Volt=10A plan), you would have about 1% errorat 0.5V (1A) your error would shoot up to around 2.5%at 0.1V (200mA) error would shoot up to around 10%at 0.05V (100mA) error would be > 25%and it continues to increasesimilarto (but not exactly) "error=constant/V" graph.I have the "native" (ie:no compensation) error graph here:https://www.eevblog.com/forum/microcontrollers/atmega328-adc-accuracy-graph/You will need to do some compensation to get down low. What I made use about 24x (approx 2A=5V mapping). With good (software based) compensation, I can get error rate at +- 3 to 5 counts down to high single digit ADC count. Below ADC=5, error goes explosive. I did not eliminate the error with compensation. I have an error graph shape just like the above, but the curve moved toward the left (lesser error until lower).Good luck and have fun...Rick

author: rs20
author_rank: Super Contributor
timestamp: August 06, 2014, 07:51:15 am
content: Quote from: ice595 on August 06, 2014, 02:50:59 amYep, no worries. Refer to Dave's open-sourced uCurrent design. It uses a small shunt resistance and two op-amps, each amplifying by a factor of 10, to give a total gain of 100, which is near enough to what you need. Dave used two separate op-amps because he wanted 300kHz bandwidth, very very good accuracy, etc etc. Depending on how demanding your application is (and if you're using a uC ADC, I hope the answer is "not very"), you can probably get away with a single op-amp just fine. Because the input signals are so small, you'll need to understand input bias currents, input offset voltages, etc -- there's an EEVBlog video about this. Give it a go, show us the design you come up with (and the part(s) you choose), and we'll critique it for you and suggest any improvement or gotchas that you might run into.Quote from: Rick Law on August 06, 2014, 07:03:27 amThis looks suspiciously like a constant error that deceivingly looks like it's exploding at decreasing values only because you're expressing it as a percentage of those decreasing values... I mean, if I measure a 1.6 mV source and the multimeter reports 0.001V, isn't it a bit deceiving to say that it's off by 60%? It's just off by one count. There's a reason that multimeter manufacturers specify accuracies as something along the lines of "DC accuracy: 0.05% + 3 counts". You should probably attempt to do the same (as it sounds like you sort of started to do) and specify the error accordingly; most people who read the results you present above are going to get worried about nothing -- they'll think "oh, no, an error of 25%!?" instead of "OK, an error of 25% on a tiny measured value works out to... a super-tiny error", which is more appropriate in most applications. In the unlikely case you actually need true relative accuracy on a huge dynamic range of signals, you need ranging like a multimeter has.

author: ovnr
author_rank: Frequent Contributor
timestamp: August 06, 2014, 08:54:14 am
content: Yes, there isn't really a problem using an opamp to boost your signal 133x - just be sure to find one with low input offset voltage, etc.I'd also strongly suggest using a proper ADC...

author: zapta
author_rank: Super Contributor
timestamp: August 06, 2014, 08:59:08 am
content: Since you want to measure capacity you need to do integration. Some chips will do it for you, that is continuous charge integration. You don't really need an a/d. Check for example ltc2943. You will get better accuracy this way for bursty current.

author: Richard Head
author_rank: Frequent Contributor
timestamp: August 06, 2014, 11:19:51 am
content: It's generally best to use a differential amplifier to measure the shunt voltage. Much more convenient.

author: mij59
author_rank: Frequent Contributor
timestamp: August 06, 2014, 11:24:03 am
content: You could use a instrumentation amplifier like the INA333.

author: rs20
author_rank: Super Contributor
timestamp: August 06, 2014, 02:07:43 pm
content: If you want to have a bit of fun and learn things, you can easily configure op-amps to operate in an integrator topology (google "op amp integrator" -- it's just a feedback capacitor instead of a feedback resistor) -- but just using an LTC2943 as suggested is probably quicker.Quote from: Richard Head on August 06, 2014, 11:19:51 amDo you mean an op-amp in a differential configuration, or some sort of specially designated differential amplifier part? I've heard of parts with differential outputs, but all op amps are sort-of differential input if you want them to be, right?Quote from: mij59 on August 06, 2014, 11:24:03 amNote to OP: Many op-amps won't work properly when the input voltages are very close to the rails. For instance, this INA333 is only specified for operation above 0.1V and below supply voltage - 0.1V. You can get around this by shifting the inputs voltages down using a diode drop/resistor dividers, etc. Or use a rail-to-rail opamp, or use LTC2943.

author: ludzinc
author_rank: Unknown
timestamp: August 06, 2014, 02:58:03 pm
content: I've recently blogged about my adventures in programmable gain and currrent measurement.See here:http://ludzinc.blogspot.com.au/2014/07/microcontrolled-analogue-gain.htmlhttp://ludzinc.blogspot.com.au/2014/07/multi-channel-meter.htmlhttp://ludzinc.blogspot.com.au/2014/08/multi-channel-meter-part-2.htmlTL:DR;Use the MAX4238 as per Dave's uCurrent - he runs that at x100 gain, no worries, x133 should be okay as well.

author: ice595
author_rank: Regular Contributor
timestamp: August 06, 2014, 04:45:05 pm
content: wow, I didn't think measuring current would be so difficult.can't I just use a low drift 1ohm resistor as my current sense resistor? as long the resistance doesn't drift too much with temperature.

author: zapta
author_rank: Super Contributor
timestamp: August 06, 2014, 04:57:46 pm
content: Quote from: ice595 on August 06, 2014, 04:45:05 pmIt's not ;-)The circuit with the LTC2943 is very simple has small footprint and you can talk to it I2C from the Arduino. It has both A/D for momentary reading and integrator (Coulomb Counter) for accurate and continuous integration. It's high side (good, you get a common ground), only 50mv burden voltage mac scale, and also measures voltage and temperature. What else do you need? can't be simpler than that ;-)For shunt you want to use an accurate 'kelvin' resistor. This way you get accuracy off the shelf and can't mess with with PCB soldering variations. I am using Digikey FC4L64R025FERCT-ND (0.025 ohm) for 2A range. Scale it down for higher range.Edit: schematic herehttps://github.com/zapta/power-monitors/blob/master/pmon_3v8/eagle/pmon_3v8_schematic.pdf?raw=true

author: mij59
author_rank: Frequent Contributor
timestamp: August 06, 2014, 05:00:50 pm
content: Quote from: ice595 on August 06, 2014, 04:45:05 pmWhat is the voltage range of the source you want to measure ?A 1 Ohm resistor at 5A will dissipate 25W ! you probably can't afford such a resistor with low tempco .

author: David Hess
author_rank: Super Contributor
timestamp: August 06, 2014, 05:51:31 pm
content: Quote from: ice595 on August 06, 2014, 04:45:05 pmIt is not difficult but it can be tricky to deal with common mode voltages close to the positive supply rail. Resistive dividers and difference amplifiers can be used to handle this but they introduce additional common mode errors.I prefer to use a singled ended level shift with a precision operational amplifier instead of a difference amplifier. This creates a ground referenced voltage proportional to the current.http://www.analog.com/library/analogdialogue/archives/44-12/high_side.html

author: Rick Law
author_rank: Super Contributor
timestamp: August 06, 2014, 09:40:49 pm
content: Quote from: rs20 on August 06, 2014, 07:51:15 amI am in agreement with that. Mathematics dictates that a percentage will balloon as the denominator gets smaller and smaller. Indeed a lot of that graph is illustrating exactly that.When I was first looking at the data, I attempted to curve fit it. I looked at the data enough to realize that the count error does increase as it get down closer to zero. I continue to use that graph as a baseline because it illustrates very clearly how getting down low is getting it more laden with error as well.The "elbow" dividing the vertical drop and the horizontal "almost linear slow drop". In my own way of thinking, it defines "the habitable zone" for the ADC verses "the wild country" of the vertical drop. Now when I make something that depends on the ADC, I do what I can to keep the ADC readings to be inside the "habitable zone".

author: Rick Law
author_rank: Super Contributor
timestamp: August 06, 2014, 09:55:06 pm
content: Quote from: ice595 on August 06, 2014, 04:45:05 pmFolks here like to recommend the better solutions. Better solutions will tend to get more complicated than simple ones. If the application is not demanding, you could do it exactly as your initially layout. You just have to accept that when you are measuring below 1V, your accuracy will go very very far south.I actually acquired the ADS1115 for the random / quick&dirty measurements if/when I need something better than the ATMEGA328's native ADC.

author: ice595
author_rank: Regular Contributor
timestamp: August 06, 2014, 10:45:34 pm
content: it would be nice if the LTC2943 didn't come in such a pain-in-the-arse pin package.anyways, what are some popular differential amplifiers that comes in through hole or at least SOIC package?

author: zapta
author_rank: Super Contributor
timestamp: August 06, 2014, 11:12:15 pm
content: Quote from: ice595 on August 06, 2014, 10:45:34 pmYou can use a breakout board when breadboardinghttps://oshpark.com/shared_projects/HUri2i6ROr use a schmart board for that package. You can solder it by hand with a fine tip.Or use an ina169http://www.adafruit.com/products/1164, it doesn't have a linear integrator but may be good enough.At least you have choices ;-)

author: retrolefty
author_rank: Super Contributor
timestamp: August 06, 2014, 11:29:34 pm
content: Quote from: Rick Law on August 06, 2014, 09:55:06 pmI agree with your recommendation. The ADS1115 is a nice general purpose 4 channel (2 diff) with lots of useful configuration options, esp the gain settings. The Adafruit firm has done a good job mounting this to a breakout module and writing a arduino support library making implemention pretty simple.http://www.adafruit.com/product/1085

author: GeorgeOfTheJungle
author_rank: Super Contributor
timestamp: August 06, 2014, 11:57:27 pm
content: Quote from: Rick Law on August 06, 2014, 09:55:06 pmAren't the Arduino ADC steps 5/1024? That's ~ 4.88 mV. I don't see why it would be any less accurate than that below 1V? Could you please elaborate?He just needs to amplify x133.33 as he said and a 1023 read at the ADC will mean 5 Amps, or 4.88 mA per count.

author: Rick Law
author_rank: Super Contributor
timestamp: August 07, 2014, 01:40:40 am
content: Quote from: GeorgeOfTheJungle on August 06, 2014, 11:57:27 pmIf you assume condition is perfect, you would have 4.88mV for every step. There is no ideal opamp even inside an MCU. Whatever circuits it uses, it is going to have imperfections. Random noise in and out side the circuit...Measuring equipment, power supply... they all have their imperfections.- If I have a 1V source, a 0.01V random noise pulse show up as 1% error. If I have a 0.1V source, a 0.01V noise pulse show up as 10% error.- When ADC count is 100, 1 count off is 1%, when ADC count is 10, 1 count off is 10%- It takes longer time for the ADC to measure 100 count reading than to measure 10 count. A repeating noise pulse would inject more noise (longer time) at 100 count than at 10 count.- Count is an integer. You either get there or you don't. 4.88mV per count means below 4.88mV count is 0. 4mV has a zero count, the 0.88mV error in percentage is infinite. At 9.76mV (count=2), ADC measuring just a tiny tiny bit low would have count=1, error of 100%etc etc etcIn short, I don't know exactly where the error comes from, I measured them to be such. I agree it is rather unsatisfactory, but that's the case with imperfections.Rick

author: zapta
author_rank: Super Contributor
timestamp: August 07, 2014, 02:51:10 am
content: Quote from: Rick Law on August 07, 2014, 01:40:40 amThe OP wants to measure battery capacity. That means that he will do some long term integration. Random noise with zero average will long affect the results. Also, a typically battery capacity measurement does not get to near zero current or voltage.

author: Rick Law
author_rank: Super Contributor
timestamp: August 07, 2014, 03:44:57 am
content: Quote from: zapta on August 07, 2014, 02:51:10 amTrue and true... But just to clarify, I was responding to the later question "why is the error bigger below 1V."Quote from: ice595 on August 06, 2014, 02:50:59 amI too thought about if low V really applies here. He wants to measure current with "0.1A precision" followed by the line "...with 0.1A through it...". Taking both lines together probably means he wants 0.1A precision going down to a low of 0.1A.At 5V=10A means he needs to go down to 0.05V (easier for me to think about 50mV instead).I would consider 50mV being in the low volt arena (10 to 12 ADC count of 1023) - where I would consider "wide country" instead of "habitable zone" of the ADC.Rick

author: rs20
author_rank: Super Contributor
timestamp: August 07, 2014, 05:38:42 am
content: Quote from: Rick Law on August 06, 2014, 09:40:49 pmI'm looking at your other thread where you first presented these results. At one point, you show the graph ofabsoluteerror, and it was really tiny and not at all blowing up at the left hand side of the graph. So what you should be saying here is "Indeed, a lot of that graph is illustrating exactly that. Furthermore, looking at the data before I made it look insane by dividing by x, I can see that that's 100% of the explanation for the explosion on the left."Quote from: Rick Law on August 07, 2014, 03:44:57 amOnly because your definition of "the error" is different to everyone else's and inevitably broken at small values... See the confusion and worry you're causing in this thread?EDIT: Just throwing it out there in case it's the root of the confusion; people often use % to specify ADC accuracy, but that's % of full scale, not % of measured value. Because it'simpossibleto build an ADC that's accurate to even 40% for all measured values, quantization error forbids it.

author: Rick Law
author_rank: Super Contributor
timestamp: August 07, 2014, 07:11:19 am
content: Quote from: rs20 on August 07, 2014, 05:38:42 amI do see your point. I use percent as percent of reading as typically the reading is end result, and likely displayed somewhere for use; whereas FS may not be remembered (if different dividers/multipliers deployed via a selection switch.) So while %FS is more technically definable, %reading is more "user-friendly".I did the graph so naturally it was clear to me, but I can see how others could get confused.It is also clear from some responses that some doesn't expect absolute error would increase and expected 1023 bit precision to the last count. Error does "explode" from the perspective of the user who merely read the final voltage/current reading. The noise that was nothing before suddenly becomes deadly. When you are down to single digit/low double digit ADC counts, the reading isn't so meaningful anymore. So I think while it may be confusing, it served some purpose to raise the awareness for those who hasn't attacked this problem before.Rick

author: rs20
author_rank: Super Contributor
timestamp: August 07, 2014, 10:19:21 am
content: Quote from: Rick Law on August 07, 2014, 07:11:19 amThe thing is, the error rising to infinity is purely an artefact of your definition of error, it'll happen with any ADC. In fact, the same phenomenon will occur with all sorts of things people trust, like a metal ruler. Use a ruler to measure 1.3mm, it's awfully difficult to get "10% accuracy". Use the same ruler to measure 130mm, it's incredibly easy to get "10% accuracy". This is a good principle to be aware of in general, of course, but presenting it as if its a special peculiarity of AVR ADCs is surely very confusing for people. I think most people, myself included, interpreted your message as "beware of microcontroller ADCs, they have this weird behaviour" as if it was somehow unique to the AVR or non-dedicated chips.I'm struggling to think of an instrument I've encountered in my life that displays an error as +/- x % of measured value. I totally take your point that % of FS should be left on the ADC datasheet on not presented to consumers or end-users because it could be misunderstood, I agree with that, and I haven't seen any instruments do that either. But the solution is to present error in millivolts, milliamps, millimeters or widgets, not % ofanything.

author: zapta
author_rank: Super Contributor
timestamp: August 07, 2014, 01:23:24 pm
content: Quote from: rs20 on August 07, 2014, 10:19:21 amDoesn't happen with a logarithmic ADC, right?

author: Rick Law
author_rank: Super Contributor
timestamp: August 07, 2014, 05:20:01 pm
content: Quote from: rs20 on August 07, 2014, 10:19:21 amWe are in total agreement of facts, but in disagreement of presentation. We are from very different frame of reference.First, I did not (nor mean to) say or imply "beware of MCU" when it come to measurements. I was saying: "hey, there is this habitable zone and there is this wild country, stay in the sweet zone you are fine, but assume the whole thing being flat and everywhere (Vmin to Vmax) being same, you are off for a problem."Second, I fully understand your "struggling to think of an instrument I've encountered in my life that displays an error as +/- x % of measured value." Makes perfect technology sense, but not user friendly except EE-kinds. FS%error+RD%error+CountError.My experience is mostly outside the EE world, and few if any one think of error as %FS value. My experience has been mostly :Read a number and add a tolerance (Value+-%error).This is one part that is very confusing to people not of EE background. The way DMM defines accuracy was driving me nuts.The car engine guy may think it makes perfect sense measuring speed in terms of % of max engine power, but some one walking into the shop would be totally confused.It was precisely that difference in frame of reference that I translated the data to something I encounter more in my world:LowLimit < Value +- X%error < HiLimitThis is an EE forum, of course everyone here should use an EE common frame of reference. But not everyone is there yet. The way the OP framed his original question, it looks like a post from someone like me - not already an EE expert, thus... Perhaps "more common ways" may be easier to understand.

author: GeorgeOfTheJungle
author_rank: Super Contributor
timestamp: August 07, 2014, 10:03:18 pm
content: Quote from: Rick Law on August 07, 2014, 01:40:40 amI see your point. The imperfections are specified in the datasheet:• 10-bit Resolution• 0.5 LSB Integral Non-linearity• ± 2 LSB Absolute AccuracyYes, it´s a good idea to be aware.

author: ice595
author_rank: Regular Contributor
timestamp: August 09, 2014, 06:33:09 pm
content: since all I wanted to do was to make a battery capacity meter, can't I just make a constant current dummy load with a stop watch timer?for example, if I want to discharge a lithium ion cell from 4.2v to 3.0v, can I just start the timer and stop the timer when it reaches the 3.0v threshold then multiply the constant current value by the discharge time in hours then get the amp-hour value of the battery?

author: David Hess
author_rank: Super Contributor
timestamp: August 09, 2014, 07:49:09 pm
content: That will work but may not accurately represent the characteristics of the load. Switching power supplies provide a constant power load (negative resistance), linear supplies provide a constant current load, and some loads are resistive so the current and power drop as the voltage drops. Any of them used consistently will measure battery capacity.Batteries are usually specified with constant current loads.