Go Down

Topic: ATTiny85v and Sleep Functions (Read 5 times) previous topic - next topic

Coding Badly

I'm also wondering why this does not work at 1MHz.

I can think of two possibilities.  From what I know of the folks who built the OneWire library, the first is much more likely to be the culprit and the one you should pursue first...

1. The oscillator needs to be tuned.  From the factory, the oscillator is ±10%.

2. There is a bug in the OneWire library.


Is there an easy way to tune the oscillator? I tried looking it up, and I don't see anything easy to use. If I just run it at 8MHz, will I still run into issues at all? Will it use much more battery life this way? Or will it be pretty negligible?

Nick Gammon

Have a read of how One-Wire works:


It's moderately timing dependent, eg.


To send a "1", the bus master software sends a very brief (1-15 µs) low pulse. To send a "0", the software sends a 60 µs low pulse. The falling (negative) edge of the pulse is used to start a monostable multivibrator in the slave device. The multivibrator in the slave clocks to read the data line about 30 µs after the falling edge. The slave's multivibrator unavoidably has analog tolerances that affect its timing accuracy, which is why the output pulses have to be 60 µs long, and the starting pulse can't be longer than 15 µs.

Running at 1 MHz when the library is written for 8 or 16 MHz is going to introduce an error of 8 or 16 times.

Looking at the library, it looks like it uses delayMicroseconds. That would probably be OK if the sketch was compiled for the correct clock speed, and you didn't just compile under 8 MHz and run under 1 MHz.

Even if it was, lines like this probably won't work too well:

Code: [Select]

        DIRECT_MODE_OUTPUT(reg, mask);
DIRECT_WRITE_LOW(reg, mask);
DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise
r = DIRECT_READ(reg, mask);

Even if delayMicroseconds adjusts to the slower clock speed, the different between 3 and 9, after dividing by 16, is not going to be much.

Coding Badly

Is there an easy way to tune the oscillator?

Yes, but...

If I just run it at 8MHz, will I still run into issues at all?

I doubt it.

Will it use much more battery life this way?

What is the sleep / awake ratio?  If it's very large then the extra energy burned from the higher clock speed is insignificant.

If you decide you can't live without 1 MHz, let me know and I'll get you instructions for tuning.


Great info. I will just run it at 8MHz. Seems to run nicely that way. I did find the TinyTuner, so I can always do that. Looks like you use it to find the offset/correction amount needed to make it accurate, and then add some code to anything running on that MCU to adjust. That might help with some issues later down the road, too. And I think I would like to add it to my best practices for the future anyway.

To answer the previous question, the MCU sleeps (power down mode) for ~32seconds, then checks the temperature reading from the DS18B20. If the temperature is too high or too low, it will blink for ~2seconds, but it also sleeps during each section (HIGH, sleep, LOW, sleep, HIGH, sleep...). So it should normally be in power down sleep mode most of the time.

One other thing I might want to add is a photocell. I could code it so that it will not take temperature readings and blink out warnings unless it is dark. So the MCU would basically sleep for ~32seconds, and if it is light out, then continue sleeping. That should keep everything running at 5.5uA, and then I would not lose any power to taking temperature readings and blinking the IR LED during the day.

Nick Gammon

Just FYI my test of a similar thing is still running from 3 x AA NiMh batteries, only dropping about 1 mV of battery voltage per day (3.9589V right now). So it's got a while to go before it hits the "knee" of the voltage curve (around 3.6V for 3 batteries). And that's with the warning LED flashing half the day, because it is getting colder at night.

Nick Gammon

Sep 01, 2013, 08:09 am Last Edit: Sep 01, 2013, 10:54 am by Nick Gammon Reason: 1
It's September and the voltage of those same batteries is now 3.83V. That's still way above the nominal voltage of 3.6 (for three cells) and that is five months later!

Go Up