Battery sleep wake up code

No the code should be basically the same (apart from you read pin 2 to find the switch state).

One thing here is that you will be burning power through the 1k pull-up when the switch is closed. This is non-ideal.

Two ways to address this:

  1. Increase this resistor to reduce the current. However, this will mean the pulse on pin 1 is longer, as the cap is charged through the 1k/10k resistors in series.

  2. Use another XOR gate to buffer the signal. Ground one input, and tie the other input to the switch. Pull up the switch with a really big resistor (eg 470k). As the gate input is high impedance, there’s little leakage and you can do this. Tie the output of this second XOR gate to the input of the first one (ie, where the switch is connected now). You don’t need a pull-up between the two gates.

…but, that’s an optimization once you have got this working :slight_smile:

Thanks for pointing out that power issue @Hugo. (After I had posted I recalled that the genesis of this thread was about saving power, so that that low value pullup really wasn’t the way to go). Good call at adding in one of the unused gates.

i still cant get it to work! i just keep going though batteries! is there a breakout board or something easier to make it work!

I’m wondering if the April board itself is killing your batteries.

If I may suggest a different strategy. Perhaps have the reed switch connect power to the April board/IMP at the BATT/USB jumper (on the batt side).

What is the reed switch actually looking for? Is that all your application is reading?

If the reed switch is used for a Door/window/something physical and likely to be closed for several seconds, that should power the IMP for a long period of time (long enough than the IMP needs to process your code anyways)

Your process doesn’t need to read any inputs then, it simply runs your process and sends to the agent every time whenever it cold boots.

The imp then processes your application, perhaps waits a short period (if needed for debounce or re-trigger) and goes to deep sleep waiting for the reed switch to drop the power out.

If the reed switch connects (on and off again) in less time than IMP needs to run your process you could have a FET/Transistor in parallel with the reed switch controlled by the IMP.

So when the IMP boots, the first thing it does is activate the FET/Transistor to hold its own power on. Then run the process do its updating then drop the FET/Transistor out. In my day we called this ‘self quenching’… ha ha

That should change the standby power draw to zero. And will last as long as the batteries can last.

There are some ramifications to having the power off all the time; but depending on the application maybe you don’t care.

Thanks @Hyperone but I don’t understand any of it! All I am trying to do is run an imp of a 2 aa batteries for longer than 2 minutes. i don’t have any history with electronics. can you draw what you are saying.

@Hugo and @LarryJ here is a picture of how i have it hooked up (hook up only not parts)
hopefully one of you guys can tell me now i have tried 20 different ways!!

@jr -

I took a quick look at your wiring, and have a couple of initial comments …

How many batteries are you using, two or four? (I looked back through the thread, and don’t believe this was ever mentioned). Hopefully it’s four (or at least three), because if you’ve only got two (as your diagram might suggest) you’re starting with a really marginal power situation, which could certainly explain why you’re not seeing any battery life (if the circuit were even to work at all). I also assume you’ve got the BAT jumper set correctly on the April card?

Also, it looks like you’ve got some mis-wiring of your XOR. I note you’ve got pins 4 and 10 (vs. 5 and 9) grounded. These are outputs, so that’s not a particularly good thing.

I’ve got a few add’l observations, but this is just some initial feedback. (Also, what’s the value of the resistor between pins 1 and 2? I’ll try to update that schematic to also show using one of the currently unused XOR gates as @Hugo mentioned in an earlier post … I liked that suggestion for your circuit).

Hi @LarryJ thanks for the reply I only have 2 AA batteries hooked up ill try 4 to start with. Yes battery jumper set correct. Ill get back to u after I change to 4 batteries. Thanks

Ah, ok … I’m thinking that’s very likely a large part of your ‘batteries run 2 minutes’ problem. (And I did just note your earlier comment about only using two AAs … sorry about that). You’ll barely make the 3.3v minimum with only two cells … and then not for very long at all.

Given we get your hardware correct, you’ll still have to deal with properly implementing the deep-sleep code … but at least you should be getting closer with the hardware and power now.

(I did update that schematic utilizing an add’l XOR gate as a buffer stage. Just did it quickly, but I think I’ve got it right should you choose to update your implementation).

