attiny85, when to sleep or not to sleep

Hi,

In a situation where my program is looping doing nothing at all just waiting for a button to be pressed is better to do:

while( digitalRead(BUTT)!=0); // loop here until button pressed

and keep the program as simple as possible or should I take the trouble to put the attiny85 to sleep and wake it up with the same button I'm reading.

I have already made a few not totally unsuccessful tries but I'm not sure where the problem is, right now my BUTT :slight_smile: is PB1 and is configured with a pinMode(BUTT, INPUT_PULLUP); maybe PB1 is not a good pin for a wake up ? I have all the pins used with but I can change the button to other pin, but so far I was not able to understand if this is an issue or not.

In this case power is not a (big) issue, as the circuit is being powered from a 220V->5V power supply but it's messing with my head wasting it doing nothing.

Just for context, I'm doing a small attiny85 based project that can actually save me some money (that's a 1st), so far I have forget to turn off my soldering station a few times, one of that times it stayed on the full week end, which is not good to the iron tip nor to your wallet.
So I'm doing a power outlet that provides 220V power for 1 hour and then turns off until you press a button, this is done with a solid state relay OMROM G3MB-202P. The attiny fires the relay via a NPN transistor, counts 3600 seconds and turns it off, and then waits for the press of a button forever.

One thing that I'm not sure. When sleeping what happens to the output pins ? They all go to DOWN state or they can float ? I'm worried that the pin that keeps the relay off could float to a ON state. This is something that I thought on this moment... Maybe going to sleep here is not a good idea after all.

When the part is put to sleep, pins are left in their previous state. Outputs remain outputs set to whatever they were last set to and so on.

IMHO it is worth to go to sleep (you will learn how to do this which is valuable). Any pin may be used for wake up.

should I take the trouble to put the attiny85 to sleep and wake it up with the same button I'm reading.

Yes, if you are running on batteries. Otherwise, don't bother.

I’ve actually been thinking about doing something similar. Of course you could do a minimal implementation with only a 555 timer, however I thought that even with a MCU, single button, a led, buzzer and relay module, it would be possible to make the time pre-settable. This is how I would see it:

  1. If the button is held in at the time the device is powered up (plus a few seconds), it enters time setting mode, and indicates this by blinking every say four seconds to indicate an increasing count of “on” intervals of say 5 minutes each. As soon as the button is released, the counting stops and the number of time intervals thus counted is stored in the eeprom.
  2. At power on, the device blinks N times to indicate for how many time intervals it is preset.
  3. During use, the buzzer bleeps when say time expiry is in say one minute.
  4. A press of the button at any time during operation refreshes the expiry time to the number of preset time intervals.
  5. At expiry, the buzzer bleeps twice and all is shut off.

I agree about sleep mode not being appropriate for this, especially if you need to use millis() for the timing since this restricts the sleep modes. Further, you have found it necessary to switch the relay led with a transistor which indicates you are expecting 10 or more milliamps anyway during on time.

Incidentally, one of my father’s colleagues set fire to his house because of an a accident with a forgotten soldering iron.

You guess almost everything I'm doing here.

  1. One pin with a red led that lights when the power is off.
  2. One pin with an active piezo buzzer, it beeps 3 times when the relay turns off.
  3. One pin have a switch to VCC or GND, this is only read on setup(), if it's HIGH then the relay turns ON when it boots, it stay OFF otherwise.
  4. The last pin with a button (debounced with 2 resistors and a cap).

If the relay is OFF pressing the button turns it ON and start the 1 hour timer, if it's ON it turns it off immediately. At 1st I made it reset the timer but decided that it was better to have an emergency stop.

To enter setup mode, instead of doing complex code to detect a double or triple or long click I would just read the button on setup(), if pressed during boot enter setup mode signal it with flashes of the red led.

Sound like a useful product and one for the Exhibition Gallery corner of the forum.

DrAzzy:
When the part is put to sleep, pins are left in their previous state. Outputs remain outputs set to whatever they were last set to and so on.

Ah... Somehow I thought that all the pins would be clamped down to GND or left floating.

And from the datasheet...
"When entering a sleep mode, all port pins should be configured to use minimum power. The most important thingis then to ensure that no pins drive resistive loads. In sleep modes where both the I/O clock (clkI/O) and the ADCclock (clkADC) are stopped, the input buffers of the device will be disabled.(...)"
And at this time my program will be powering a LED, so, it makes no sense at all going to sleep here.

6v6gt:
Sound like a useful product and one for the Exhibition Gallery corner of the forum.

Thank you...
I had it almost ready to implementation... :slight_smile: :slight_smile: :slight_smile:
Now I'm in a can of worms trying to learn how to use the EEPROM.h lib... :slight_smile:

The EEPROM library documentation is here: Arduino - EEPROM

Usually the best is to define a struct which contains all the variables you want in the eeprom.
Use EEPROM.get() to load the struct from the eeprom (into RAM), change the values of any variables as required, then use EEPROM.put() to resave it all into the eeprom.

As all I need is 1 byte I decided to use the EEPROM array, I gave it a try last night and it did not work, probably I'm doing something stupid.
As I don't want to use the same address I come up with the idea of writing allays 2 values.
My config byte can be 1, 2, 3, 4 or 5, always write a 0 on the next address.

if EEPROM[0]==0 then the EEPROM is not configured, use defaults
otherwise, look for the first zero, current configured value will be in the previous byte.
If writing, write new value to that byte (if different from previous) and a zero to the next, so each time I save a config it will go to the next byte.
When it reaches EEPROM.length() it is full and I erase it writing 0 to each address...
Not very complex but maybe 02:00 in the morning after a work day is not the best time to write that kind of code.

So I have to setup a software serial and a ttl->usb in order to do debug, that's the problem of developing on the 85, or maybe I test this on a Nano, the only difference should be EEPROM.length(), right ?

I think you are taking too much trouble to avoid eeprom "wear".

An occasional write to the eeprom, triggered by a manual operation, is not problem. It is only when you start writing to the eeprom from the loop() that you will eventually destroy it.

If your are writing bytes, EEPROM.write() and EEPROM.read() are alternatives.

Incidentally, an unwritten EEPROM cell has a value 0xFF not 0x00 as your test implies.

6v6gt:
Incidentally, an unwritten EEPROM cell has a value 0xFF not 0x00 as your test implies.

ah! that explains it...
I erased it with the Arduino IDE, so I was expecting it to be all zeros, will have to reverse my logic then.
Thanks.

Learned something new today.

I wanted to dump the EEPROM.
Instead of setting up a software serial interface I knew that it could be done with avrdude.
So, I connected the aatiny85 to a raspberry pi with an USBASP and extracted it with

root@pi3:~# avrdude  -p t85  -c usbasp  -b 19200 -e -U eeprom:r:eeprom_dump.bin

Examine the file with od -xv eeprom_dump.bin or an equivalente tool.

What I didn't knew and I have never read about is that the sketch that we load into the UNO or the Nano to program the attiny85 can be used to other things.
Just look at the console window on Arduino IDE, copy the line used to call avrdude to send your sketch to the attiny85, and just change it to run the -U function.

C:
CD \TEMP
C:\Users\XXXXXX\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude -CC:\Users\XXXXXX\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf -v -V -pattiny85 -cstk500v1 -PCOM19 -b19200  -U eeprom:r:eeprom_dump.bin

of course, that windows beeing windows you'll have to download your hex viewer program, I'm using something called HxD that looks to work ok.