Pages: [1] 2 3 ... 5   Go Down
Author Topic: ATTiny85v and Sleep Functions  (Read 5662 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will be programming an ATTiny85v using an Arduino as the programmer. My end project will be the ATTiny with 2xAA's (~3.2V), one analog temperature sensor, and one IR LED for output. The program will basically take 50 (or whatever) readings, throw out the lowest 10 and highest 10, then average the remaining readings to get a solid temperature reading. It will then blink the IR LED slowly for 2 seconds if the temperature is below a range, leave it off if within the acceptable range, and blink fast for 2 seconds if above that range. Then it will wait for 30 seconds and do it all again.

I have this running well on the Arduino itself, and will transfer it to the ATTiny85v.

But I need some help for figuring out how to maximize battery life. This will be a constant ON device (to monitor temperatures in babies' rooms and show me via the night vision cameras if it is too cold or hot). I found the code to use the power down sleep mode, and I found somebody who modified it to work on the ATTiny85, so I should be able to get that to work. I am wondering if I can use that to put it to sleep/wake up during the light blinking process. Basically, would there be any issues using the system_sleep() function in place of ALL delay() calls? The examples set the watchdog timer duration during the setup, so it would have to use the same sleep time each time it is called, but it would be nicer if a different time could be used (for example: half second in between LED blinks, and 8 seconds inside of a loop 5 times for a 30 second delay). Is that possible?

I was also wondering about any/all other tips for maximizing battery life. I see I can call power_adc_disable(), power_timer0_disable(), power_timer1_disable(), and power_usi_disable(). I can disable ADC during sleep and re-enable after. What about the timers and the USI? Can I disable the USI completely in the setup, or would that prevent me from flashing another program onto it??? I'm worried about that! And do I need timer0 and timer1? It is unclear to me exactly what those are needed for. If I disable them before a sleep call, would it turn off the watchdog timer and not wakeup?

So many questions, but I am learning quickly. Thank you all for your help!
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 150
Posts: 5665
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Basically, would there be any issues using the system_sleep() function in place of ALL delay() calls?

No.

The examples set the watchdog timer duration during the setup, so it would have to use the same sleep time each time it is called, but it would be nicer if a different time could be used (for example: half second in between LED blinks, and 8 seconds inside of a loop 5 times for a 30 second delay). Is that possible?

The watchdog time can be changed as often as you like.

I was also wondering about any/all other tips for maximizing battery life. I see I can call power_adc_disable(), power_timer0_disable(), power_timer1_disable(), and power_usi_disable(). I can disable ADC during sleep and re-enable after. What about the timers and the USI? Can I disable the USI completely in the setup, or would that prevent me from flashing another program onto it??? I'm worried about that! And do I need timer0 and timer1? It is unclear to me exactly what those are needed for. If I disable them before a sleep call, would it turn off the watchdog timer and not wakeup?

Timer0 generates the interrupt which increments millis(), you might need that. Timer1 normally isn't needed (it's used for PWM generation if you do analogWrite()).

Disabling USI won't affect programming (the first thing a programmer does is RESET the chip).

The power consumption of all those modules is listed in the datasheet (in "Power Management" and "Typical Characteristics" sections). The worst offenders are Timer1 and the ADC.

So many questions, but I am learning quickly. Thank you all for your help!

This is quite advanced stuff... smiley
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I suggest you program the fuses to use the 128kHz internal oscillator instead of the 8MHz one, and program the clock prescaler register to run the processor at the lowest clock frequency that you can manage with. This way, you can get the current consumption of the chip down to a fraction of a millamp while it is awake.

If you do this, then I doubt that it's worth having the device sleep while blinking the IR LED, since the ATtiny current consumption will be tiny compared to the IR LED.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Denmark
Offline Offline
Edison Member
*
Karma: 36
Posts: 1084
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Timer0 generates the interrupt which increments millis(),

Are you sure ?

From the core_build_options.h

Quote
/*
  For various reasons, Timer 1 is a better choice for the millis timer on the
  '85 processor.
*/
#define TIMER_TO_USE_FOR_MILLIS                   1

Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 150
Posts: 5665
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Timer0 generates the interrupt which increments millis(),

Are you sure ?

From the core_build_options.h

Quote
/*
  For various reasons, Timer 1 is a better choice for the millis timer on the
  '85 processor.
*/
#define TIMER_TO_USE_FOR_MILLIS                   1


You must have a different core than me. Mine uses timer0.

If you're worried about power, Timer1 uses 10 times as much power as Timer0. smiley

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There are a lot of things you can do to save power:

http://www.gammon.com.au/power


Using the watchdog timer to sleep is a reasonable way of allowing time to elapse. Clearly, very exact timing it not important.


If I may suggest, some other blink pattern to indicate the battery power is low? There are ways of self-detecting battery power. I'm not sure about the Attiny85, but a quick look at the datasheet would appear to indicate it does that too.

Quote
I am wondering if I can use that to put it to sleep/wake up during the light blinking process.

Yes you can, although as dc42 says, there may not be much point.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What about the timers and the USI?

If you use the watchdog, you won't care about the timers.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just out of curiosity (in case I want to make one of these) how are you planning to read the temperature accurately, in a way that is resistant to fluctuating supply (battery) voltage?
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Timer0 generates the interrupt which increments millis(), you might need that. Timer1 normally isn't needed (it's used for PWM generation if you do analogWrite()).


