Hi, If you want to power a servo with batteries and move only once in a while, you should turn it off with no more power consumption, let's see how we can do this energy saving mode.
In the code, a servo is moved from a low to a high position, the higher position is maintained for one second, and after the servo returns to the lower position, it remains there for about 20 seconds, in this time the ATmega328p (or Arduino on breadboard) goes to sleep and the servo is turned off.
In order to turn off the servo must perform this procedure :
1 ) Use the detach() command to disconnects the servo from the pin to which it is connected, in this case D9
2 ) The pin connected to the servo must become an input pin, in this case D9
3 ) Take off the power supply to the positive of the servo using a p mosfet
4 ) Put in sleep mode the microcontroller
The total power consumption, so the Arduino + servo, change from approximately 60mA with both active, at about 7μA with microcontroller in sleep and servo turned off, normally only the servo when is stopped consumes approximately from 6 to 20mA , the microcontroller awake around 10mA.
I hope you like it.