External 32 KHz timer2 in order to shutdown

Hi there all,
I'm a newbe and i finish to read some articles
about power saving of arduino.
i just want to ask you a little question.

I have a pulse counter that i want to make it to save more power possible.
This pulse sensor must read a LED pulse of my current power meter
and then send it via RF every 5 minutes to another arduino base.
Must work using 2 X 1.5 V Batts.

If i understand the most of CPU can be shut down in order to consume less power.
The problem is that i need an accurate timer in order to know
the time between the last 2 pulses every about 5 minutes.

So if I power down the arduino, i lost the mills().
But if i use an external timer2 using an external 32 KHz crystal
i can wakeup and have also the accurate timer2 mills... is this true ?
Is this correct that i understand please ?

Thanks a lot
Denis

I don't quite see how you would know when the LED blinks if it is asleep.

You might be best off trying to keep current low, but awake. For example, running off 3V would keep consumption down. Definitely use a "bare" board, not an Arduino development board (ie. no USB, voltage regulator etc.). Configure it to run at 1 MHz (run off the internal oscillator with "divide by 8" fuse set).

I am guessing you could get current down to around 1 mA at that rate. Using a 2 x AA battery with (say) a rating of 3000 mAH that would last you 3000 hours (125 days).

You could have the RF transmitter switched on by a MOSFET so every 5 minutes or so you power up the transmitter, send the readings, and power it down again.

The problem is that i need an accurate timer in order to know the time between the last 2 pulses every about 5 minutes.

If you have a way of knowing when the pulses come, even when it is asleep (interrupt?) then you could attach a clock chip (RTC) board with a battery backup. That would remember the time, even if the main processor is asleep.

But if i use an external timer2 using an external 32 KHz crystal i can wakeup and have also the accurate timer2 mills... is this true ?

Someone had a project about that recently here. He was making a clock or something.

denisj:
This pulse sensor must read a LED pulse of my current power meter
and then send it via RF every 5 minutes to another arduino base.

I know of one type of meter that sends a data packet every 1.0 seconds. If I only had to send data every 5 minutes then I would get a packet, send my data and sleep until just less than 5 minutes -- enough to let the board get up to speed to read the next packet.

Hi there and thanks all for the answer.

Now I don't remember where I read but I remember that tha cpu can sleep
but have also the external interrupt active.
Is this true ?

The LED pulse is read from a photo transistor
like here: http://forum.jeelabs.net/files/monitor_design_v01.png

It must wake up every 5 min in order to send the data via RF
but also must wake up every pulse that arrive via interrupt in order to count it.

Can I put it in sleep but external interrupt active please ?

Thanks again
Denis

denisj:
Now I don't remember where I read but I remember that tha cpu can sleep but have also the external interrupt active.
Is this true ?

Absolutely. But the only external interrupt that will wake it from deep sleeps is the LOW interrupt, which means the pin has to be brought low. If you can arrange that, fine.

Maybe the pin change interrupt would work for a transition but I haven't tested that. Actually page 70 of the datasheet seems to say that a pin change interrupt could be used to detect a rising or falling level:

Pin change interrupts on PCINT23..0 are detected asynchronously. This implies that these interrupts can be used for waking the part also from sleep modes other than Idle mode.

Dear Nick,
thanks again for the help

I don't know what type of interrupt I use ... i just copy paste the command from some source around
I know that I activate it using:

attachInterrupt(1, onPulse, RISING);

It is the LOW interrupt please ?
Thanks a lot
Denis

I would also suggest a bare bones chip + RTC. I have very strong doubts that it would be even remotely easy to beat a dedicated RTC with regard to both power consumption and accuracy.

Why not put it into sleep mode but leave counter/timer 1 counting clock cycles (and waking the processor up every 5 minutes), and counter/timer 2 counting input pulses from the phototransistor?

You can run the whole chip from a 32768Hz crystal, unless you need a faster clock speed when sending the data via RF.

Running @32kHz implies flashing @8kHz. This is a major pain. For low power I would run @1Mhz internal clock + use an RTC. Deep sleep till the RTC triggers the wakeup. Then perform whatever is necessary and go to sleep again. Unlike a crystal the RC oscillator will be ready much earlier. It also consumes less power.

Also keep in mind to shut down all parts that are not really needed (e.g. unneeded timers, brown out, I2C, ADC). The datasheet holds the details :wink:

