Measuring single-digit mA on microcontroller - Page 1
Forum section: electronics / beginners

author: carl0s
author_rank: Unknown
timestamp: March 04, 2022, 11:53:37 pm
content: Hi. I have a little Arduino project and I want to get low power battery usage. Standard stuff.I wasn't expecting to have so much trouble when it came to actually getting current readings though.I have removed LEDs from the small Arduino Pro Mini 3.3v board, removed the regulator and replaced with an MCP1700.I am running off 4x AA alkaline batteries in a series battery holder.The device is an ATmega328P, with the aforementioned MCP1700 LDO regulator, an ST VL53L1X time-of-flight distance sensor which is doing one-off readings (rather than continuous mode), and an RFM95W HopeRF 868MHz LoRa transceiver. The project is an oil level sensor that I intend to mount in the top of our home kerosene heating-oil tank.I am using the deep sleep mode of the MCU in between readings. For testing, I am waking every second to take a reading and immediately send a Lora packet. I am sort of expecting that the current draw at sleep could be as low as 10uA, and while sending Lora might be 50 - 70mA.Using my UT61E meter to monitor current in mA yields no joy, and it over-ranges in uA mode. With the DMM in series, the MCU seems to reset at the end of the main loop - just as the RF transmission happens, which requires the largest increase in power supply.My decades-old ISO-tech IDM67 is hopeless too.I read about using a shunt resistor, and I just about understand the idea - the voltage drop and ohm's law, but I only just about get it. The lowest resistors I could find on hand were 10 ohm 0.6w. I have placed two of them (in parallel with each other, so 5 Ohms now), in series with the battery +ve, and then I have put my scope across the resistor. Yes I did make the ground-loop mistake. I thought I would be OK because the DUT is using batteries, but the TTL -> UART-USB that is plugged into my PC for serial monitoring caused a problem. The resistors nearly burned up. Lucky escape for my newish ~£1,500 'scope and my PC huh. That is another question I have - once I do get this stuff figured out (the shunt resistor, amplifier ? stuff), can I get a cheap-but-ok (£100) differential probe and use that, or would a budget differential probe not be compatible with something like Dave's µCurrent GOLD or a DIY equivalent? or should I just use two probes? For now I have been resorting to unplugging the USB-TTL adapter every time I connect the scope probes, and disconnecting the scope probe BNC at the front of the scope every time I need to re-program the board.My questions are:If I'm using a 5R resistor and my 'scope (SDS2104X upgraded to 500MHz etc). is showing 33mV at MCU sleep, jumping to 868mV during RF transmit, how do I convert that into an understanding of the current draw. I = V/R, so the current is 33/5 ? mA? That would mean 6.6mA at sleep (believable, not what I wanted though), and 173mA during transmit. Also much higher than expected.I presume there's a reason why Dave's µCurrent GOLD has a special amplifier in it. Is this vital to get proper readings? I also came across this article, which again using a very expensive amplifier:https://dorkbotpdx.org/blog/paul/measuring_microamps_milliamps_at_3_mhz_bandwidth/As above.. can I use a differential probe with this stuff as well to make life easier? Save me having to disconnect stuff, or figure out the math (2 ch) stuff on my newish 'scope.Any simple rules to follow regarding the resistor values, and whether I should take the low or higher voltage readings with a pinch of salt, etc. ?cheers,Carl

author: carl0s
author_rank: Unknown
timestamp: March 05, 2022, 12:26:40 am
content: Having watched Dave's original KickStarter video for the uCurrent Gold, I see why the amplifier is needed - to get outside of the 'scope's noise area.Also, I'm wondering if my maths above should have been 0.033/5 and 0.868/5, or just expressed the result in uA instead of mA since the volts were actually mV instead of volts.That would mean 6.6uA in deep sleep (ideal, but seems unlikely that I would get the ideal numbers at this stage), and 173.6uA during peak radio transmit (not possible, data sheet says more like 29mA at 13dBM).

author: rstofer
author_rank: Super Contributor
timestamp: March 05, 2022, 12:34:47 am
content: Quote from: carl0s on March 04, 2022, 11:53:37 pmYou posted just as I finished this:The units seem to be messed up. One place you are talking about 33 mA and then you're talking about 868 mV. I'm not sure what to make of 33 mA / 5 Ohms. I could see 0.033 A * 5 Ohms giving 165 mV but I'm not sure that is useful information, you already have the current.You want to use as little resistance as possible because you're giving up voltage drop as a function of current. OATOH, you want to use the full range of the ADC. That's why folks sometimes use an op amp to amplify the voltage to use full scale in the ADC.Chapter 4 in "Op Amps For Everyone" explains how to offset and scale using an op amp.https://web.mit.edu/6.101/www/reference/op_amps_everyone.pdfYou probably want to use a rail-to=rail op amp so you can deal with 0V input and output. You should probably max out at something less than Vcc so, maybe 3V on a 3.3V MCU. Then you can use a 3.3V op amp. I'm assuming your MCU is the 3.3V variant but if it is 5V, change the numbers accordingly.

