Go Down

Topic: Servo power down / current drain / watchdog timer attiny85 (Read 4 times) previous topic - next topic


Mar 09, 2012, 06:24 am Last Edit: Mar 09, 2012, 06:27 am by marky_mark Reason: 1
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:


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.


Nick Gammon

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


Coding Badly

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.

Nick Gammon

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!


Nick Gammon



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?

Nick Gammon

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

Code: [Select]
// 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.

Nick Gammon

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.

Nick Gammon

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.



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


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.

Go Up