ATtiny85 - Using capacitor for backup power to persist clock time to EEPROM

As discussed in the initial design post, the idea is to connect a 0.47F capacitor to the VCC and GND pins of the ATtiny85. Then when we lose power, the capacitor will provide the ATtiny85 with enough juice to store the current clock time to its EEPROM. When we gain power again, the ATtiny85 will read the clock time back from the EEPROM and start over. In this way, we avoid killing the EEPROM of the ATtiny85 with too many write operations.

To that end, I purchased something like this over EBay (2 for $3, so works out to about $1.50 each).

The code to check for supply voltage drop looks like this:

void loop() {
  // Execute loop() every second
  if (!timer1) return; else timer1 = false;

  // Measure supply voltage
  // Source:
  ADMUX = _BV(MUX3) | _BV(MUX2); // VCC as reference, band gap voltage (~1.1V) as input
  _delay_us(250); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Start conversion
  while (bit_is_set(ADCSRA, ADSC)); // measuring
  vcc = (int)(1100L*1023L/ADCW);

  // Start hibernation when VCC < 3V
  if (vcc < 3000) {
    if (!hibernate) {
      hibernate = true;
      EEPROM.write(0, clockHH);
      EEPROM.write(1, clockMM);
      EEPROM.write(2, clockSS);
  // Normal routine when VCC >= 3V
  else {
    hibernate = false;
    // TinyWire requirement
    // Clock tick-tock
  // Sleep now

Then in the startup() code, we read the clock time from EEPROM:

  // Read clock time from EEPROM if available
  byte hh =;
  byte mm =;
  byte ss =;
  if (hh >= 0 && hh <= 11 && mm >= 0 && mm <= 59 && ss >= 0 && ss <= 59) {
    clockHH = hh;
    clockMM = mm;
    clockSS = ss;    

This configuration has been tested to work quite smoothly.


Popular posts from this blog

Adding "Stereo Mixer" to Windows 7 with Conexant sound card

Roomba navigation algorithm

Hacking an analog clock to sync with NTP - Part 5