The Vera Community forums have moved!

Advanced => Plugins & Plugin Development => Programming => Arduino Sensor Plugin => Topic started by: a-lurker on December 26, 2013, 02:07:24 am

Title: Battery powered Arduino sensors
Post by: a-lurker on December 26, 2013, 02:07:24 am
I've set up a temperature sensor running off a couple of AA batteries. Looks like the battery lifetime will be a year or more. However, I would like to monitor the battery Voltage as well as the temperature, just to keep an eye on it. So I have set up one of the ADC inputs to sense the battery Voltage as part of the temperature sketch and that works OK.

I would like the battery Voltage to show up as a battery device in Vera, so I can feed it to dataMine as a Voltage level. Question is - what's the best practice to do this?

I've looked through the typedef enum for "variableType" but there's no battery or Voltage mentioned. The typdef enum for "internalMessageType" mentions I_BATTERY_LEVEL and I_BATTERY_DATE. But I don't know what the internalMessageType does or how it is meant to be used.

I can't find any doco on the general procotol, so how should this be all handled? Does the typedef need to be expanded? Any guidance would be useful.

Thanks.
Title: Re: Battery powered Arduino sensors
Post by: mitekdg on December 26, 2013, 03:11:01 am
Hi, could u share your scheme? I'm interesting in arduino u use, power supply how it was connected?
Thx!
Title: Re: Battery powered Arduino sensors
Post by: hek on December 26, 2013, 03:42:50 am
Use the "sendBatteryLevel"-method from your sensor. Also make sure to use the latest L_Arduino.lua plugin file as there were a bug that was fixed a couple of weeks ago.
Title: Re: Battery powered Arduino sensors
Post by: a-lurker on December 26, 2013, 07:03:16 pm
Software: Hek has already set up the software for battery operation. You can report the battery condition as a percentage (see above) and the low power sleep time can be set as well.

Hardware: I had one of these available, which is basically just a processor, a 3V3 regulator and a power indicator LED:

http://www.ebay.com/itm/Redesign-Pro-Mini-atmega328-3-3V-8M-Replace-ATmega128-Arduino-Compatible-Nano-/200914924969?pt=LH_DefaultDomain_0&hash=item2ec77675a9

It was modified for low power operation - see attached image - PCB layout varies depending on manufacturer:
1) disconnect the power indicator LED by cutting the track between the LED and the resistor in series. Saves about 1.5 mA
2) disconnect the 3V3 regulator as it's not used. Cut the Vout pin with a sharp fine wire cutter. Saves about 220  uA.
3) install the FTDI header excluding the Vcc pin connection. We don't want the PCB powered by the USB cable while programming it - a 3V3 FTDI was used - be careful you plug it in, the right way up:

https://www.sparkfun.com/products/9873

4) the device is powered by two AAs in series and connected to the PCB using one of these ultra low power step up convertor/power regulators:

https://www.sparkfun.com/products/10967

When powered down, the CPU, DS18B20 temp sensor and radio consume (very roughly) 120 uA - which is stuff all and similar to the self discharge rate of the batteries.

The battery lifetime can be calculated by determining the average current of the set up using the general equation:

I avg = (t0*I0 + t1*I1 ... + tx*Ix) / (t0 + t1 ... + tx)

Note these figures are indicative only - your results may certainly vary. Currents are measured at the battery with a battery Voltage of 3.0 V

idling:  I0 = 28 mA, t0 = 0.65 sec (using the delays I set up in the sketch)
transmitting:  I1 = 31 mA, t1 = say 50 mSec (time is just a guess)
sleeping:  I2 = 120 uA (very roughly), t2 = 15 minutes (the temperature sampling rate)

Using the above figures the effective current = 0.142 mA  The sleep current is difficult to measure accurately, so this figure may vary and that would effect the lifetime calculation.

