Servo power down / current drain / watchdog timer attiny85

righto, cheers Nick.

Well I've made a few changes (so I could understand it basically) and tested with my Attiny85 I happened to have lying around. :slight_smile:

#include <avr/sleep.h>
#include <avr/wdt.h>
#include "Servo8Bit.h"

Servo8Bit myServo;  //create a servo object.
int pinLed = 0;

void setup()
  {
  pinMode(pinLed,OUTPUT);
  }  // end of setup

void loop()
  {
  //reattach the servo that was previously detached
  myServo.attach(1,544,2200); //attach the servo to pin PB1 

  digitalWrite(pinLed,HIGH);  // let led blink
  for(int pos = 0; pos < 180; pos++)  // goes from 0 degrees to 180 degrees
    {                                   // in steps of 1 degree
    myServo.write(pos);             // tell servo to go to position in variable 'pos'
    delay(15);                      // waits 15ms for the servo to reach the position
    }

  for(int pos = 180; pos > 1; pos--)  // goes from 180 degrees to 0 degrees
    {
    myServo.write(pos);             // tell servo to go to position in variable 'pos'
    delay(15);                      // waits 15ms for the servo to reach the position
    }
  digitalWrite(pinLed,LOW);

  myServo.detach ();

  //save the value of the Timer Counter Control Register 1
  byte saved_TCCR1 = TCCR1;
  //turn off timer 1
  TCCR1 = 0; //turn off timer 1
 
  pinMode(pinLed,INPUT); // set all used port to intput to save power
 
  system_sleep();
 
  pinMode(pinLed,OUTPUT); // set all ports into state before sleep
  //Restore the TCCR1 register to re-enable the timer.
  TCCR1 = saved_TCCR1;
}  // end of loop

// set system into the sleep state 
// system wakes up when wtchdog is timed out
void system_sleep() 
  {
  // disable ADC
  byte saved_ADCSRA = ADCSRA;  
  ADCSRA = 0;

  // clear various "reset" flags
  MCUSR = 0;     
  // allow changes, disable reset
  WDTCR = _BV (WDCE) | _BV (WDE);
  // set interrupt mode and an interval 
  WDTCR = _BV (WDIE) | _BV (WDP3) | _BV (WDP0);    // set WDIE, and 8 seconds delay
  wdt_reset();  // pat the dog
  
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  

  sleep_enable();
  sleep_cpu ();  
  sleep_disable();

  ADCSRA = saved_ADCSRA; 
  }  // end of system_sleep

// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) 
  {
   wdt_disable();  // disable watchdog
  }   // end of WDT_vect

I didn't quite get what the flag was for the watchdog, so that went out the window. And a few other things I didn't get either.

I'm not quite sure about the servo output. I'm seeing about 25 pulses of about 1.54 mS high each followed by 18.76 mS low, per batch.

With the LED disconnected the circuit draws about 8.8 mA when active, and about 675 uA when sleeping. Is that roughly what you are expecting? With the LED on pin 5 it draws 16.5 mA (that would vary depending on the resistor on the LED).

(edit) See below for corrected figures.

Hmm... I can't believe you could take half a look at that mess and create something tangible.

Thanks for your help, I'm looking forward to sitting down and trying to figure out what's going on and where I was going wrong.

I hope this little exercise will help someone else.

Hat's off to you mate.

I made a major cock-up of the measurements. I had left in the circuit on the breadboard an IR sensor from a previous experiment that was drawing current.

The revised figures are:

  • Sleeping: 8 uA
  • Awake (with no LED): 8.1 mA

Just a quick update...

I had Nick's code running fine yesterday, however today when I tried it, it was a no go again. Tried on a couple of different chips and rewired a dozen times so who knows what's going on there.

Went back to the code I'd posted above. The trick is not so much in the detachment of the servo after all that, it is to put a protection diode on the pin controlling the servo .

Testing on my desk at the moment and working fine. Figured this out as there was no current when the red to the servo was detached, but remained current draw when the brown was.

Trick for young players I guess.

Yep the old protection diode. An oldie but a goodie.

Hmm....

The plot thickens...

The unit is kind of functioning, however when running through the MOSFET, it crashes after a few cycles (not many, maybe 5 - 20).

Because I'm running with the LED of the gate pin for the MOSFET, I can see the LED behaves strangely when it crashes, either flickering a bit, staying on continuously, or staying off. I should point out it would function correctly up until this point.

Voltage is fine, around 5v, however different power supplies seem to result in the same issues.

Odd, but no doubt common... I thought best to continue this thread?

Is this likely a stack thing, or a resistor pull-down thing (which I don't completely understand, but think I have right), or something else entirely?

Cheers, Mark.

What's your circuit?

This is how I am wiring up MOSFETs in this situation:

The 10 ohm series resistor limits current out of the processor, and the 10K pulls the gate low when the chip is off/asleep.

In your case you would have the servo where "Load" is.