Servo power down / current drain / watchdog timer attiny85

Hi all,

I am new to this, however have managed to make some inroads into my project.
What I am attempting to do is to have a servo motor power on, complete a couple of swipes, and then shut down for a period of minutes before looping. I have been borrowing heavily from:

http://www.cunningturtle.com/attiny4585-servo-library/
and

http://www.insidegadgets.com/2011/02/05/reduce-attiny-power-consumption-by-sleeping-with-the-watchdog-timer/
The tricky part is that I want this system running for a period of months off a 4600mAh LifePO4 pack. As the servo continues to draw power as it waits for a pulse, I have added an N-MOSFET:

parts schematic: http://cdn.shopify.com/s/files/1/0045/8932/files/NDRIVE_NDriveModule.pdf?100736

and have wired it into the system assuming that the servo is operating as a motor like this:

Whether or not the circuit is 100% correct remains to be seen, however my issue is this...

If I load the attiny85 with the watchdog timer example given, It flashes correctly and measures in the uA range when sleeping as it should.
I am using the led in this as the gate signal for the MOSFET controller.
When I start adding the servo commands, in particular it seems to be the myServo.attach(1...) command, it starts reading around 2.5mA.

I've tried myServo.detach(), however this doesn't seem to solve the issue, which is not helped by the fact that every time i connect the ammeter in this state, the circuit becomes unstable/stops working. I also can't figure out how to make the servo reattach after detached for the next loop. When I remove it and reconnect the power as normal, it is relatively stable.

I feel bad asking for help, but this has had me stumped all week, trying with NPN transistors and all sorts to figure out what's going on.

Is there anything obvious in my code? The issue is in it somewhere, I just don't know Arduino well enough to see where, as you'll no doubt notice in the attached file.

Many thanks in advance for any assistance - I hope it proves useful to someone else once the bugs are worked out.

Mark.

timer_inoV8.ino (12.3 KB)

Before I look at your sketch, take a look at this page about power savings:

I've tried myServo.detach(), however this doesn't seem to solve the issue...

Were I in your shoes, I would focus on just that task. Create a very simple sketch that: attaches, moves, detaches. Ensure the processor stops sending pulses after the detach and restarts sending pulses after the attach. It is certainly possible that you have crossed paths with a bug in that library. The audience is relatively small and most folks do not need detach.

From what I've seen so far detach removes the interrupt but doesn't necessarily stop the timer.

Thanks for the amazingly fast response. Is there any way stop the clock if detach has not done it? Failing that; if i have hit a bug, is there another processor i might use which may be more stable? I want to make around 100 of these, so cost is a factor, however not as important as power consumption.

Many thanks again!

Mark.

Stop. All the clocks.

Yes you can do that.

Thanks Nick... I did a quick search and can't seem to find anything on stopping the clocks. I'd assume you'd want to stop it before calling the sleep function and attach the servo after wake. Is it just a case of a couple of lines or is there more to it?

Also curious if anyone has come across the ammeter screwing with everything before?

You stop the clocks by "turning them off". Here for example I stop timers 1 and 2.

// reset Timer 1 and Timer 2
  TCCR1A = 0;             
  TCCR1B = 0;              
  TCCR2A = 0;
  TCCR2B = 0;

As for the ammeter, yes that is a well-known problem. It is called "burden voltage". Basically the ammeter works by drawing current through a current-sensing resistor, which drops the voltage a bit, and it measures that drop. With a bit of maths (knowing the resistor, and the amount of voltage over the resistor) it can work out the current.

However the voltage drop may actually change the behaviour of the circuit under test. Read more here:

A different meter might help, or make up your own test if possible with a low-value resistor. You could buy his uCurrent device but they tend to be out of stock (as they are today).

Another alternative for you might be to measure the voltage drop yourself, and if it is (say 0.5V) increase the voltage to your circuit by that amount (while you are measuring current) to compensate.

That's great Nick, will try that monday.

Would I need to re-enable the clock before enabling the watchdog timer/calling sleep; or would that in itself do it?

I'm guessing you'd just insert the above string you mentioned before sleep?

Have a good weekend mate.

Do you mean "after calling sleep"?

I'm not sure, it looked like the servo code initialized the timer, so probably re attaching the servo would do it.

However some sleep modes turn the timers off. I would read the page I linked and do careful testing.

no, before... should it be after?

I read your page too BTW, great work, a little over my head in places; and not too sure what does and doesn't apply to the attiny.

Very good read very well done though, helps get an idea of what is possible.

marky_mark:
Would I need to re-enable the clock before enabling the watchdog timer/calling sleep; or would that in itself do it?

I'm not sure if we are talking about the same thing here.