Assume the  batteries are good for 2000 mAhr, then that gives a lifetime of 14,101 hours, which equals 19.6 months. The lifetime is pretty much dominated by the sleep current and sensor sample rate. Assuming the figures & calculations are correct (let us know if they aren't), it's likely the batteries would die partly of old age rather than through actual usage.

I used a 1 M and 470 K resistor in series, connected to the battery plus and ground and connected the tap point to the A0 input on the CPU. The tap point is bypassed with a 0.1 uF capacitor to keep the noise level low, at this otherwise high impedance point. The ADC is set to use the internal reference of 1.1V - so Vmax at ADCmax = 1.1*(1e6+470e3)/470e3 = 3.44V  That then has to be converted to a useable percentage to suit Vera's battery devices.
More info here:

http://maniacbug.wordpress.com/2011/10/19/sensor-node/

This site suggests the operating current while sleeping could be lowered even further:

http://www.gammon.com.au/forum/?id=11497

It could be argued that the step up convertor is not required at all - just connect the PCB directly to the batteries. A few pro and cons:
I suppose you could try both methods and report back in a year or so!

When you think that all this sophisticated stuff can still run using a few microAmps, you really have to wonder at how science has progressed electronic devices over the last fifty years. Pity the bits are so frigging small you can't see them without a microscope, let alone trying to solder them together.
Title: Re: Battery powered Arduino sensors
Post by: hek on December 26, 2013, 07:20:48 pm
Thanks for a great post a-lurker!
Title: Re: Battery powered Arduino sensors
Post by: MDoc on January 20, 2014, 10:36:05 pm
Ditto A-lurker...Your post helped a lot.

I'm also looking at how low of current I can get.  I'm using a Pro-Mini 3.3V, with the NRF24L01 radio.
I've removed the regulator and LED... Hek is already turning off the BOD (Brown Out Detection) and the A/D Convertor in SleepDelay and SleepWDT.

Right now, I'm not using a charge-pump (using direct... two AA Batteries).  I wanted to see how low of voltage the Pro-Mini would operate.  I know the radio should be good down to 1.8V.  At 8MHz, I believe the Pro-Mini should be able to run down to 2.4V.  The problem is that once I get down to about 2.8V, it stops running.  Have you tested to see how low of voltage yours can run?
Title: Re: Battery powered Arduino sensors
Post by: a-lurker on January 21, 2014, 01:12:49 am
No I haven't tried this - I decided to use the charge pump, so I got 3V3 no matter what. I was a bit concerned my sensors would stuff up. The spec shows maximum Frequency vs. VCC line for between 1.8 and 2.7 V. The line equation is V = (F-k)/m, where from the spec k = -8 and m =6.6666 so for 8 MHz:  Vmin = 2.4V

I had this all going well for a couple of weeks and the battery Voltage was still looking really good. Then - for reasons unknown - I think the node lost contact with the gateway (gateway crashed?). It looks like the node goes into continuous TX retries at that point, which flattened the batteries in just one day  :-X

I've not looked at what the retry code does - but it looks like some code mods are required for anything that is battery powered. All needs a bit more investigation LOL

Just on reporting the Voltage, I just got the raw ADC value and integer divided by 10. So 100% in Vera = so 3.44 * 1000/1023 = 3.36 Volts


Title: Re: Battery powered Arduino sensors
Post by: veravance on January 21, 2014, 02:45:00 pm
for a-lurker
i have the same problem. My Gateway crash very often and i must reboot it.
The node with the arduino pro  goes into continuous TX retries like you.

For the Gateway crash this could be a problem with the power of the vera's usb because i have also an usb stick connected ?
Title: Re: Battery powered Arduino sensors
Post by: MDoc on January 21, 2014, 03:37:30 pm
Thanks A-lurker.

Also see that.  As the voltage drops,  at some point the transmit light starts flashing rapidly (around 2.7V in my board).... I guess this is what you're talking about.   I could see where that would suck the battery dry.

Currently (because I'm using direct batteries) I'm using the internal 1.1V reference to get my battery voltage and then calculate the % to send to the gateway.  For that calculation I need to know how low I can go (voltage) thus the reason for this test.

EDIT:  I've had no issues with my gateway.  It's just when I lower my supply voltage to around 2.7 volts, the transmit light flashes rapidly just as you saw some type of oscillation)  Once I raise the supply voltage, it stops and transmits normally.

Title: Re: Battery powered Arduino sensors
Post by: Derek Macias on January 22, 2014, 02:19:15 am
The gateway crashing could be your usb hub.  I'm on my third hub. First one seemed to get hot and was a bit bulky.  So I bought a mobile one but upon reboot the serial port wouldn't work and took too much effort to get serial to show up. Switched back to old one but it gets hot and so does Vera. I found an Inland usb hub with 3 ports and 1 usb mini plug for cameras which is perfect for arduino nano.  Now if I can just get it inside my Vera lite with the arduino and radio.

Edit: It didnt work. Usb flash drive kept dropping and vera would freeze up.
Title: Re: Battery powered Arduino sensors
Post by: korttoma on January 22, 2014, 03:12:49 am
@Dere Macias
Did you experience Arduino GW hangs on your previous hubs?
What is your experience with the hub in the picture? Is it a powered hub? Where did you find it?


I also have allot of failures of the Arduino GW and have tried 2 different hubs but both work as bad.

Is there anyone out there using the combination Veralite + USB hub + Arduino GW without any problems? In that case what kind of USB hub do you use??
Title: Re: Battery powered Arduino sensors
Post by: Derek Macias on January 22, 2014, 10:41:56 am
I just bought the one pictured a couple days ago. When I rebooted Vera the serial port was listed. It's been working fine for a couple days. I felt it this morning and it was not hot. It is not powered. I bought it at Microcenter. Just Google "usb hub with mini" and look at the pictures. I have the arduino nano with button, leds, and nrf24 radio with external antenna in a Veralite box and a 4GB usb nub plugged in.

Also my previous hub that would get hot did have an external power port but I didn't use it and it didn't hang.  The laptop hub I think was having problems.  The serial wouldn't show up most of the time on reboot also.

Edit: The one pictured didn't work out.  The usb drive kept dropping off, causing vera to freeze.  So I'm back to the 4 port powered hub that got hot. I'm going to try it powered.  I think powering the hub might be a must.
Title: Re: Battery powered Arduino sensors
Post by: p3tter on February 03, 2014, 03:12:24 pm
is anyone using the arduino nano with motion sensor and batteries?
i have 3x AA batteries connected to the 5v pin on the arduino, but the battery drrains around 3% each day.
i have disabled the red led. is it some other things i could do to get the battery last longer?
thanks
Title: Re: Battery powered Arduino sensors
Post by: RichardTSchaefer on February 03, 2014, 03:24:03 pm


It takes power to drive the Ultrasonic Sensor ... I do not know if there is startup/learning time for the sensor ..
You might power down the sensor and the Arduino and wake it up and check for motion once per second or 1/2 second.

Title: Re: Battery powered Arduino sensors
Post by: mitekdg on February 04, 2014, 05:51:39 am
Hi. can u help me plz
at http://code.mios.com/trac/mios_arduino-sensor/wiki/TipsAndTricks#Case3
there is an example:
Code: [Select]
// Send the Battery Percentage to the Gateway
#define VBatMax 3.1             // The voltage of New Batteries
#define VBatDropout  1.8    // The battery dropout voltage
gw.sendBatteryLevel((readVcc()-(Dropout*1000)) /(((VBatMax-Dropout) *10)));

What the "Dropout" is?
Title: Re: Battery powered Arduino sensors
Post by: garrettwp on February 04, 2014, 07:25:11 am
The voltage at which the battery does not have enough power to power the arduino.

- Garrett
Title: Re: Battery powered Arduino sensors
Post by: mitekdg on February 04, 2014, 07:35:11 am
I see, but there is no such a var in this code. The correct is "VBatDropout"?
Title: Re: Battery powered Arduino sensors
Post by: a-lurker on February 04, 2014, 03:14:06 pm
It should be VBatDropout, not DropOut. The code returns 100% at VBatMax (3.1V) and 0% at VBatDropout (1.8V).  readVcc() returns the current Voltage in milliVolts.
Title: Re: Battery powered Arduino sensors
Post by: MDoc on February 04, 2014, 07:43:50 pm
Yep,  My bad!  axii... can you change Dropout to VBatDropout in the calculation sent to the gateway?
Title: Re: Battery powered Arduino sensors
Post by: derrij on March 03, 2014, 03:08:54 am
It is necessary to power up the radio all the time when waking from sleep. Or may be will be better to power up the radio if only temperature has changed .
Will it save some mA? Or radio needs some time for power up befor sending data?

Title: Re: Battery powered Arduino sensors
Post by: hek on March 03, 2014, 03:27:14 am
It is necessary to power up the radio all the time when waking from sleep. Or may be will be better to power up the radio if only temperature has changed .
Will it save some mA? Or radio needs some time for power up befor sending data?

You don't need to power up radio if you're not going to send any data..
Title: Re: Battery powered Arduino sensors
Post by: a-lurker on March 15, 2014, 10:10:09 pm
Brown out Detector (BOD) problems:

According to the spec the min Voltage at 8 MHz should be:
(( 2.7-1.8 )/2)+1.8 = 2.25 V

Quote
@ MDoc:  when I lower my supply voltage to around 2.7 volts, the transmit light flashes rapidly just as you saw some type of oscillation)  Once I raise the supply voltage, it stops and transmits normally.