Stuff about power saving:

This makes me think about the vast number of projects where AVR is a bit overkill.

Thanks a lot all of you guys ... it's wonderfool to see a lot of help.

I just ordered yesterday some 32,768 MHz crystals in order
to begin to test it.

So after i read (but not understand all) the wonderfool article post by Nick (http://www.gammon.com.au/power)
i understand this steps:

  • i must run it using the Timer2 trained by an 32MHz external quartz in order to have an accurate time after wake up
  • i must program the atmel in order to use 8MHz (i anyway need at last 4Mhz in order to use the RFM12B for rf transmitter)
  • for the LED pulse i will use the LOW interrupt wake-up on pin 5 of atmel
    (i hope will fill the LOW status, cause now in my program i use the RISING status)

Is everything right until now ?
The Timer2 is work in "Power Down" mode ? ..or it's work only in "Power Save" mode ?

I just must understand about how to program the atmel in this case.
If I understand I can not use the normal flash using the bootloader
cause of use of internal 8MHz RC clock... it is there a bootloader that can use 8MHz internal clock ?

Thanks a lot again
Denis

cause of use of internal 8MHz RC clock... it is there a bootloader that can use 8MHz internal clock ?

Yes there are 8 MHz bootloaders. The Lilypad is one I think.

There's Arduino as ISP to minimal config breadboard 8 Mz.

AFAICT this warning applies to UNO's with 168 chips, not 328's, see the chip-specific part in parenthesis. I can tell as far as my 328-chip UNO bootloading and programming 328P-PU's
using material and wiring from that link.

Note that these techniques only work with the Arduino Duemilanove w/ an ATmega328, not the Arduino Uno (or older Arduino boards w/ an ATmega168).

Hard to predict change, that comment must be ooooooooooooooold!

This project got me interested in what sort of low power consumption might be achievable. I made up a test case with a LLS05-A linear light sensor.

The wiring for that is pretty simple:

// Count light pulses on a light sensor.
// Author: Nick Gammon
// Date: 18 November 2012

#include <avr/sleep.h>

const byte LED = 13;
const byte photoTransistor = 8;

// count of pulses
volatile unsigned long count;

/*

Pin change interrupts.

Pin                  Mask / Flag / Enable

D0	  PCINT16 (PCMSK2 / PCIF2 / PCIE2)
D1	  PCINT17 (PCMSK2 / PCIF2 / PCIE2)
D2	  PCINT18 (PCMSK2 / PCIF2 / PCIE2)
D3	  PCINT19 (PCMSK2 / PCIF2 / PCIE2)
D4	  PCINT20 (PCMSK2 / PCIF2 / PCIE2)
D5	  PCINT21 (PCMSK2 / PCIF2 / PCIE2)
D6	  PCINT22 (PCMSK2 / PCIF2 / PCIE2)
D7	  PCINT23 (PCMSK2 / PCIF2 / PCIE2)
D8	  PCINT0 (PCMSK0 / PCIF0 / PCIE0)
D9	  PCINT1 (PCMSK0 / PCIF0 / PCIE0)
D10	  PCINT2 (PCMSK0 / PCIF0 / PCIE0)
D11	  PCINT3 (PCMSK0 / PCIF0 / PCIE0)
D12	  PCINT4 (PCMSK0 / PCIF0 / PCIE0)
D13	  PCINT5 (PCMSK0 / PCIF0 / PCIE0)
A0	  PCINT8 (PCMSK1 / PCIF1 / PCIE1)
A1	  PCINT9 (PCMSK1 / PCIF1 / PCIE1)
A2	  PCINT10 (PCMSK1 / PCIF1 / PCIE1)
A3	  PCINT11 (PCMSK1 / PCIF1 / PCIE1)
A4	  PCINT12 (PCMSK1 / PCIF1 / PCIE1)
A5	  PCINT13 (PCMSK1 / PCIF1 / PCIE1)

*/

ISR (PCINT0_vect)
  {
  byte sensor = digitalRead (photoTransistor);
  digitalWrite (LED, sensor);  
  if (sensor == HIGH)
    count++;
  PCICR  &= ~_BV (PCIE0);   // disable pin change interrupts
  }  // end of lightOn


void setup ()
{
  pinMode (LED, OUTPUT);
  
  // pin change interrupt masks (see above list)
  PCMSK0 |= _BV (PCINT0);   // pin 8

}  // end of setup

void goToSleep ()
  {
  PCIFR  |= _BV (PCIF0);   // clear any outstanding interrupts
  PCICR  |= _BV (PCIE0);   // enable pin change interrupts
  
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  sleep_enable();

  byte old_ADCSRA = ADCSRA;
  // disable ADC to save power
  ADCSRA = 0;  
  // turn off various modules
  PRR = 0xFF;  
   
  // turn off brown-out enable in software
  MCUCR = _BV (BODS) | _BV (BODSE);
  MCUCR = _BV (BODS); 
  sleep_cpu ();  
 
  // cancel sleep as a precaution
  sleep_disable();
  PCICR = 0;  // cancel pin change interrupts
  PRR = 0;    // enable modules again
  ADCSRA = old_ADCSRA; // re-enable ADC conversion
    
  }  // end of goToSleep
  
void loop ()
  {
  goToSleep ();  
  
  // do something with results here
  }

I put an LED on pin 13 to see if it was working. The code counts when the light comes on (ie. the LED on the power meter flashes). You could add code to check the time (from a RTC chip) and periodically upload the results to somewhere else (after powering up the transmitter).

I measured it taking about 2 uA when asleep, which isn't too bad. Note that the darker it is, the less current drawn by the phototransistor, and thus the less power consumption. So the ideal thing would be for the sensor to be in a meter box with the door shut (or a black cloth over it or something), to keep the light out.

This was tested on a "bare bones" board. No voltage regulator, USB interface or anything like that.

See my page here for tips about turning on external things like clocks, transmitters: http://www.gammon.com.au/power

The simple thing, I think, would be to check the time when it wakes (where I have the comment "do something with results here"). Or maybe even every 5 flashes to keep it even lower consumption. After all, if the meter isn't flashing, you hardly need to upload new results.

Of course, the LED in my test uses power also. I would disconnect that when you are comfortable it is working. When testing, the LED should flash in time with the meter flash.

I tested it at 3V, it worked OK, except I needed to set the "divide clock by 8" fuse bit. The processor isn't rated to run at 16 MHz at 3V. An alternative would be to run from the internal oscillator at 8 MHz (that's OK at 3V) thus saving the need for a crystal or resonator.