Wow! Great info from all! I am not using the millis() function, so I should be good there. I just wanted to make sure the watchdog timer will still work. I use the ADC to read the temperature sensor, and I use the INTERNAL 1.1v reference voltage for that reading as well. But I should be able to turn that all off during sleep and then back on without any serious issues. The datasheet mentions a brief wait time for the internal reference voltage to start back up, so I might add a tiny delay(10) before it begins taking readings. And right now, I have it taking 100 readings and throwing out the highest 20 and lowest 20 readings before averaging. By doing that and using the internal voltage reference,I get pretty steady temperature readings.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suggest you program the fuses to use the 128kHz internal oscillator instead of the 8MHz one, and program the clock prescaler register to run the processor at the lowest clock frequency that you can manage with. This way, you can get the current consumption of the chip down to a fraction of a millamp while it is awake.

If you do this, then I doubt that it's worth having the device sleep while blinking the IR LED, since the ATtiny current consumption will be tiny compared to the IR LED.

Nice. I have seen some about programming the fuses with avrdude commands. Can anyone provide the correct command to make these recommended settings changes?

How will running it at 128kHz affect my timing? For example, if in my test program I am using a delay(1000) on the Arduino itself to wait 1 second, what does that get translated to on the ATTiny85v running at 128kHz?

Will that change the watchdog timing as well? If I have the watchdog set to wait 8 seconds, will it still be 8 seconds at the slower frequency? Thanks!
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice. I have seen some about programming the fuses with avrdude commands. Can anyone provide the correct command to make these recommended settings changes?

See http://www.engbedded.com/fusecalc/ to get the correct fuse values and the avrdude docs for the corresponding commands.

How will running it at 128kHz affect my timing? For example, if in my test program I am using a delay(1000) on the Arduino itself to wait 1 second, what does that get translated to on the ATTiny85v running at 128kHz?

Depending on the core you are using, if you set F_CPU to 128000 in your board config when compiling, delay() may still give the correct timings.

Will that change the watchdog timing as well? If I have the watchdog set to wait 8 seconds, will it still be 8 seconds at the slower frequency? Thanks!

The watchdog timer is always clocked at 128kHz, so it won't be affected.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just out of curiosity (in case I want to make one of these) how are you planning to read the temperature accurately, in a way that is resistant to fluctuating supply (battery) voltage?

http://arduino.cc/en/Reference/AnalogReference

I use analogReference(INTERNAL); in the setup. The ATTiny85 also has a 1.1V internal reference voltage, so this should work there. Works very well on the Arduino! Before using this, I was getting lots of stray readings (even though I was running off a steady 5v USB supply), and as soon as I switched it, I am now getting very steady readings. I hope those steady readings will translate well to the use of batteries.

Which brings up another point. What do I need to do about low batteries? I saw something that said the ATTiny can be damaged if the batteries get low, and you can use the built-in BOD, but I never saw a good example on exactly how to implement it. DO I really need to worry about that?

If I get enough life out of the 2 AA's, I would be interested in moving to maybe 2 CR2032's in parallel. It would make a smaller package.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To use the BOD, all you need to do is program the fuses.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice. I have seen some about programming the fuses with avrdude commands. Can anyone provide the correct command to make these recommended settings changes?

See http://www.engbedded.com/fusecalc/ to get the correct fuse values and the avrdude docs for the corresponding commands.

I am on there now, and I see 3 options for 128kHz, and not really sure what they all mean. Which options should I pick???
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Page 29 of the ATtiny25/45/85 datasheet lists the recommended usage for each of the 3 options.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1] 2 3 ... 5   Go Up
Jump to: