Arduino power saving options and ISRs - Page 1
Forum section: electronics / general

author: Peabody
author_rank: Super Contributor
timestamp: January 04, 2019, 12:03:30 am
content: I've used TI MSP430 in the past, but have my first Arduino Nano clones on order, and have familiarized myself with Energia, which is a port of the Arduino IDE for certain TI parts. So I'm starting to feel at home with Arduinos. But the thing that I'm having trouble with is the power consumed by the Arduinos. I'm working on a project that will be battery powered, probably 9V, and I see videos on Youtube that say Nanos draw 17 ma when doing nothing, and Unos twice that much (don't really understand why there should be any difference).So in addition to desoldering the power LED, I want to ask about slowing things way down in an effort to reduce the current draw. My project includes a rotary encoder with push button and 7-segment displays multiplexed by segment, and for those things I need to service them every 2 ms. But the processing will only take a small percentage of that time, so I would like to do this:Change the system prescaler to 16, producing a system clock rate of 1 MHz instead of 16 MHz.Change the timer0 prescaler to 8.Disable the overflow interrupt that drives the millis stuff.Enable the CTC interrupt for the OCR0A register, and write 250 to that register, and change the mode so that the timer overflows at 250 rather than 255.I think that would give me an interrupt every 2 ms exactly (16MHz/16/8/250). Will the Arduino IDE let me do all of that? I don't think it will let me change the vector for the timer0 overflow, but I'm hoping it will let me disable that interrupt and change the other registers. Will all this save very much power, or is so much power consumed by the voltage regulator and the CH430G USB adapter that any savings are just not material?Then the other question is whether I can put all of my servicing code into the ISR, and leave loop() completely empty. All the the variables could be global. It seems that I should be able to do that, but I'm not sure whether any mysterious C or Arduino stuff is going on in the background that I don't know about.And finally, if I can put all my stuff in the ISR, is there any way to turn off the cpu between interrupts? Looking at the datasheet, it looks like the first level of low power operations shuts down CLK i/o, but I need that clock for the timer to generate the interrupt. If the ISR only takes 5% of the time to do its thing, what can I do that would materially cut power consumption during the other 95% of the time? This is child's play in assembler on the MSP430 parts, where I can disable the processor between ISRs, but I don't see how I would do that on the 328P without also disabling the timer - unless I'm reading the datasheet wrong, which is certainly possible..I would appreciate any guidance as to what can actually be done along these lines.

author: westfw
author_rank: Super Contributor
timestamp: January 04, 2019, 03:23:35 am
content: Sure. But you'll have broken most of the Arduino library functionality (and it seems like you know what you're doing without it), so why are you even using it?Yes, depending on which interrupts and what version of "off" you mean. The AVR has about as many sleep modes as the MSP, including the ability to power down individual peripherals. See especiallythe Narcoleptic library.Like the MSP, it's generally considered "best practice" to run at faster speeds for less time, as opposed to slowing down the clock.That too. For minimal power consumption, you should get a "Pro Mini" clone, which doesn't have any of those.QuoteThe Uno has additional circuitry for automatic power switching, more LEDs, different regulator and USB chips...

author: battlecoder
author_rank: Regular Contributor
timestamp: January 04, 2019, 03:29:40 am
content: I'd recommend you to watch this video on deep sleep:Hope that helps!

author: Peabody
author_rank: Super Contributor
timestamp: January 04, 2019, 05:00:18 am
content: Thanks very much for the replies. I just found out that I have to be away for a couple days, but will go over everything again when I get back. But it still looks like there's no way to have the timer0 interrupt wake the processor from sleep.

author: sleemanj
author_rank: Super Contributor
timestamp: January 04, 2019, 05:20:40 am
content: Ok, so firstly for power consumption, the standard Nano (and Uno and ....) stuff is terrible if you want low power, it's not that the AVR itself sucks juice, but especially the regulators they use on them do, even if you don't use them.They use the AMS1117 series generally and these include an internal diode from output to input, so even if you "bypass" the regulator and inject your 5v or whatever directly, they are effectively powered and their quiescent will still be drawn, and that quiescent is CONSIDERABLE (like 30mA from memory, it's a lot, perhaps more so with cheap chinese ones).Then you have the power leds, and the activity leds, and the USB-UART chip, there is a lot of waste.Until you get rid of those things any change you make via software, is a drop in the ocean, simply inconsequential.For off-the-chinese-shelf buying, the Pro-Mini is a little better because it ditches the USB gubbins, but it still has that hungry LDO, although you can easily desolder (or just break it's legs), same for the power LED.A while back I made aNano footprint boardbut without all the junk (no USB, uses low quiescent regulators, 3v3 regulator can be disabled entirely, no leds), and in forever sleep with most everything off it takes about 10 microamps, which is good enough for me.I also wrote a library to make sleeping a bit easier:https://github.com/sleemanj/SimpleSleep/blob/master/README.mdCode:// ExamplesSleep.deeplyFor(1000);Sleep.lightlyFor(1000);Sleep.idle();Sleep.forever();For "do something when you hit the button" type things, sleeping forever is a good way, and put your "button" on reset, so you hit the button, your thing resets, does it's thing, and goes to sleep.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: January 04, 2019, 09:28:12 am
content: Quote from: sleemanj on January 04, 2019, 05:20:40 amNah, their quiescent current isn'tthatbad! Actually about 10mA, according to a brief poll of datasheets of some AMS1117-5.0 on LCSC.A CH340 will use up to 30mA when working, and 200uA when in suspend mode (i.e. USB disconnected). I don't know what the ATmega16U2, as used for the USB interface on genuine Arduino Uno R3 boards, consumes - I'm sure someone must have documented it somewhere that will come up on Google.Quote from: sleemanj on January 04, 2019, 05:20:40 amAs a corollary to this, I measured the other day an ATmega32M1 in 'power down' sleep mode, with all peripherals (UART, SPI, ADC, etc) turned off, as using 11uA.