author: carl0s
author_rank: Unknown
timestamp: March 05, 2022, 12:46:31 am
content: Apologies, yes I did mean 33mV. I was referring to the voltage readings on the oscilloscope. I'm unsure how to translate them into current readings for a the 5 ohm resistor value.Thanks for the op amps PDF link!

author: carl0s
author_rank: Unknown
timestamp: March 05, 2022, 01:37:07 am
content: Quote from: rstofer on March 05, 2022, 12:34:47 amIs that to say: You want to minimise voltage drop as much as possible, so that the circuit still receives its minimum voltage requirement (I already knew this bit). But smaller voltage drop = a smaller signal (voltage drop) to try to examine on the 'scope, so you have to tune down into the lower v/div range of the scope, where more noise is evident.It seems obvious now that I write it.Are there any other issues with large voltage drop? Can I not just increase the supply voltage so that the DUT receives its required voltage, post-drop, and my scope sees a higher fluctuating voltage drop?

author: ledtester
author_rank: Super Contributor
timestamp: March 05, 2022, 03:35:04 am
content: Perhaps you should clarify what your test set up is with a schematic.You mention "battery powered", "usb ttl" and "differential probe".Will your battery powered device be connected to USB out in the field?QuoteWhere in the circuit did you place the current shunt resistors?Here is a good, accessible article by Saleae on how to measure current with an oscilloscope:https://articles.saleae.com/oscilloscopes/how-to-measure-current-with-an-oscilloscopeAt the end it covers how to safely measure current with two oscilloscope probes using the scope's math function. They call it a "differential probe setup" but there is no actual differential probe used. A real differential probe costs upwards of hundreds of dollars.

author: Kleinstein
author_rank: Super Contributor
timestamp: March 05, 2022, 09:07:22 am
content: Quote from: carl0s on March 05, 2022, 12:26:40 am33 mV/5 ohms is 5 mA as the current for the low current phase (the scope would not be good enough to measure 0.033 mV). Similar the 868 mV/5 Ohms is 173 mA. So the current consumption is quite high, more like higher than expected.For the measurement with the shunt one may have to use a little more than the later planed 3 V (e.g. more like 4-5 V or 3 xAA) to have room for the votlage drop. The regulator would have to work hard to keep the voltage to the circuit stable, but should at least keep the voltage resonable stable. It could be a good idea to add some capacitor (e.g. some 100 µF) after the shunt, so that the voltage does not change too fast for the regulator. This would smooth change incurrent but should still allow to get the current reasonable separate.The typical points to cause higher current needs are open inputs at the µC and maybe some HW parts that are not properly shut down before going to deep sleep. Another point can be different supply levels resulting in boarderline logic levels that increase the current on CMOS inputs. Even 74HCT ... can draw more current, when the logic level is "only" 3.3 V.For testing a measurement every second looks OK, later longer pauses (e.g. 1 min) are likely still OK.The measurment at the shunt is one of the few cases where a 1:1 probe is OK. One would usually only need a reduced BW anyway. For the µA range current one would need a larger shunt or an extra amplifier. A seprate measurement with a high value shunt (depends on the current to measure) and parallel diode may be an option here. So the voltage drop would be limited to some 0.6 V for the higher current phases.

author: carl0s
author_rank: Unknown
timestamp: March 05, 2022, 09:57:33 am
content: Quote from: ledtester on March 05, 2022, 03:35:04 amHi. No the USB TTL is just for programming the microcontroller (which happens frequently during testing and development of course!)I know about the 2-probe math method, although haven't done it yet.The "cheap but ok" differential probe I was referring to was the Micsig ones for ~£120 GBP / $160 USD. e.g. DP10007Thank you for the link! Another one to read over the next few days.It has just occurred to me that the book I got for Christmas -The Hardware Hacking Handbook by Colin O'Flynn and Jasper van Woudenbergshould cover practical ways to analyse this quite well. It's all about microcontroller power analysis and watching the current draw as certain CPU instructions are executed (checking a condition, performing encryption/decryption, etc.). I need to start reading it from the beginning!

