Go Down

Topic: External 32 KHz timer2 in order to shutdown (Read 2075 times) previous topic - next topic

Nick Gammon

#15
Nov 18, 2012, 05:31 am Last Edit: Nov 18, 2012, 06:14 am by Nick Gammon Reason: 1
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:




Code: [Select]

// 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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

denisj

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


Nick Gammon


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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

GoForSmoke

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.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

denisj

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 :-)

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 :-(

Thanks again all
Denis

GoForSmoke

Is there nowhere near an electric meter that you can plug a USB charger in?

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Nick Gammon


I will not need a like a calendar time ... but just to have the millis between last 2 pulses.


Why? Don't you just want to count the number of pulses over 5 minutes? If you want to know the number of watts, who cares when the pulse occurred, to the nearest millisecond?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

dhenry

Quote
but just to have the millis between last 2 pulses.


Generally, if your duration between tasks is that short, you don't put your mcu into sleep: the restart of a crystal oscillator can be several ms on most mcus.

If that's what you truly want to do, look into msp430: their crystal oscillator is "instant on". Energy micro is a distant 2nd and other mcu oems are not even in the same universe with TI on that.

denisj


Is there nowhere near an electric meter that you can plug a USB charger in?

The problem is not the power, i just want to make it run from 2 X 1.5 Batts cause i'm sure it can run some mounts if i make it right ;-)


Why? Don't you just want to count the number of pulses over 5 minutes? If you want to know the number of watts, who cares when the pulse occurred, to the nearest millisecond?

Nick, i will need to send 2 types of data on the net ...
1. the actual power that the PV system generate, for this i'll need the time between last 2 pulses every 5 minutes
2. the total generated power, and here i'll count all pulses of that day.


If that's what you truly want to do, look into msp430: their crystal oscillator is "instant on". on that.

Dhenry ... I allready have difficult to understand this world cause I'm newbe, thansk a lot but i don't want to go versus new mcu's

I want to ask something also.
In the link of Nick page http://www.gammon.com.au/forum/?id=11497 i so the difference from
SLEEP_MODE_PWR_SAVE: 1.62 mA - this I can use if I'll use the external Timer2 using the 32KHz cristal
SLEEP_MODE_PWR_DOWN : 0.36 mA - for this mode I'll use the external RTC chip.
...so the diff is 1.26 mA
My question is... how much ampere an external RTC can use ? ...less than 1.26 I hope right ?

Thanks again all
Denis

dhenry

Quote
how much ampere an external RTC can use ?


Look into the device datasheet for that. Can be less than 1ua, depending on the rtc and mode you are  running it in.

denisj


Look into the device datasheet for that. Can be less than 1ua, depending on the rtc and mode you are  running it in.


Whoau ... great thinks
So now I only most fount an RTC that give me the millis
can anybody target me to some of this RTC please ?
Thanks again
Denis

dhenry

Quote
So now I only most fount an RTC that give me the millis


You never will, because a beast like that does not make sense, as has been explained to you earlier.

denisj


You never will, because a beast like that does not make sense, as has been explained to you earlier.


Sorry dhenry ... where exacly was has been explained ?

Go Up