I ended up having this problem as the batteries got tired. These two lines of code turn off the BOD during sleep to save power. When the sleep time is up, the BOD is turned back on again. This functionality is only available for some chips. Refer to the pages 39 and 44 of the ATmega manual:

"p44: BODS and BODSE only available for picoPower devices ATmega48PA/88PA/168PA/328P"

Code: [Select]
sleep.pwrDownMode();           // set sleep mode
sleep.sleepDelay(SLEEP_TIME);  // sleep for SLEEP_TIME

When the CPU is active, the BOD is on. This file sets the BOD threshold, when the CPU is flashed:

..\Arduino\hardware\arduino\boards.txt

In that file you will find:
Code: [Select]
Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328
...
pro328.bootloader.extended_fuses=0x05

This sets the BOD threshold to 2.7V (typical). As the power supply Volts goes below 2.7V the CPU resets itself continually ie oscillates, which then results in total destruction of the battery.

The brownout threshold is found in the "Extended fuses" - only the lower three bits are used and they are mutually exclusive and active low - pages 287, 312 of the ATmega manual:


Not absolutely sure but I think that the unused bits always read high - however it doesn't matter what you program them with. So you can use either of the hex values listed above or other mixtures - where the unused bits are either high or low. It's only important to get the 3 lowest bits correct.

