Interrupt pin and power consumption

I am using the BinarySwitchSleepSensor sketch in the “Mysensors” library. It uses a pin interrupt on pin 2/3 connected GND to switch. When the switch is not engaged I am getting 23-24uA. When the switch is engaged I am drawing 117uA.

How can I reduce these numbers even further? I know there is a powerdown/shutdown mode(Cant find how to do it) and maybe adding external resistors to lower the current draw but what does everyone else recommend?

Update^^

Okay, some more news on my front. I downgraded from the latest build of Arduino to 1.0.6 and now here are my numbers for BinarySwitchSleepSensor sketch:

Sleep mode w/o switch engaged: 2.5uA !!!
Sleep mode w switch engaged: 98-100uA.

Now I need to find a way to have the switch engaged use very little current. Any idea’s?

#include <MySensor.h>
#include <SPI.h>

#define SKETCH_NAME "Binary Sensor"
#define SKETCH_MAJOR_VER "1"
#define SKETCH_MINOR_VER "0"

#define PRIMARY_CHILD_ID 3
#define SECONDARY_CHILD_ID 4

#define PRIMARY_BUTTON_PIN 2   // Arduino Digital I/O pin for button/reed switch
#define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch

#if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN > 3)
#error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
#endif
#if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3)
#error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
#endif
#if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
#error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same
#endif
#if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID)
#error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same
#endif
 
MySensor sensor_node;

// Change to V_LIGHT if you use S_LIGHT in presentation below
MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);

void setup()  
{  
  sensor_node.begin();

  // Setup the buttons
  pinMode(PRIMARY_BUTTON_PIN, INPUT);
  pinMode(SECONDARY_BUTTON_PIN, INPUT);

  // Activate internal pull-ups
  digitalWrite(PRIMARY_BUTTON_PIN, HIGH);
  digitalWrite(SECONDARY_BUTTON_PIN, HIGH);
  
  // Send the sketch version information to the gateway and Controller
  sensor_node.sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER"."SKETCH_MINOR_VER);

  // Register binary input sensor to sensor_node (they will be created as child devices)
  // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
  // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
  sensor_node.present(PRIMARY_CHILD_ID, S_DOOR);  
  sensor_node.present(SECONDARY_CHILD_ID, S_DOOR);  
}

// Loop will iterate on changes on the BUTTON_PINs
void loop() 
{
  uint8_t value;
  static uint8_t sentValue=2;
  static uint8_t sentValue2=2;

  // Short delay to allow buttons to properly settle
  sensor_node.sleep(5);
  
  value = digitalRead(PRIMARY_BUTTON_PIN);
  
  if (value != sentValue) {
     // Value has changed from last transmission, send the updated value
     sensor_node.send(msg.set(value==HIGH ? 1 : 0));
     sentValue = value;
  }

  value = digitalRead(SECONDARY_BUTTON_PIN);
  
  if (value != sentValue2) {
     // Value has changed from last transmission, send the updated value
     sensor_node.send(msg2.set(value==HIGH ? 1 : 0));
     sentValue2 = value;
  }

  // Sleep until something happens with the sensor
  sensor_node.sleep(PRIMARY_BUTTON_PIN-2, CHANGE, SECONDARY_BUTTON_PIN-2, CHANGE, 0);
}

Edit: Updated link
Read this: Power Saving by Nick Gammon

I have read that link in the past, thanks for the reference, it really is amazing material.

My question though is not answered in that link.

If in the program there is a pull up resistor being used and waiting for an external interrupt to wake up the Arduino, in an open switch state I am getting 2uA Sleep. When I have the switch closed and pin2 connects to GND, my current is at 100uA in sleep. The program is supposed to save the state is last saw and change the interrupt every time the state changes.

What am I missing to reduce the current usage when the switch is in the closed position and the circuit is complete?

The internal pull-up is supposed to be between 20 k and 50 k according to the datasheet. Taking the most optimistic value, 50 k, let’s calculate the current for 5 V.

5 / 50000 = 0.0001 A  (100 µA)

So, working exactly as expected.

What does this switch do? Can you design it so the switch is normally open? Maybe just have a momentary contact switch, instead of permanently closed.

I am using a reed switch for a door lock. Most of the reed switches on the market are "Normally Open" before a magnet is used. If a magnet is near, the circuit is complete. So I need a way to lower the current to a livable scenario. Someone on another forum suggested I remove the "digitalWrite" and attach an external 1Mohm resistor to get it down to what I am looking for.

I am using an 8mhz Nano Pro 3.3v

I know there are few reed switches that are normally closed before a magnet is applied but the price on those are much more expensive and not practical in a security standpoint.

You should have less than 2 µA when asleep if you aren't using the watchdog, but anyway ...

For a door lock an alternative would be to sleep using the watchdog timer (that will increase consumption to about 6 µA) and then wake, say, every 1/2 second. Activate the pull-up, check the door, de-activate the pull-up and go back to sleep.

