Arduino Sleep to conserve battery - HELP pls

Good day

I am a real novice and interested in learning. I have spent lots of time trying to integrate my sketch with various sleep methods from the net, but have not been successful and thought you might be able to help. using (SLEEP_MODE_PWR_DOWN)

Basically my project uses a touch sensor to turn a servo motor from 0 to 180 and back to zero.

Most of the time there is no activity and the arduino nano and servo motor kill my batteries after a few days. Sensor may only be touched once every few days if that.

So putting the arduino to sleep will help and then I need to figure out how to use a logic level mosfet to cut power to the Servo SG90 until the touch sensor is touched.

What arduino pins connect to the gate, drain , and source and do i need any resistors to the Gate or a pull down resistor?

Here is my code

#include <Servo.h>

// constants won't change
const int SENSOR = 2; // Arduino pin connected to motion sensor's pin 7
const int SERVO = 9; // Arduino pin connected to servo motor's pin 9

Servo servo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

// variables will change:
int lastMotionState; // the previous state of motion sensor
int currentMotionState; // the current state of motion sensor

void setup() {

Serial.begin(9600); // initialize serial
pinMode(SENSOR, INPUT); // set arduino pin 9 to input mode
servo.attach(SERVO); // attaches the servo on pin 12 to the servo object

servo.write(0);
currentMotionState = digitalRead(SENSOR);
}

void loop() {
lastMotionState = currentMotionState; // save the last state
currentMotionState = digitalRead(SENSOR); // read new state

if (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: LOW -> HIGH
Serial.println("OFF");
servo.write(0);
delay(100); // waits (X)ms for the servo to reach the position
}
else
if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
Serial.println("ACTIVE");
servo.write(180);
delay(2000); // waits (x)ms for the servo to reach the position
}
delay(10); // repeats program - larger delay - trigger is
}

I would have liked to show my wiring diagram but not quite sure how to do it

Your help would be greatly appreciated. I have been trying to figure this out for months now. Your comments are greatly appreciated

Best Regards,
Anthony

https://forum.arduino.cc/

This definitive site for low power has a basic mosfet switch circuit for devices attached.

http://www.gammon.com.au/power

It also addresses many of the issues that make a truly low power sleeping device a bit of a challenge; you did mention a lack of success, so perhaps here you would find good advice.

There is no substitute for measuring the current when you are chasing the last microamperes.

A cell phone picture of a hand drawn schematic is a good and easy start to sharing your wiring concepts.

What touch sensing device or technique are you using? Obvsly it will need to be actively listening for a touch, this will be the lower limit to the consumption of power, indeed it will probably be able to make it 99.99 percent of the power used in sleep.

a7

Processing: WTP-PROD-V3_schem.pdf...

alto777
Yes I have looked at the website that you mentioned. I just don't know how to intergrate it within my program.

Touch sensor is a TTP223

If that was supposed to be a link to your circuit, please try again it does not operate as you posted it.

I suggest that rather than looking at that website you study it.

Try to get a small example program working that does nothing but go to sleep and wake up on response to, say, the press of a button.

Hint: you can practically cut and paste such an example from the website.

Then in your program, decide why and when you want to go to sleep, use the same code you have familiarized yourself with, just transplanted.

Wake it up with a pushbutton. When that works, and after you have played with the touch sensor in a similar fashion, wake up your program with the touch sensor.

Worry about powering up and down the servo last, there is circuit on that page and an explanation of how it works and how to wire it to your Arduino.

It may sound like work. For a good reason. But if you break the project into sections and get each part working independently, you will have less trouble combining them.

sleeping
waking up by pushbutton
responding to touch
waking up to touch
powering up and down the servo

Give it a shot and when you have problems with something you have tried which you think should have worked but did not, post the code and the circuit and we can help.

What Arduino board are you using? Some boards are tots inappropriate, others may need modifications for the soundest sleep to be realized.

a7

Hi a7

Being a novice to programing I found your comments a somewhat condescending and not helpful. and by the way the system here does not allow new users to upload attachments

The reason I joined this forum was to learn and get help if I get stuck or share some experience if possible.

