ATTINY85 power consumption in deepsleep

Hi there,

This circuit will be placed on a door with a magnetic reed switch to detect door change states.
Meaning, that if the door either opens or closes, the LED will blink and the circuit will go to deep sleep.
Note: The circuit design nor the code logic does not check whether the door is open or closed.
My goal is to run this circuit with a single Li battery.

So far this design with the code, is working to my expectations.

Reed switch will be a normal open.

#include <avr/sleep.h>
#include <avr/interrupt.h>

const int switchPin = 3;
const int statusLED = 2;

void setup() {
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);
  pinMode(statusLED, OUTPUT);

  // Flash quick sequence so we know setup has started
  for (int k = 0; k < 10; k = k + 1) {
    if (k % 2 == 0) {
      digitalWrite(statusLED, HIGH);
    }
    else {
      digitalWrite(statusLED, LOW);
    }

    delay(250);
  }
}

void sleep() {
  GIMSK |= _BV(PCIE);                     // Enable Pin Change Interrupts
  PCMSK |= _BV(PCINT3);                   // Use PB3 as interrupt pin
  ADCSRA &= ~_BV(ADEN);                   // ADC off
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // replaces above statement

  sleep_enable();                         // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
  sei();                                  // Enable interrupts
  sleep_cpu();                            // sleep

  cli();                                  // Disable interrupts
  PCMSK &= ~_BV(PCINT3);                  // Turn off PB3 as interrupt pin
  sleep_disable();                        // Clear SE bit
  ADCSRA |= _BV(ADEN);                    // ADC on

  sei();                                  // Enable interrupts
}

ISR(PCINT0_vect) {
    // This is called when the interrupt occurs, but I don't need to do anything in it
}

void loop() {
  sleep();
  digitalWrite(statusLED, HIGH);
  delay(1000);
  digitalWrite(statusLED, LOW);
}

However, I'm seeing excessive power consumption during deep sleep.

When the reed switch is closed (connected to the ground potential), the chip wakes up blinks the LED and goes back to deep sleep mode. Power consumption shows now as 15mA.

Next, when the reed switch is open (floating), the chip wakes up blinks the LED and goes back to deep sleep mode. Now power consumption is in sub-microamps. which great.

How come there are differences in sleep mode power consumption depending on the reed switch state, mainly when it is closed?

thanks.

Your diagram and words suggest an N/O reed switch, but your description of operation suggest an N/C switch.

Don’t forget a current limiting resistor on the LED

As you have connected the reed switch, use pinMode(switchPin, INPUT_PULLUP);

When it is closed it is drawing current through the internal pullup resistor.
However 15mA seem very high are you sure it is actually asleep?

Also you need a curent limiting resistor on the LED. Hope you have not damaged the 85.

Did you see the:

That activates the pullup resistor.

1 Like

You are right. When I wrote that, I didn't consider that calling the two instructions would have the same effect as if I wrote registers directly. I only paid attention to the INPUT parameter.

well if it is not sleeping, then the loop() should work right? and the LED should be blinking, right?

oh sorry, I missed to include it in the schemetics. on the breadboard i have used a resistor.

Seems more like caffeine kicked in

With the switch closed it will draw more than if it is open but should only be at most 165uA.
Can't explain why it's 15mA unless your meter is wrong or the 85 is damaged.

1 Like

Damaged or .. fake one.

might be as it was purchased like 5 years ago from an ebay seller.

let me test it with some more samples.

Even if it was fake, how could it draw that much current if with open switch you have sub uA? Internal pullup 220ohm? :grinning:

A simple test would be to not enable the pullup and test the current draw with the switch open and closed. They should be very close and small.

1 Like

You mean there's certain areas not even fakes go into? :upside_down_face:

Sorry for been silent such along time as been not well.
The meter was faulty.
When the switch is closed, as you've suggested it draws about 0.131mA.
And when open Zero.

May I ask, is there a way that I could have this working at 2 or 3 uA level?
Maybe using some other technique?
Anyone have an idea?
Because this project i've been following for sometime now, has been able to achive that.

Since you now appear to have a working meter you can experiment with using a weak external pullup resistor instead of the internal pullup resistor.

If you don't have to register the change in state of the gate immediately, you could wake the microcontroller every say 8 seconds, switch on the pullup resistor and test the state of the switch, act as necessary, then switch off the pullup and sleep again.

I stopped that video after a few seconds. Can you find a schematic and code for the ESP solution which does not involve sitting through a special effect loaded video ?

sorry, there is none for the latest version.
Apparently he is selling this product in Tindy.
However, there is a schemetic for his previouse version of the board, but it only detects a single state change (either open or close).

Thanks I will test your suggestion.

You'll need the watchdog timer for this.

That project has, in the mean time, been migrated to an ESP32.

Anyway, he appears to achieve very low power not by an inbuilt sleep mode of the ESP but by externally detecting an event using low power discrete components, then powering up the ESP for the duration of the event processing, data transmission etc. then shutting down the ESP.

The (old) circuit looks very elaborate for that task but I may have missed some subtleties by a quick scan.

1 Like