Your idea of the 1 M resistor could work. That would get the consumption down to 5 µA which is better. The trouble with a large resistor is it would be more susceptible to noise. You could compensate by taking multiple readings once you wake up, and only act if the switch is open for a second or more.

I have an nrf24l01 wireless transceiver connected to this circuit, waking up ever 500ms would kill me completely. Ultimately this would be a battery powered device on a coin cell that would last a year or more, it goes to sleep constantly, wakes up when there is a change state, publishes the state and goes back to sleep. So the "up" time is only when the door/window/mailbox/whatever opens/closes, publish the new state,then sleeps.

wouldn't a cap help clean up some noise?

I am using the MySensors library because it has mesh networking and many to 1 addressing for up to 255 end nodes. Which is great for me in building a home automation project. I have bought ZigBee Digi chips before and in sleep mode they are 1uA but have high rise/fall times and huge current draw. My other alternative was a Bluetooth Mesh made by OCR. The draw on that is 1.5uA in sleep but has extremely high programming needed. I think I am going the right route and sticking with Arduino and nrf24l01 for my applications. Maybe even design a product :wink:

The cap could well help, try it and see. And the 1 M resistor.

Nick, I just realized who you were! I want to say officially thank you for all the work you did on gammon site. I have used the reference code for your arduino power options in the past in tandem with the uCurrent hardware and its just excellent to venture into a maker hardware with such low current usage. Your write-up actually made me start down this path.

As I am a 28 year old without a college degree and no programming/electronic engineering background (teaching myself basically) Thank you for all the time you spent in testing and giving that information for free.

Thanks!

I will be at the office on Monday and will test the 1M setup and let you know what happens! :slight_smile:

Side note, if you are ever in New York City Nick, my employer would probably love to meet you if you have cool idea's to show or want to demo something in our office or pilot it out to a test building. I work for Buildinglink.com and we have 2932 buildings, mostly in NY but all over the world using our software and we are starting to develop our own sensor package that could integrate with our software. (Somehow I am actually apart of these projects haha)

So far I made a XBee sensor with IR beam that goes on gym equipment to tell people on a website if there is any free gym equipment. In our own office I am using Arduino and nrf24l01 attached to all our bathroom door locks to tell if the bathrooms are occupied or not and shows on a dashboard.

Either way thanks again!

Thanks for your kind remarks. I’ll bear that in mind if I visit New York. :slight_smile:

It sounds like you are putting Arduinos (or the Atmega chips anyway) to good use in lots of innovative ways. These “small apps” are exactly what I think they are good for. Solving small problems in a helpful way.

I know there are few reed switches that are normally closed before a magnet is applied but the price on those are much more expensive and not practical in a security standpoint.

I just remembered, I have a reed switch that does both on my garage door. Should be easy enough to find.

Example: N/O & N/C Reed Switch & Magnet. Only $AUD 6 and $AUD 4.75 in lots of 8 or more, comes with magnet, has mounting holes for screws.

You should be able to get current down to under 1 µA with the contact being normally open, depending on what the transmitter uses in power-down mode, but I don’t think it is much.

Every time I look at building something I price the parts out and see what it would cost me in mass order. I really want to make my own sellable product one day. Cheapest Reed switches are n.o.

I can remove turning pin 2 high and that can save some power.

Well I found some on eBay "10PCS Reed Glass Magnetic Switches N/O N/C SPDT 2.5X14MM" for $14.99, so that's $1.50 each.

The one I quoted further up though, is already in a nice plastic package and has mounting holes, so it is ready to screw to the door. Saves you having to make that stuff up.

There is more than the capital cost of the parts. A switch that is normally open (ie. NC in your case because of the magnet) will use less current, say 500 nA compared to 5 µA. So, other things being equal, it lasts 10 times as long. Hence you have to replace the battery less often which saves the cost of the batteries, and also the labour of doing the changeover.

True but whose cost is it? Mine or the customers? Playing with lower and lower uA it becomes kind of useless once you get close to the discharge rate of a coin cell. As long as I can get a minimum of 1 year out of a coin cell, that has become the standard minimum of IOT battery replacement.

Here is a reed switch example, only 35+ cents in mass quantity.

I know you gave examples of already made magnetic door strikes but picture a small sensor puck that is the size of a quarter(ish) and can mount to any surface, and all you need to do is put a grain of rice magnet close to it and act as a switch. You just made a small, smart, wireless sensor that you can practically on off any moving object!

Here is a reed switch example, only 35+ cents in mass quantity.

Where?

Strange, I thought I linked it, sorry :frowning:

SO an update, I connected a 10Mohm Resistor and my current draw is only 3.1-3.2uA when the switch is engaged and 2.7-2.8uA when the switch is removed. I think that is really livable numbers. So far not getting any false positives which is great. :slight_smile:

Sounds pretty good.