author: carl0s
author_rank: Unknown
timestamp: March 05, 2022, 10:07:12 am
content: Quote from: Kleinstein on March 05, 2022, 09:07:22 amThank you Kleinstein.I am feeding 6v to the regulator. This is where I have the shunt as well.I had the 'scope probe in 10x mode (and 'scope set to match of course). I should look at the difference in 1x.Thank you for the ideas. I did try setting the pins. I read somewhere that setting them to just INPUT (on ATmegas328) is fine, has same effect as setting them to OUTPUT, but I'm not sure. I definitely read that they should be set to OUTPUT when I first started looking into this, but then I came across test results showing that INPUT was fine. I quickly did a (i = 1 to 20, pinMode(i, INPUT)) and it make little difference. Blanket setting all pins to OUTPUT broke SPI to the radio module and i2c to the distance sensor. I need to write proper code to exclude the in-use pins. I was rushing though.I may just make that adapter thatPaul S (PJRC/Teensy)made. I hadn't realised who it was at first.. £25 for the ADC chip is a lot though!

author: Kleinstein
author_rank: Super Contributor
timestamp: March 05, 2022, 10:35:36 am
content: Setting the pins to input can lead to a slightly higher current. One could use the mode with input and pull up.

author: carl0s
author_rank: Unknown
timestamp: March 05, 2022, 11:01:47 am
content: Thanks, I will try!Also, my shunt has been on the +ve. I'm reading that it would work better if it was on the -ve. I just didn't physically have room for it there on the breadboard yesterday but I will rejig the layout and try that too.

author: Ian.M
author_rank: Super Contributor
timestamp: March 05, 2022, 11:05:38 am
content: Take a look at theµCurrent schematic. Note how the negative J2 (current input) jack connects directly to the negative J4 (voltage output) jack. The *ONLY* way to use a µCurrent safely with a grounded scope and no differential probe (and without resorting to poor man's differential probe using two channels and waveform math) is if you keep those µCurrent -(black) terminals grounded.If you had a µCurrent, for your single battery setup, you'd need to ignore the output - terminal J4 (to avoid ground loops), connect scope and load ground to input - J2, (keeping the load ground wire as short as practically possible), connect the scope probe tip to output + J3, and connect the negative side of the battery (or floating PSU) to input + J1. Select Invert on that scope channel as positive load current will give negative output from the uCurrent. You could then safely connect programmers and other grounded test gearSimilar restrictions apply to any other current sense amplifier that has OPAMP circuit ground connected to one end of the shunt resistor.If you want to measure anything other than current to/from a ground node, you need a differential instrumentation amplifier for your current shunt which will give you a ground (or local 0V) referenced output voltage. Unfortunately high accuracy ones with good CMRR don't come cheap, and building your own without resulting to hand-trimming or select on test to calibrate the gain and get good CMRR is not easy. When you look into this in any detail, it makes Paul S's AD8428 setup look very reasonable, in spite of the disadvantages that its fixed gain x2000 only and will be damaged if the voltage across the shunt ever exceeds 1V.For minimum power consumption, unused pins on your MCU should either be set output low or input with pullup. Output low is preferred because its less sensitive to EMI, however as you have found, its only appropriate for UNUSED pins. If you loop and set all pins input + pullup, before any other hardware initialization, then override that as required to access whatever peripherals you shouldn't have any conflicts. If you want to use output low, you need to keep track of which pins must be left alone. If you are powering down an external peripheral, its output drivers *should* hold input pins connected to it low enough, but worst case, you may need to add external pulldowns on those input pins, or if you are feeling brave, switch them to output low after the peripheral has powered down and its Vcc has had time to decay to near zero, and switch them back to inputs before powering up the peripheral.

author: rstofer
author_rank: Super Contributor
timestamp: March 05, 2022, 08:46:53 pm
content: Current measurement is best done on the high side so as to keep all ground connections at ground rather than floating upward because of the sense resistor. There are different gain configurations available. You need to do the math to determine which variant to buy.https://www.ti.com/product/INA293The device is in stock at DigiKey (surprised me!). Gain is by part number.There's a somewhat pricey evaluation module with a cluster of devices with different gains so you can play around with different resistors and gain.https://www.ti.com/lit/ug/sbou225a/sbou225a.pdfhttps://www.digikey.com/en/products/detail/texas-instruments/INA293EVM/11502312Bigger surprise: it's in stock at DigiKey although there is one less than there was a minute ago. This could be a handy gadget to have on the bench.

author: Renate
author_rank: Super Contributor
timestamp: March 05, 2022, 11:27:15 pm
content: I'm fond of the INA226 with I²C interface.Showing zero at Digi-Key.

author: rstofer
author_rank: Super Contributor
timestamp: March 05, 2022, 11:57:38 pm
content: One problem with the INA923 approach is that it only has a gain of 500 (max). Assuming a 1 Ohm resistor, 100 uA yields 100 uV which when multiplied by 500 is only 50 mV and that doesn't spread very far on an ADC. I suspect another op amp would be used to multiply this up by another 500 to get up around 2.5V.The AD8428 solution may be better since it has more gain but I haven't looked at it.

author: Ian.M
author_rank: Super Contributor
timestamp: March 06, 2022, 12:59:47 am
content: OTOH your suggested eval board has two modules of each gain from x 20 to x 500 in 1,2,5 steps, and I don't see any reason not to cascade two of them, so for $25 + either a PP3 battery & clip or a USB power lead, you can do exactly what you suggest with the two x 500 modules, for a total gain of x 250K.

author: RJSV
author_rank: Super Contributor
timestamp: March 06, 2022, 08:42:42 am
content: Uhhh, (Kleinstein showing good advice, there).Uh, but I'm wondering, every little detail, here, is getting 'pumped up'...(oh and there's a bunch of smoke, coming off it...), ...Things like that.My suggestion / question:For Ball-Park type measuring, what about, just a simple DMMI mean, who cares, if your 6.8 mA shows as, 6.5 ma ?You are using a waveform display, for a supply current,and wouldn't a simple capacitor aid that METER readings ? (If the +supply is following the load too much). And by the way: A hand-held DMM IS going to be a 'differential' style voltage reading.On that vein, you 'touch' on ground loops, then it's OFF to 'another' mystery.You've got ADD, worse, even, than me...But I can be a sour-mouth, so apologies there.Um, maybe Klienstein's advice would be good to focus. JUST SEE, if you can point to, like 3 essential questions/issues. Way too confused to start building a 'special probe /amplifier'.I think you need an isolated, inner ground plane, plusa double-sheild coax...No, TRIPLE SHIELDED.

