Battery sleep wake up code

Hi I am having trouble making my batteries last any length of time. I have a read switch that sends a post when an event takes place and all works really well but the battery only lasts a day I have tried many different code and cant seam to make it work. Thanks

You need to send your imp to deep sleep to save power drain on the battery. With this sort of code the battery will last for weeks:

`// Log an ADC value each minute, only tell the server once an hour

t <- time();

function hourly() {
agent.send(“hourlydata”,; = “”;

function log() {
local min = (t/60) % 60;
// = + format(" %d",; = + format(" %0.3f", hardware.voltage());
if (min == 00) {
imp.onidle(function() { imp.deepsleepfor(60 - (time() % 60)); });

if (!(“nv” in getroottable())) {
nv <- { data = “” };
server.log(“clearing nv memory”);


Hi Dr Jack thanks for that.
Is there a way for the batteries to last a couple of years? I am willing to put in a high Quilty one

The current draw of an imp is 6 microamps in deep sleep, jumping up to milliamps when active, and as much as 240 milliamps when on and using wifi (though perhaps only for one second). The deep sleep current from your battery depends on the voltage regulator efficiency and the battery voltage.

So the drain on your battery is made up of the deep sleep current as above, plus any standby current for the rest of your circuit, plus the time average of the periodic consumption when you wake up and measure something, plus the time average of the peak consumption when you wake up and communicate to the agent.

To make the battery last as long as possible, wake up as infrequently as possible, and talk to the server only when you really have to. If you need a couple of years of life, you should also be considering the battery chemistry and how long it will last without draining itself.

I have an example of a little 850mA LiPo running a C3V0 (imp002), measuring and transmitting temperature and humidity every ten minutes, and that lasts happily for two months. I also monitor the battery voltage to see when I need to recharge the LiPo.

If you are monitoring your mail box / cat flap, perhaps it is outside and you can consider a small photovoltaic cell to keep it topped up?

With an appropriate low power design, battery life should roughly depend on how many wakes (from deep sleep) your device does. Therefore how many wakes/day you need for your application could give you an approximate indication of how long your batteries will last. (Coupled of course with your choice of cell as you mention. Environmental conditions are also a factor you might need to consider).

Here are a couple links if you haven’t seen them related to low power design, and the Nora reference design for a low power node.

I’ve got a little project going (based on Nora) which wakes a couple times per day. I’m just using two AAA alkalines at the moment (though will likely switch to lithium). It’s been running a couple months now with no significant change in battery voltage, so I don’t think a year or two of operation is unreasonable to expect.

Again though, the first things are to minimize your overall circuit power, and deep sleep as @DrJack recommends.

Sounds like you need a little circuit to generate a wakeup pulse every time your reed switch changes state. Usually you can do this with a single XOR gate, a resistor and a capacitor - possibly this features in one of our reference designs… otherwise can draw you a diagram.

With that, the imp will essentially be totally off when nothing is happening, which is a good way to make batteries last years. If you look at the nora reference design, you can see that we can get 80,000 wakes (ie, events) from 2xAA lithium cells so many years life is absolutely possible.

@Hugo–I’d like to see the wiring for a circuit that would allow me to wake up the imp and have the signal present for input, likely a digital input. I usually use wall power, but could benefit from being able to trigger off a digital input to wake up a battery powered imp (as in the reed switch example above). If something is already out there, please just point me in the right direction.

@LarryJ, is your Nora code based on the reference code, or have you managed to make something a bit simpler? I find almost any change I attempt in that code results in the device sending unhandled events to the agent.

I just actually only copied the Nora power supply related stuff @DrJ (for a mailbox alert) … haven’t even looked at any reference code.

@jr -
from your description, it sounds as if you have the XOR circuit hooked up ok, but it’s difficult to tell exactly what the three connections on the reed switch are, and how you’re using it to create the input signal. (It also appears that you may not have the desired signal going to pin 2 per @Hugo’s previous suggestion). Might you have a schematic and/or p/n for the switch? (If we’re seeing COM - NO - NC on the side of the switch, I’m not sure I understand how it’s s’posed to function).

And yes, you would need to supply power (i.e. Vcc … pin 14) to your device. It’s a bit difficult to make out from your pictures, but I don’t believe I see anything connected to pin 14.

@Hugo, I can’t find any designs out there if anyone can send me a link or draw one up for me it would be great. It seams like i have the reed switch in the wrong pin (1) and need to ad the capacitor and the resistor some how and ad the nora code. Is that what you are saying?

Anyone got any idea’s with the above post

Is it possible that i have it hooked up wrong to. i am using pin1 for the reed switch in one end and in the other from the reed switch to the ground on the imp. I am i ment to have the ground to the battery insteed?

Is it possible to wake up without turning the wifi on?

@brolly759 - I responded here.

See page 4, “dual edge detector”, the top circuit on the page.

Connect the reed switch to IN, pin 1 to OUT, and also connect the reed switch to another pin on the imp (so when you wake you can tell which state the switch is now in).

You pick R/C to set your pulse length. 10ms should be fine, so you could do that with R=10k and C=1uF.

You can get single-gate XORs eg and these take, typically, 0.1uA.

Very helpful! Is the concept here that the first pulse is to wakeup the imp (if it’s not awake) and the second pulse to make the call to the specified callback which will read() the other pin the reed switch is connected to? Perhaps a valuable addition to the Developers Guides?

The pulse is on any change - the point here is that any time the state changes, the imp will wake, read the pin (just as it would if you were polling it) and know the new state in absolute terms.

Yep, could be a good one as an example in the design guide…

@Hugo Ok thanks I have ordered the gate. When I went to go buy the r and c they wanted to know what voltage? I wasn’t sure?

Resistors don’t really have a voltage rating, they have a power rating. Take whatever they have, it’ll be at least 1/32nd of a watt.

Capacitor need to be rate to at least 3.3v. There’s derating on ceramics as you get anywhere near that, so maybe for got a 10v or 16v rated cap just so you get near the actual 1uF you want.