I am making a project with an ATTINY85, and I want it to either turn off, or go into deep sleep if it hasn't been used for some time. Can I deep sleep an ATTINY85, or should I have an automatic power off circuit?
Did you try Google...??
Hmmm...
I shall rephrase my question...
Do you think it is better to deep sleep my ATTINY85, or have an automatic off circuit? It may be unused for up to a year, then be used again, then stored for a long time. My "power button" is a momentary push button. It can either be used to wake up the ATTINY85, or as a power switch.
It will also have an OLED screen attached to it.
That depends on how low of a power draw you need. You can get an Attiny85 pretty low in sleep, but if you are running off a battery you will probably want to turn it off. The screen will obviously draw a fair bit of power too.
Yes, I will be running it off a single 18650 (3500mAh) battery.
I'd suggest a protected cell or circuitry to prevent over discharge. I damaged some brand new 18650s on a recent project due to not doing that.
Yes, I am getting a protected cell. I don't trust unprotected cells.
I don't know if this will help, but I accidentally left my Attiny85 and OLED I2C screen on when I put it away one time and it was still on, but dim, a bit over 24 hours later on a single CR2032. You may be able to estimate you power needs based on that.
I have an ATtiny85 project (NRFLite/Sensor_TX_ATtiny85_2Pin.ino at master · dparson55/NRFLite · GitHub) where I deep sleep the mcu but keep the watchdog timer running to allow it to wake itself up on its own, and it draws 5 microamps in this low power state. If you turned off the watchdog and just woke it with an external interrupt, it would be even lower. With a 3400mAh battery it could theoretically run for multiple years with the watchdog running. As for the OLED, the modules I've tried have a significant power draw in their lowest power state, usually in the milliamp range, so for my battery powered projects I always end up dedicating 1 pin of the mcu to control a switchable power bus for external devices. On the ATtiny85 you can theorectically power up to 40 milliamps on such a pin directly, or have it control a transistor or mosfet that supports higher power draws. Good luck!
I'm going to do this. Can I use a mosfet?
Yes a transistor or mosfet can both work fine. I have specifically used PN2222 npn transistors (OutletTimer 2 - using a mechanical relay - YouTube) and also the IRLML6402 p-channel mosfet (Moms Timer 08 - main circuit complete and learning to use a p-channel mosfet - YouTube) for projects.
This is my proposed circuit, driven by the ATTINY pin
As shown in my p-channel mosfet video I linked, you just need to make sure the mosfet you choose supports the gate threshold voltage that you'll be applying. In your diagram you're connecting the gate directly to a pin so when the pin is LOW, the gate-source = -5V (0V on gate minus 5V on source = -5V). So check the datasheet of your mosfet to see if -5V is within the acceptable range for the gate threshold voltage.
As shown in my video, the mosfet I was using needed between -0.40 and -1.2V and my source voltage was 3V, so I needed to use a voltage divider to get the right voltage.
One other suggestion, you should add a 10K pull up resistor between the p-channel mosfet's gate and source to ensure it remains off when not driven low by the ATtiny's pin. See the explanation of the "pull-down (or up) resistor" on https://www.baldengineer.com/7-mosfet-myths-and-misconceptions-addressed.html for more detail. When you put the ATtiny to sleep, you'll want to make all the pins inputs to keep the power draw low (see the code I previously linked), so the mosfet's gate will be floating without that pull-up resistor.
I think it is 3v? But it doesn't say -3v, so I'm not sure...
Oh this is an N-channel mosfet which is used to turn on and off the GND side of a circuit, not the VCC or 5V side of the circuit. So in your diagram you'd connect the VCC of the OLED to 5V and instead of connecting the GND pin of the OLED to GND, you'd connect it to the drain of this n-channel mosfet. Then on the gate, you'll need to make a voltage divider with resistors to create the the necessary/typical Vgs of about 2.1V (n-channel mosfets have a positive Vgs voltage). I'd start by using two 10K resistors to divide the 5V in half which will give you 2.5V, and that's close to the typical voltage they are recommending. And then for the final pin of the mosfet you'd connect the source to GND. So that will allow the ATtiny to turn on and off the GND side of the OLED.
So more like this?
If you have a push button to wake things up, you might consider just switching the entire power supply when you need it. Then you don't have to worry about sleeping, or partial shutdowns of this component or that. Here's a low side circuit which might work. On powerup, your code would bring the GPIO low to keep the power on after the push button is released. Then when the task is done, the GPIO would be brought high, or switched to input mode, and the power would switch off. At least I think it would switch off. You would need to test it.
[a revised version is in post #20.]
The 2 resistors and connection to the gate are ok, but instead of connecting the mcu pin between the resistors, instead connect it to the other side of the upper most resistor to allow it to supply either 5V or 0V to the resistor divider circuit. The other side of that resistor will then either be at 2.5V or 0V depending on the state of the pin, and that will be a good starting point to see if it controls the mosfet ok.
I got to thinking that my power circuit may not turn off after all. So for future reference, I like this version better:
When it is actually in use, does it consume much power? If not, then you could be better off using a non-rechargable lithium battery, both from a cost and longevity standpoint.