2 uA is pretty good, because a AA battery will self-discharge at a somewhat faster rate than that anyway, so the battery is the limiting factor, not the power consumption of the chip.

Dear Nick,
thanks a lot for your big help
I was out of home all weekend, and i can't replay.

My simply opinion is that 2 uA it's very good.
So for the timer... you think that is better if i put there an external RTC chip ?
...or is better using external 32Mhz crystal for timer2, and internal oscillator at 8MHz ? in this case i must flash it different right ?

I'm looking for some LLS05-A linear light sensor... but i already have some photo-resistance maybe i'll test using that.
Again thanks to all
Denis

denisj:
So for the timer... you think that is better if i put there an external RTC chip ?

Yes, I don't see how the other options will work. If the processor is asleep, it won't be counting time. If it isn't asleep it isn't saving power. The $10 RTC chip with its battery backup will know the time, even if the processor spends most of its time sleeping.

Could the IR pulse or data from the meter serve in a time-keeping capacity? Does the meter data include time? Even if it doesn't, the Elster meters I'm somewhat familiar with emit data every 1.0 seconds to some degree of accuracy so count the packets and you have time.

Nik,
thanks again for your answer.
Like you can understand, I'm trying to keep the hardware as simple as possible.

I will not need a like a calendar time ... but just to have the millis between last 2 pulses.
So i search some RTC that have a millis also... but i don't find for now.
For example i found DS3232 from Maxim and on the key fastures i read:
Real-Time Clock Counts Seconds, Minutes, Hours, Day, Date, Month, and Year with Leap Year Compensation Valid Up to 2099

It is there something that give me the mills ? i don't need other ... as little is possible :slight_smile:

Can i use the RTC with the current from the principal batteries ? so with out the battery backup.
After the reset I'll set to 0000 the time and then will count only the mills between last 2 pulses.
Then every 5 minutes wakeup, send the watt/hour via RF and i'll turn the RTC to 0000, then back to shutdown.
I think in this mode will consume less power.

GoForSmoke@ ... the current meters in Italy have only the LED that emit a pulse every watt, there is not the time :frowning:

Thanks again all
Denis