author: Peabody
author_rank: Super Contributor
timestamp: January 05, 2019, 09:45:25 pm
content: If I'm reading the datasheet correctly, there are Sleep options that shut down the CPU but allow awakening by a timer interrupt. But I may need to use timer2 for that to get the most benefit.But as you guys have pointed out, there can't be a good low-power option if the regulator has minimum load current or quiescent current spec in the 5-10 ma range. I'm fond of the MCP1702 LDOs (US$.49 at Digikey), with quiescent current of 2 uA, and they provide more than enough current for this project. So assuming I can make material power saving inroads inside the MCU, it does appear that the Pro Mini is the better choice, or maybe just a bare 328 chip. Of course that would also require a USB adapter. I guess there are no perfect options.

author: Kasper
author_rank: Frequent Contributor
timestamp: January 06, 2019, 06:45:48 am
content: Have you considered running it without a regulator?If you do use a regulator, 9V battery is wasteful with an LDO to 3.3V or 5V that arduinos typically use. If you have a switching regulator your efficiency can be better than LDO but they generally have more quiescent leakage.

author: Peabody
author_rank: Super Contributor
timestamp: January 06, 2019, 06:32:38 pm
content: Quote from: Kasper on January 06, 2019, 06:45:48 amIt's going to be driving an SSR. And some SSRs that claim to work down to 3V don't actually do that. So I think I need 5V. Of course it could be powered from USB, and then current use won't matter. But since mains power is remotely involved, I just would prefer battery power. I understand that 9V with a linear regulator is not efficient, but it just depends on how much current is being drawn. I'm going to have displays that use about 4 ma, plus whatever the SSR needs, which is effectively an LED, plus a couple other LEDs with 2.2K resistors.I'm just going to have to wait until the Nano arrives and see what I can do. I think slowing the clock way down will help, and there are all the other adjustments to reduce power. But in the end it's a question of how long the battery will last.

author: Peabody
author_rank: Super Contributor
timestamp: January 11, 2019, 03:23:41 pm
content: I need to ask again about the AMS1117 5V regulator in the Nano. Looking at the datasheet, it has minimum current and quiescent current of 5ma. I think that means that anytime the regulator is powered, it's dumping 5ma through its ground pin no matter how much or how little regulated current is being produced. Is that right? Or does that waste go away if it's providing more than 5ma of output to the circuit?If I provided another source of regulated 5V and connected that to the 5V pin on the Nano, which connects directly to the output pin of the 1117, with nothing connected to the 1117's input, would I still get 5ma flowing back through the 1117's output to its ground pin?

author: boffin
author_rank: Unknown
timestamp: January 11, 2019, 04:12:39 pm
content: Why not just use the chip directly (you can test on an Arduino, and then just burn the chip directly), and toss the rest of the circuitry. I use an Uno for initial testing of stuff, and then just burn ATMegasThe ATTiny or ATMega chips aren't too bad for consumption, and can be put into a sleep mode. The only real issue is going to be the 9v -> 5v regulator; there has to be better things out there than the 1117 that the Uno/Nano use; perhapshttp://www.ti.com/product/LM2936??

author: sleemanj
author_rank: Super Contributor
timestamp: January 12, 2019, 02:02:03 am
content: Quote from: Peabody on January 11, 2019, 03:23:41 pmYes.The 1117 is common as muck and can supply a reasonable amount of current, but not designed for minimal power consumption.If you are supplying 5v directly, simply cut all the legs of the 1117 or desolder it then you have solved that problem.

author: Peabody
author_rank: Super Contributor
timestamp: January 12, 2019, 03:16:54 pm
content: Quote from: boffin on January 11, 2019, 04:12:39 pmThe user of this circuit may need to change parameters in the firmware, so it needs to remain in a flashable configuration. So I think it needs to be an Arduino, not just the MCU.QuoteMaybe I misunderstand, but it looks to me like the ATMega 328 is pretty bad for current draw. In the the Deep Sleep Tutorial video linked in Battlecode's earlier response, it shows the 328 drawing 13ma while running normally, but with the blink LED off. And that's after making all the I/O ports non-floating. Of course you can put it to sleep, but when it's active it looks like it's a real power hog. The MSP430 chips i grew up with I don't think would have ever drawn as much as a milliamp in that situation.If it works out, this is going to be a very low cost, simple, reflow controller that could be used by a variety of people. So it just needs to be an Arduino. I had thought about making it a TI board, particularly since the Energia IDE is very very close to the Arduino IDE, but it would require a Launchpad to flash the MCU. Also, it seems many SSR's that claim to work on 3VDC actually require 5V, and the MSP430 stuff is all 3.3V. So, as I said, it needs to be a 5V Arduino. But the current draw, even from just the 328, is a real surprise.

author: boffin
author_rank: Unknown
timestamp: January 12, 2019, 05:31:17 pm
content: Quote from: Peabody on January 12, 2019, 03:16:54 pmDead easy to re-flash it If you use a socketable 328, you just pull it, drop it into a programmer (or another Arduino) and burn itQuoteQuoteThe ARDUINO (ATMega + accompanying chips) are bad.The ATMega328P by itself is pretty decent. In power-down mode it can go down to as low as 0.1uA; in Power Save it uses 0.8uADatasheet here:http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf