How to turn off Arduino to save battery?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 7
- Joined: Thu Sep 09, 2010 8:08 pm
How to turn off Arduino to save battery?
I wanted to know if there is a way to program or write a code to turn off the Arduino after it runs once.
- adafruit_support_bill
- Posts: 88142
- Joined: Sat Feb 07, 2009 10:11 am
Re: How to turn off Arduino to save battery?
You can put it to sleep.
-
- Posts: 7
- Joined: Thu Sep 09, 2010 8:08 pm
Re: How to turn off Arduino to save battery?
Hi, I'm having a little trouble on how to write a code in order to make the Arduino wake up when the force sensor(valF>=thresHold && valFenabled) has been pressed and goes to sleep after valSenabled = 1;. Then turns the Arduino back on when the switch sensor(valS == HIGH && valSenabled) is pressed and then goes to sleep after valFenabled = 1;. My code looks like this:
[Edit - moderator - use code block]
[Edit - moderator - use code block]
Code: Select all
if (valF>=thresHold && valFenabled) { // waits for force sensor to equal or pass pressure threshold and then:
valFenabled = 0; //signal that button has been pressed before
delay(1000); //
digitalWrite(RedPin, LOW); //
digitalWrite(GreenPin, LOW); //
delay(1000); //
digitalWrite(RedPin, HIGH); //
digitalWrite(GreenPin, HIGH); //
delay(1000); //
valSenabled = 1;
}
if (valS == HIGH && valSenabled) { // waits for switch to be pressed, and then:
valSenabled = 0; //signal that button has been pressed before
delay(1000);
digitalWrite(GreenPin, LOW);
digitalWrite(RedPin, LOW);
delay(400);
digitalWrite(GreenPin, HIGH);
digitalWrite(RedPin, HIGH);
delay(400);
valFenabled = 1;
}
}
Last edited by BVILLAN on Mon Feb 28, 2011 6:33 pm, edited 1 time in total.
- adafruit_support_bill
- Posts: 88142
- Joined: Sat Feb 07, 2009 10:11 am
Re: How to turn off Arduino to save battery?
As mentioned in the link I posted earlier, the only way to wake up from a sleep is via interrupt. The second set of example code in the link shows how to wake up based on an interrupt on pin 2.
The question then is how to trigger an interrupt on pin 2 with a force sensor: That will depend on what kind of force sensor you are talking about. If it is an FSR, you enable the pullup resistor on pin 2 and connect the FSR between pin 2 and GND. When there is sufficient pressure on the FSR, it will pull pin 2 low and generate the interrupt.
The question then is how to trigger an interrupt on pin 2 with a force sensor: That will depend on what kind of force sensor you are talking about. If it is an FSR, you enable the pullup resistor on pin 2 and connect the FSR between pin 2 and GND. When there is sufficient pressure on the FSR, it will pull pin 2 low and generate the interrupt.
-
- Posts: 7
- Joined: Thu Sep 09, 2010 8:08 pm
Re: How to turn off Arduino to save battery?
The type of force sensor is FSR. So, how would that be writen using my code. I'm sorry, I'm a newbie.
- adafruit_support_bill
- Posts: 88142
- Joined: Sat Feb 07, 2009 10:11 am
Re: How to turn off Arduino to save battery?
Try the second example code in the link. When you put pressure on the FSR it reduces the resistance and pulls pin 2 low. This will generate the interrupt expected by the code.
-
- Posts: 7
- Joined: Thu Sep 09, 2010 8:08 pm
Re: How to turn off Arduino to save battery?
I got it to work thanks!
-
- Posts: 13
- Joined: Thu Nov 15, 2012 8:04 pm
Re: How to turn off Arduino to save battery?
adafruit_support wrote:As mentioned in the link I posted earlier, the only way to wake up from a sleep is via interrupt. The second set of example code in the link shows how to wake up based on an interrupt on pin 2.
The question then is how to trigger an interrupt on pin 2 with a force sensor: That will depend on what kind of force sensor you are talking about. If it is an FSR, you enable the pullup resistor on pin 2 and connect the FSR between pin 2 and GND. When there is sufficient pressure on the FSR, it will pull pin 2 low and generate the interrupt.
Please can you explain to me what you mean by " enable the pullup resistor"? . I m using FSR, planning to connect it to pin 2 .
- adafruit_support_bill
- Posts: 88142
- Joined: Sat Feb 07, 2009 10:11 am
Re: How to turn off Arduino to save battery?
See the paragraph titled "Pullup Resistors" here: http://arduino.cc/en/Tutorial/DigitalPinsPlease can you explain to me what you mean by " enable the pullup resistor"? .
-
- Posts: 13
- Joined: Thu Nov 15, 2012 8:04 pm
Re: How to turn off Arduino to save battery?
Thanks very much, I understand that part very well. But the second example code on the link you provided only interrupt when pin 12 is grounded for momentary, and pin 2 is +5v for the LED to be OFF. And when pin 12 is removed from the ground and then pin 2 is grounded that LED will be ON? ...For example what if i wanted to use FSR at pin 2 connected to ground and i want my arduino to be active without grouding pin 12 for momentary for the arduino to fall asleep, how can i do that in the example code you provided, so i do not have to go through that hassel making arduino to go to idle?adafruit_support wrote:See the paragraph titled "Pullup Resistors" here: http://arduino.cc/en/Tutorial/DigitalPinsPlease can you explain to me what you mean by " enable the pullup resistor"? .
- zappo
- Posts: 6
- Joined: Sat Oct 27, 2012 11:22 pm
Re: How to turn off Arduino to save battery?
I don't know how comfortable you are installing and using new libraries, but if you're ok with it check out the pinchangeint library for an easy to use interrupt that can be put on any pin, rather than just the two digital pins that the built-in interrupt code uses:
http://code.google.com/p/arduino-pinchangeint/
Here's a project I've been working on lately that puts the arduino to sleep after it finishes the code in loop() once, and uses pinchangeint interrupts to wake up when a change is detected on an analog pin. It might be just what you're looking for:
https://github.com/smokeyser/Remote2/bl ... emote2.ino
http://code.google.com/p/arduino-pinchangeint/
Here's a project I've been working on lately that puts the arduino to sleep after it finishes the code in loop() once, and uses pinchangeint interrupts to wake up when a change is detected on an analog pin. It might be just what you're looking for:
https://github.com/smokeyser/Remote2/bl ... emote2.ino
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: How to turn off Arduino to save battery?
That's what the first example does. Call SleepNow whenever you want to go to sleep. Ground pin 2 to wake up.zajebiscie wrote:For example what if i wanted to use FSR at pin 2 connected to ground and i want my arduino to be active without grouding pin 12 for momentary for the arduino to fall asleep, how can i do that in the example code you provided, so i do not have to go through that hassel making arduino to go to idle?
-
- Posts: 13
- Joined: Thu Nov 15, 2012 8:04 pm
Re: How to turn off Arduino to save battery?
Thanks vary much for your quick reply. Please could you tell me how long it will take the arduino to go to sleep when it is on idle mode, and how can we reset the time to our own time after FSR is not in use?driverblock wrote:That's what the first example does. Call SleepNow whenever you want to go to sleep. Ground pin 2 to wake up.zajebiscie wrote:For example what if i wanted to use FSR at pin 2 connected to ground and i want my arduino to be active without grouding pin 12 for momentary for the arduino to fall asleep, how can i do that in the example code you provided, so i do not have to go through that hassel making arduino to go to idle?
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: How to turn off Arduino to save battery?
When you call SleepNow, it will only take a few microseconds for the arduino to go to sleep.zajebiscie wrote:Please could you tell me how long it will take the arduino to go to sleep when it is on idle mode, and how can we reset the time to our own time after FSR is not in use?
I don't understand what you mean about resetting the time. Is there a clock associated with this project?
-
- Posts: 13
- Joined: Thu Nov 15, 2012 8:04 pm
Re: How to turn off Arduino to save battery?
Thanks for your help. There is no clock associated with the project, but all i wanted my arduino to go to sleep (idle) when not in use, and i want to use FSR sensor to interrupt by waking up the arduino while pressing the FSR sensor, and as soon as FSR sensor is release, it will go back to sleep(idle) and move a motor to clockwise. Below is the code i have been working on for the past two days, please could you take a look at it and see if i m missing anything. thanksdriverblock wrote:When you call SleepNow, it will only take a few microseconds for the arduino to go to sleep.zajebiscie wrote:Please could you tell me how long it will take the arduino to go to sleep when it is on idle mode, and how can we reset the time to our own time after FSR is not in use?
I don't understand what you mean about resetting the time. Is there a clock associated with this project?
Code: Select all
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/io.h>
//
void setup(void)
{
DDRD &= B00000011; // set Arduino pins 2 to 7 as inputs, leaves 0 & 1 (RX & TX) as is
DDRB = B00000000; // set pins 8 to 13 as inputs
PORTD |= B11111100; // enable pullups on pins 2 to 7
PORTB |= B11111111; // enable pullups on pins 8 to 13
pinMode(13,OUTPUT); // set pin 13 as an output so we can use LED to monitor
digitalWrite(13,HIGH); // turn pin 13 LED on
}
//
void loop(void)
{
// Stay awake for 1 second, then sleep.
// LED turns off when sleeping, then back on upon wake.
delay(100);
sleepNow();
}
//
void sleepNow(void)
{
/* Now is the time to set the sleep mode. In the Atmega8 datasheet
* http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35
* there is a list of sleep modes which explains which clocks and
* wake up sources are available in which sleep modus.
*
* In the avr/sleep.h file, the call names of these sleep modus are to be found:
*
* The 5 different modes are:
* SLEEP_MODE_IDLE -the least power savings
* SLEEP_MODE_ADC
* SLEEP_MODE_PWR_SAVE
* SLEEP_MODE_STANDBY
* SLEEP_MODE_PWR_DOWN -the most power savings
*
* the power reduction management <avr/power.h> is described in
* http://www.nongnu.org/avr-libc/user-manual/group__avr__power.html
*/
// Set pin 2 as interrupt and attach handler:
attachInterrupt(0, pinInterrupt, LOW);
delay(100);
//
// Choose our preferred sleep mode:
set_sleep_mode(SLEEP_MODE_IDLE);
//
// Set sleep enable (SE) bit:
sleep_enable();
power_adc_disable();
power_spi_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
power_twi_disable();
//
// Put the device to sleep:
digitalWrite(13,LOW); // turn LED off to indicate sleep
sleep_mode();
//
// Upon waking up, sketch continues from this point.
sleep_disable();
digitalWrite(13,HIGH); // turn LED on to indicate awake
}
//
void pinInterrupt(void)
{
detachInterrupt(0);
}
Please be positive and constructive with your questions and comments.