Oh thanks mate! Can I use a 9 volt battery or is the voltage to high. 9 volt would fit in the case I have made.

From a voltage standpoint you should be fine with 9v. The April is spec’d to 17v input, and you’re likely operating at low duty cycle, so I don’t anticipate any regulator heating/stress issues.

Not a battery expert here, but from a capacity standpoint you might consider a 9v lithium. It’s likely better with peak current and overall capacity for your application.

Ok thanks ill try a lithium one and get back to you. Thanks again for your help

ok i have had it running on the 9v for a couple of hours now no problems i just have to change some code for the pin2 part. What would you think is a good deep-sleep code any links on how to properly implement it? Thanks so much for your time i can ask some dumb questions at times

Happy to hear you’re working through your circuit/power issues @jr.

Re: code - it’s a bit hard to address at this point as I’m not sure it’s been explained what your project or application is. It might be useful if you elaborate a bit and/or expose what code you have working, what you’re trying to do, and what problems you’re having. This would hopefully elicit some helpful feedback.

That said, assuming that perhaps in general you’re trying to monitor some infrequent event (indicated by your relay closure), wake up the imp and have it take some action, then sleep again 'til the next event … you might review this thread. Within, @beardedinventor has embedded a couple of useful code snippets that are applicable to implementing a general pin 1 wakeup and subsequent deep sleep sequence on the device side.

hi Larry Yeah its just monitoring opening and closing like in the example you have given me on the thread but using a reed switch. yesterday i used an alkaline 9V and it lasted about 7 hours. I rewired it like in your new drawing and trying it with @beardedinventor code and a lithium battery but it turns on and nothing else happens does not read the reed switch. I must have wired it up wrong. Ill have a look more into it. I am using a 1k and 10k resistor and 63v 1 uf capacitor. I am pretty sure its the way i hooked it up because if i take the deepsleep code out as before its not working and the wiring was the only thing i have changed from when it was working. Thanks Josh

It will be probably be difficult to make progress at this point Josh without having others being able to review your code. The ‘read the reed switch’ part is not part of the code from that other thread … it is something you would need to add. The only thing that previous code would do as-is would be to turn on the imp for a few seconds each time you open or close your relay, then sleep. If it is doing this (which you seem to indicate) you may have things wired properly, as well as have that wakeup / deep sleep code snippet implemented correctly.

Oh ok I see what your saying.
// Copyright © 2013 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
//
// Description: Debounced button press with callbacks

class Button{
static NORMALLY_HIGH = 1;
static NORMALLY_LOW = 0;
_pin = null;
_pull = null;
_polarity = null;
_pressCallback = null;
_releaseCallback = null;

constructor(pin, pull, polarity, pressCallback, releaseCallback){
    _pin             = pin;               //Unconfigured IO pin, eg hardware.pin2
    _pull            = pull;              //DIGITAL_IN_PULLDOWN, DIGITAL_IN or DIGITAL_IN_PULLUP
    _polarity        = polarity;          //Normal button state, ie 1 if button is pulled up and the button shorts to GND
    _pressCallback   = pressCallback;     //Function to call on a button press (may be null)
    _releaseCallback = releaseCallback;   //Function to call on a button release (may be null)

    _pin.configure(_pull, debounce.bindenv(this));
}

function debounce(){
    _pin.configure(_pull);
    imp.wakeup(0.050, getState.bindenv(this));  //Based on googling, bounce times are usually limited to 10ms
}

function getState(){ 
    if( _polarity == _pin.read() ){
        if(_releaseCallback != null){
            _releaseCallback();
        }
    }else{
        if(_pressCallback != null){
            _pressCallback();
        }
    }
    _pin.configure(_pull, debounce.bindenv(this)); 
}

}

//Example Instantiation
b1 <- Button(hardware.pin2, DIGITAL_IN_PULLUP, Button.NORMALLY_HIGH,
function(){
//server.log(“Button 1 Pressed”)
// create table and send to agent
local data = { “button1”: “CLOSED”, “mykey”: “23123faa5ba5ceee” }
agent.send(“senddata”, data);
},
function(){
//server.log(“Button 1 Released”)
// create table and send to agent
local data = { “button1”: “OPENED”, “mykey”: “23123faa5ba5ceee” }
agent.send(“senddata”, data);
}
);
function hardwareWakeup() {
agent.send(“pin1wakeup”, null);
}
// if we woke up because of PIN1, notify agent
if(hardware.wakereason() == WAKEREASON_PIN1) {
hardwareWakeup()
}

// we can only go to deepsleep if hardware.pin1 is low
function TrySleep() {
if (hardware.pin1.read() == 0) {
// if pin1 is low, we can sleep
server.log(“Going to sleep”);
imp.onidle(function() { imp.deepsleepfor(3600.0); });
} else {
// if we can’t go to sleep yet, wait a while, them try again
server.log(“Can’t sleep.”);
imp.sleep(5.0);
imp.onidle(TrySleep);
}
}

hardware.pin1.configure(DIGITAL_IN_WAKEUP);

// go to sleep
TrySleep();

this is the device code sorry I don’t know how to wrap it. it is going to sleep and waking up once the reed switch is openned and closed but I am not getting the agent send data anymore.

` // Copyright © 2013 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
//
// Description: Debounced button press with callbacks

class Button{
static NORMALLY_HIGH = 1;
static NORMALLY_LOW = 0;
_pin = null;
_pull = null;
_polarity = null;
_pressCallback = null;
_releaseCallback = null;

constructor(pin, pull, polarity, pressCallback, releaseCallback){
    _pin             = pin;               //Unconfigured IO pin, eg hardware.pin2
    _pull            = pull;              //DIGITAL_IN_PULLDOWN, DIGITAL_IN or DIGITAL_IN_PULLUP
    _polarity        = polarity;          //Normal button state, ie 1 if button is pulled up and the button shorts to GND
    _pressCallback   = pressCallback;     //Function to call on a button press (may be null)
    _releaseCallback = releaseCallback;   //Function to call on a button release (may be null)

    _pin.configure(_pull, debounce.bindenv(this));
}

function debounce(){
    _pin.configure(_pull);
    imp.wakeup(0.050, getState.bindenv(this));  //Based on googling, bounce times are usually limited to 10ms
}

function getState(){ 
    if( _polarity == _pin.read() ){
        if(_releaseCallback != null){
            _releaseCallback();
        }
    }else{
        if(_pressCallback != null){
            _pressCallback();
        }
    }
    _pin.configure(_pull, debounce.bindenv(this)); 
}

}

//Example Instantiation
b1 <- Button(hardware.pin2, DIGITAL_IN_PULLUP, Button.NORMALLY_HIGH,
function(){
//server.log(“Button 1 Pressed”)
// create table and send to agent
local data = { “button1”: “CLOSED”, “mykey”: “23123faa5ba5ceee” }
agent.send(“senddata”, data);
},
function(){
//server.log(“Button 1 Released”)
// create table and send to agent
local data = { “button1”: “OPENED”, “mykey”: “23123faa5ba5ceee” }
agent.send(“senddata”, data);
}
);
function hardwareWakeup() {
agent.send(“pin1wakeup”, null);
}
// if we woke up because of PIN1, notify agent
if(hardware.wakereason() == WAKEREASON_PIN1) {
hardwareWakeup()
}

// we can only go to deepsleep if hardware.pin1 is low
function TrySleep() {
if (hardware.pin1.read() == 0) {
// if pin1 is low, we can sleep
server.log(“Going to sleep”);
imp.onidle(function() { imp.deepsleepfor(3600.0); });
} else {
// if we can’t go to sleep yet, wait a while, them try again
server.log(“Can’t sleep.”);
imp.sleep(5.0);
imp.onidle(TrySleep);
}
}

hardware.pin1.configure(DIGITAL_IN_WAKEUP);

// go to sleep
TrySleep();
`

Anyone have any Ideas for this Please!!! It would help a lot if anyone has any idea’s.
I know I am dumb and it will be something easy for most of you guys but i just can’t get my head around it. I think it might be because I am in pin2 from the open and close part but if I go into pin 1 i will not get the battery life?