author: Kleinstein
author_rank: Super Contributor
timestamp: March 06, 2022, 08:55:59 am
content: Just the meter is OK to measure the current in the standby mode. It may still need the measurement before the regulator and a diode to limit the voltage drop for the start and a change in the program, so that the µC is in standby more much longer. One may have to use an external shunt if the DMM is not working for a current in the 1-50 µA range (some cheaper meters may not have enough resolution) - this is still not a big deal with a resistor in the kOhms range. With the measurement before the regulator I see no need to have an extra amplifier - a DMM in mV range or the scope should be good enough.For the active phase just a shunt and the scope with 1:1 probe should be good enough. The current may very well be time dependent, and besides the actual current one would also need the time the circuit is using the higher current.An alternative solution is to have a rather large buffer capacitor (e.g. 10000 µF) and supply the current via a relatively large resistor. With enough capacitoance the DMM should read the average current. One still has less information as one does not get the currents for both parts separate.

author: Renate
author_rank: Super Contributor
timestamp: March 06, 2022, 11:39:14 am
content: I just re-read the OP.The goal being to measure power consumption on a µC/SOC/device.This really is a case where accuracy does not matter, 5% wouldn't kill you.What you are looking for is surprises, the device not sleeping, waking up, doing high current things.I have a setup that I use for Android devices using an INA226 and a ATMega32u4 over USB HID to a PC. This goes to >2A. My resistor is 0.050 ohm, i.e 50 milliohms.Here's a plot of an old Nexus 7 booting up. (It also explains why many "boot loops" are in fact, weak batteries that buckle under the heavy power consumption of start-up.)This plot doesn't show it, but with the device screen off, I can see all the times that it is waking up with no apparent (to me) reason.

author: rstofer
author_rank: Super Contributor
timestamp: March 06, 2022, 04:04:12 pm
content: The INA226 Evaluation Kit is out of my price range but in stock at DigiKeyhttps://www.digikey.com/en/products/detail/texas-instruments/INA226EVM/2749419More fun is the Input Offset Voltage bar chart in the datasheet section 6.6https://www.ti.com/lit/ds/symlink/ina226.pdfWho said statistics couldn't be fun?The INA226, by itself, is not in stock at DigiKey. Nor is it in stock at Mouser.

author: rstofer
author_rank: Super Contributor
timestamp: March 06, 2022, 04:14:32 pm
content: Adafruit has an I2C breakout board for the INA219 that I haven't studied. It is in stock at Adafruit and Mouser for $10https://www.adafruit.com/product/904https://www.mouser.com/ProductDetail/Adafruit/904https://www.ti.com/lit/ds/symlink/ina219.pdf