I had already reviewed the info from http://www.gammon.com.au that you mentioned before posting here and really attempted for a few day to use his code and intergrade it with mine. Was unsuccessful hence my post here

My sketch is simply using a touch sensor to activate a servo motor from initial 0 to 180 then back to 0. then I want the Adruino NANO to go to sleep to conserve some battery life.

so if anyone can help intergrade the gammon's sketch below into mine it would be greatly appreciated. As I'm out of ideas.

**

#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
#define interruptPin 2 //Pin we are going to use to wake up the Arduino

void setup() {
Serial.begin(115200);//Start Serial Comunication
pinMode(LED_BUILTIN,OUTPUT);//We use the led on pin 13 to indecate when Arduino is A sleep
pinMode(interruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}

void loop() {
delay(5000);//wait 5 seconds before going to sleep
Going_To_Sleep();
}

void Going_To_Sleep(){
sleep_enable();//Enabling sleep mode
attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
digitalWrite(LED_BUILTIN,LOW);//turning LED off
delay(1000); //wait a second to allow the led to be turned off before going to sleep
sleep_cpu();//activating sleep mode
Serial.println("just woke up!");//next line of code executed after the interrupt
digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}

void wakeUp(){
Serial.println("Interrrupt Fired");//Print message to serial monitor
sleep_disable();//Disable sleep mode
detachInterrupt(0); //Removes the interrupt from pin 2;
}


my sketch

#include <Servo.h>

// constants won't change
const int SENSOR = 2; // Arduino pin connected to motion sensor's pin 7
const int SERVO = 9; // Arduino pin connected to servo motor's pin 9

Servo servo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

// variables will change:
int lastMotionState; // the previous state of motion sensor
int currentMotionState; // the current state of motion sensor

void setup() {

Serial.begin(9600); // initialize serial
pinMode(SENSOR, INPUT); // set arduino pin 9 to input mode
servo.attach(SERVO); // attaches the servo on pin 12 to the servo object

servo.write(0);
currentMotionState = digitalRead(SENSOR);
}

void loop() {
lastMotionState = currentMotionState; // save the last state
currentMotionState = digitalRead(SENSOR); // read new state

if (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: LOW -> HIGH
Serial.println("OFF");
servo.write(0);
delay(100); // waits (X)ms for the servo to reach the position
}
else
if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
Serial.println("ACTIVE");
servo.write(180);
delay(2000); // waits (x)ms for the servo to reach the position
}
delay(10); // repeats program - larger delay - trigger is
}

Please edit your post to add code tags. See the How to get the best out of the forum post, at the head of every topic.

Sry, I apologize profuciously. I will, as required by the internet and the rules of these fora, arrange to have myself flogged with a damp trout. And I will try to do better.

Have you run the small sleep example you provide and appreciated its behaviour?

 Going_To_Sleep();

and anything you need to drag out of that example, the variables and the interrupt routine and so forth, are all you need.

Going_To_Sleep might better be named TakeANap. In any event, when you call it, it will not return until the processor is waked up.

So.

Put Going_To_Sleep(); in your code wherever you want. Wherever you want to have the processor go into low power mode. All functioning will cease right there at that point, only to continue when the interrupt signal on the pin assigned to it is invoked.

That's it. Put the thing to sleep wherever, and count on perfect continuation of the process you were in the middle of once the signal comes in.

I recommend that you use a pushbutton as a proxy for the touch switch until you get the sleep thing working. I don't know the touch switch you linked; ones I have used can be exciting enough to work with outside the kind of circumstances you seek to surround yours with.

Again, my sincere appy polly loggies, good luck w/ your project.

a7

1 Like

So, starting at the beginning, what was the result of the basic sketch in the Gammon tutorial, with no other code or components added and what was the sleep current ?

Until you know the answers to those questions you wont be able to work out if the short battery life is caused by the servo or maybe something else.

a7
No need to destroy a good trout. Thanks for the apology.

I will continue my quest this weekend. Thanks for the info.

srnet

Yes the the gammon code on its own drops the current from around 40ma to 20ma

Now begins the hunt 20 mA is a ton of current, you should be shooting for 20 uA. Or less.

What board are you using? Some may need slight preparation.

Was that measurement with no servos attached? Or anything at all?

a7