So to get the CPU to work down to 2.25 V (using an 8 MHz clock), then the fuse should be changed to:

0x07 to disable BOD
0x06 to change BOD threshold to 1.8V

As the the BOD is not much use on a battery powered device, it's perhaps best to disable it completely using 0x07? Suggested addition to the boards.txt file (untested).

Code: [Select]
##############################################################

pro328pBODoff.name=Arduino Pro or Pro Mini (3.3V, 8 MHz, BOD off) w/ ATmega328P

pro328pBODoff.upload.protocol=arduino
pro328pBODoff.upload.maximum_size=30720
pro328pBODoff.upload.speed=57600

pro328pBODoff.bootloader.low_fuses=0xFF
pro328pBODoff.bootloader.high_fuses=0xDA
pro328pBODoff.bootloader.extended_fuses=0x07
pro328pBODoff.bootloader.path=atmega
pro328pBODoff.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
pro328pBODoff.bootloader.unlock_bits=0x3F
pro328pBODoff.bootloader.lock_bits=0x0F

pro328pBODoff.build.mcu=atmega328p
pro328pBODoff.build.f_cpu=8000000L
pro328pBODoff.build.core=arduino
pro328pBODoff.build.variant=standard


A few other ideas:
It probably makes sense to consider the actual CPU Voltage as the battery Voltage and use the technique as applied by @MDOC here to measure it:

http://code.mios.com/trac/mios_arduino-sensor/wiki/TipsAndTricks#Case3

If a battery step up convertor is used then the actual battery Voltage could just be returned as an interesting variable to plot. It's the CPU Voltage that actually matters, no matter how it is supplied.

It is probably also advisable to send the Voltage just after having transmitted the sensor values. In that way, you get the Voltage after the battery has been hit hard by the transmit current. If you read it just after the sleep time, the battery has had time to recover.
Title: Re: Battery powered Arduino sensors
Post by: a-lurker on March 20, 2014, 10:27:07 pm
A quick note on the code found here:

http://code.mios.com/trac/mios_arduino-sensor/wiki/TipsAndTricks#Case3

The constant 1126400L should be 1125300L.

1.1*1023*1000 = 1125300     correct
1.1*1024*1000 = 1126400   incorrect

Not that it makes much of a difference.

Also you can use ADCW to get the upper and lower registers in one.

Code: [Select]
// Read the CPU supply Voltage
long readVcc()
{
   long result;

   // read 1.1V reference against AVcc
   ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
   delay(5); // wait for Vref to settle after the mux change
   ADCSRA |= _BV(ADSC); // convert
   while (bit_is_set(ADCSRA,ADSC));

   //result = ADCL;
   //result |= ADCH<<8;
   result = ADCW;

   result = 1125300L / result;  // AVcc in mV = 1.1*1023*1000 = 1125300

   return result;
}