Before calling sleep you want to shut down as much as you can. Afterwards you turn things (like the servos/timers) back on. Calling sleep itself will shut down quite a bit. The various sleep modes are relevant as to what exactly gets shut down.

Yeah, I think we're on the same page now :slight_smile:

Servo8Bit author here. I gotta admit, I didn't consider low power modes when I wrote this library. Once you attach the first servo ( by calling attach() ) the Servo8Bit library configures the timer (Timer1 by default) along with the compare match interrupt and then never touches it again. So when you detach all the servos, the timer is still left ticking and the compare match interrupt is still left active and continues to fire as usual, just with all servos detached, no pins are driven high or low.

What Nick said I think is good advice. I want to add to it and point out that after you wake up from sleep you need to turn timer1 back on by setting its prescaler back to the value it was before you disabled it and went to sleep. If you are using the default timer1, the code will look like this:

//create the variable, init it to zero.
uint8_t originalValue = 0;

//save the value of the Timer Counter Control Register 1
originalValue = TCCR1;

//turn off timer 1
TCCR1 = 0; 

/* go to sleep */
/* Time passes, grass grows, tectonic plates shift, then suddenly... */
/* wake up */

//Restore the TCCR1 register to re-enable the timer.
TCCR1 = originalValue;

//reattach the servo that was previously detached
myServo.attach();

Wow. That's pretty impressive backup!

Thanks Nick, Ilya and Coding Badly, I'll check how it works out on Monday when I'm back at my desk, and will post an update accordingly.

You guys/this forum rock.

You say you just want the servo to do a couple swipes? You -could- just hard code about a second of 1ms pin highs, then about a second of 2ms pin highs and be done with it.

Unless you are interested in controlling the actual servo position, then this wouldn't work.

-jim lee

Thanks Jim, the servo hits a switch as well, so controlling position is important. Good to know though.

cheers

Mark.

Servos stop within 20ms if you stop sending pulses..

maybe this would do it for you?

while(digitalRead(swithcPin1)) sendPulse(1);

then the other end would be..

while(digitalRead(swithcPin2)) sendPulse(2);

-jim lee

Hmm... No luck as yet.

Using what I think is a fairly bare bones version of the code; it's still not working for me. I'm not sure if I've inserted the mentioned lines in the correct positions, but it still draws power throughout the sequence. If I try to inesert myServo.detach(); anywhere, the servo will not move at all.

Can anyone see where I am going wrong here? I tried to add Nicks lines too, however I obviously need to create some sort of variable for that to work?

Thanks,

Mark.

/*
 * Watchdog Sleep Example 
 * Demonstrate the Watchdog and Sleep Functions
 * LED on digital pin 0
 * 
 * KHM 2008 / Lab3/  Martin Nawrath nawrath@khm.de
 * Kunsthochschule fuer Medien Koeln
 * Academy of Media Arts Cologne
 *
 * XXXXXXXXXXXXXXXXXXX originally by InsideGadgets (www.insidegadgets.com)
 * to suit the ATtiny85 and removed the cbi( MCUCR,SE ) section 
 * in setup() to match the Atmel datasheet recommendations
 */

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

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#include "Servo8Bit.h"

Servo8Bit myServo;  //create a servo object.
                    //a maximum of five servo objects can be created


int pinLed = 0;
//create the variable, init it to zero.
uint8_t originalValue = 0;
volatile boolean f_wdt = 1;

void setup(){
  pinMode(pinLed,OUTPUT);
  setup_watchdog(8); // approximately 4 seconds sleep
    }

void loop(){
  if (f_wdt==1) {  // wait for timed out watchdog / flag is set when a watchdog timeout occurs
    f_wdt=0;       // reset flag
 //Restore the TCCR1 register to re-enable the timer.
TCCR1 = originalValue;
//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);
    //save the value of the Timer Counter Control Register 1
    originalValue = 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
  }
}

// set system into the sleep state 
// system wakes up when wtchdog is timed out
void system_sleep() {
  cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF

  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
  sleep_enable();

  sleep_mode();                        // System sleeps here

  sleep_disable();                     // System continues execution here when watchdog timed out 
  sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON
}

// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {

  byte bb;
  int ww;
  if (ii > 9 ) ii=9;
  bb=ii & 7;
  if (ii > 7) bb|= (1<<5);
  bb|= (1<<WDCE);
  ww=bb;

  MCUSR &= ~(1<<WDRF);
  // start timed sequence
  WDTCR |= (1<<WDCE) | (1<<WDE);
  // set new watchdog timeout value
  WDTCR = bb;
  WDTCR |= _BV(WDIE);
}
  
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
  f_wdt=1;  // set global flag
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

OK, get rid of the smiley-faces. Edit your post, select the code, and hit the "#" button to put it into [ code ] tags.