author: Terry Bites
author_rank: Super Contributor
timestamp: March 06, 2022, 06:23:36 pm
content: Use a 1R shunt and a MAX4173T (or variant to suit) right on the arduino board- happy to monitor currents over 1MHz. CMV up to 28V on a 3V supply. Lots of stockists.

author: RJSV
author_rank: Super Contributor
timestamp: March 07, 2022, 05:37:28 pm
content: I like reading Kleinstein's approach, here is some notes:I figured, put a 0.58 ohm resistor, from battery +, then any voltage across that, using typical DMM, you just need to divide by 2,I.E. Ohms Law, I = V / R (just simplify, and that's V x 2)So, in the SLEEP MODE, my guess is 200 microA,and that would read as V= 00.1 mV as that's the lowest, rock bottom scale, lacking any amplification.But all that is very, very fine compared to ball-park ranges you need to verify.In other words, that '00.1 mV' could be '78 micro V', with rounding (in the DMM).Photo diagram shows what Kleinstein meant, for not dropping the supply + too much, so regulator stays healthy. That reduces your voltage, for conversion to calculate current (DMM on 'mV' range or lower).But that's OK, because, in simple view, you've got to run the regulator. AND, probably you might notice: No need to run around putting in a replacement, LDO regulator, if you can live with a low current reading that's, well, good enough.Just, as diagram shows, check what sorts of NOISE can be seen, right there at the PC Board power input.

author: carl0s
author_rank: Unknown
timestamp: March 07, 2022, 06:21:09 pm
content: Thanks for so many ideas, and so much additional information as well.Another thing I didn't consider, when comparing the numbers I was seeing to what I might expect from the ATmega328p in sleep, and the HopeRF module transmitting, vs the data sheets, is that I am monitoring the current on the 6v supply to the regulator. So of course these numbers will be quite different to the @3.3v figures from datasheets.

author: RJSV
author_rank: Super Contributor
timestamp: March 07, 2022, 06:47:46 pm
content: ...Don't forget,... Many of us like to hear about your progress, on the CODE WRITING aspects !And, that means, a bunch of questions, you could help explain...Meanwhile, I can't resist, using SEARCH, to check out what that 'time of flight' sensor is...hmmm

author: ledtester
author_rank: Super Contributor
timestamp: March 07, 2022, 06:57:26 pm
content: Quote from: rstofer on March 06, 2022, 04:04:12 pmI always wondered about why chips which are out-of-stock at the major distributors seem to be in plentiful supply at aliexpress, e.g.:Are they knock-offs? rejects? an employee running off some extra wafers to sell on the black market?

author: rstofer
author_rank: Super Contributor
timestamp: March 08, 2022, 02:42:50 am
content: Quote from: ledtester on March 07, 2022, 06:57:26 pmProbably some combination of all of the above. I'm betting on floor sweepings...

author: carl0s
author_rank: Unknown
timestamp: March 08, 2022, 08:08:00 am
content: Quote from: RJHayward on March 07, 2022, 06:47:46 pmI will post some info once I've made a little more progress, and caught up on sleep & work.For now I just want to be able to use my tools properly.I do want to use the oscillocope. I want to see the spikes when the range sensor takes a reading, when the radio transmits, and when the MCU enters / exits sleep. Why not 'see' it since I have a nice screen?Last night I read about how linear regulators work. Somehow I thought they magically turned volts into amps, so I thought running with a 6v (4x AA alkaline) supply to the regulator was good. It's not. So I will re-think the battery type. I want to get an easy 2-years battery life if possible, taking into account self-discharge as well.As it happens, I could run power to this location, but we have two fairly large fields here and my girlfriend is starting all sorts of planting things. So distant moisture sensors and stuff could be future projects. Will be useful to get some ultra low power things worked out.

author: Kleinstein
author_rank: Super Contributor
timestamp: March 08, 2022, 08:56:10 am
content: For testing the 6 V supply is OK, as it gives the regulator some headroom to compensate for drop at the shunt.For later use I would consider 2xAA and thus around 3 V. The mega328 should be OK with 2.7 V and in a more moderate temperature and clock range also less voltage. I tested one AVR (tiny2313) to lower voltage and it worked down to about 1.5 V, though the specs were for 2.7 V only. So chances are the 2 x AA could be used to about empty.For measuring the current supply, one could really use the 2 cases separate:The low power mode can be measured slow, with a relatively large shunt + parallel diode for startup.The higher current part would be with a small shunt resistor (e.g. some 1-10 Ohms) and than the scope. With suitable chosen resitor value one should get away without an extra amplifier.The regulator can compensate for much of the voltage drop. With a CMOS linear regulator the current at the input side is essentially the same as at the output side plus a tiny bit of no load current for the regulator.