Author Topic: Interrupt Driven Reed Switch Circuit  (Read 4363 times)

Humancell

  • Newbie
  • *
  • Posts: 25
  • Country: us
  • The future is awesome!
Interrupt Driven Reed Switch Circuit
« on: May 31, 2016, 01:43:50 AM »
Hello,

First off ... thanks for the Moteino and all of the incredible information.  I bought several of them, and have run through several of the examples, and am learning the libraries.

I read the other thread on reed switches, and also the mail box notifier (all versions) and have a question about using reed switches in extremely low power configurations.

I really do want to create a basic "magnetic door sensor" that is able to notify me on open and close.  Now, in my installation, the door I am monitoring could be left open or closed for extended periods.  In addition, different from the other thread, I can not "poll" the status, but am hoping to interrupt and wake the processor on either edge - rising or falling - with a reed switch being connected with a pull-up or pull-down resistor.

Am I going about this wrong?  Is there a better possible solution?

If I am heading down the right path, then what is the best way to connect the reed switch for the lowest power consumption?

I also want the Moteino to wake up - no matter what - and "heartbeat" maybe once every 15 minutes or hour to simply "check in" and report the current state.  Now reading some of Felix posts, it seems that I might be able to heartbeat at an even faster rate and still be power efficient?

Thoughts?  Comments?

I wanted to for some experience before I start to hack and experiment too much ...  :-)

Scott

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 5958
  • Country: us
    • LowPowerLab
Re: Interrupt Driven Reed Switch Circuit
« Reply #1 on: June 02, 2016, 02:09:26 PM »
Different folks in the forum have different strategies for waking up.
For my motes, at the expense of more power used (still low power enough IMHO) i use the atmega328 timers to wakeup every 8s. With that largest possible sleep resolution, I count the # of cycles to end up sleeping a few minutes, hours etc, the various sample sketches illustrate that.

Regarding the reed switch, I have not used them much but there are other threads on this subject in the forum so please take a look around, I'm sure you will find something useful.
My first ever mailbox notifier used a magnetic sensor and a magnet, much like a reed. The sensor would be powered from a GPIO and polled every Xms to see if state changed, not very efficient.
Then after a long time in use I moved to PIR sensors because the mailbox door was never really left open, so I only cared when there was movement. IMHO, unless you have a specific requirement to make the difference between OPEN/CLOSE you will eventually just want to rid of the many different types of events and just be notified when that door moved.

john4444

  • Jr. Member
  • **
  • Posts: 67
  • Country: us
Re: Interrupt Driven Reed Switch Circuit
« Reply #2 on: June 04, 2016, 01:00:55 AM »
Hi Humancell
I also thought that a magnetic reed-switch would be beneficial for the mail-box notifier.
It initially seemed that it would be perfect - only apply power through the switch when the door was opened, small, non-contact, very reliable, hermetic. All good qualities for a switch.
However, there are several issues with them.
One big problem is that they are open circuit when away from the magnet.
This is the reverse of what you want when the magnet is on the mail-box door.
If you overcome that problem, you will experience short battery life if the door is left open or only partially closed.

Felix is right,
Quote
you will eventually just want to rid of the many different types of events and just be notified when that door moved.

 
John AE5HQ

Humancell

  • Newbie
  • *
  • Posts: 25
  • Country: us
  • The future is awesome!
Re: Interrupt Driven Reed Switch Circuit
« Reply #3 on: June 07, 2016, 01:43:22 AM »
Thank you for the reply.  I understand, and agree that the 8s timer can be pretty efficient if it is only going to increment a counter.  I saw someone posting about the TI WDT chip that could be used, but in my model I want the sensor to send "heartbeats" at user defined intervals, so the 8s timer works for me.

In my case, I do need the distinct notifications for "open" and "closed" and so I need to have something that gives me a way to measure the "closed" condition within a 1/4" ... hence the reed switch.   I've got it working now, but then as john4444 states I do have a issue I'll have to deal with related to power consumption through the reed switch when the door is closed ... or open.  I'm testing to see what the maximum size resistor that I can use and still keep the current minimal, or as small as possible.

I've got another possible solution I want to test based on a 9-axis accelerometer/gyroscope/magnetometer.  I want to know if the resolution would be good enough to detect the door vibration and slight magnetic (compass) orientation change.  :-)

Also ... per the Mailbox Notifier code here in Github:  https://github.com/LowPowerLab/RFM69/blob/master/Examples/MailboxNotifier/MailboxNotifier4_sender.ino

I have a few questions:

Line #60 defines the pin as "hardware interrupt 1 (D3)"
https://github.com/LowPowerLab/RFM69/blob/master/Examples/MailboxNotifier/MailboxNotifier4_sender.ino#L60

But then line #87 seems to set D1 to INPUT?
https://github.com/LowPowerLab/RFM69/blob/master/Examples/MailboxNotifier/MailboxNotifier4_sender.ino#L87

Line #88 then attaches the Interrupt properly ...
https://github.com/LowPowerLab/RFM69/blob/master/Examples/MailboxNotifier/MailboxNotifier4_sender.ino#L88

I found this issue as I was trying to do a digitalRead of MOTIONPIN to get the reed switch value.  I ended up realizing that the pin I was reading was not correct.  It seems that Arduino now recommends using:  attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

Changing the pin to then be "3" worked!

The other question that I had was why the SPI.h was included in this sketch?  Is it being used for something?

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 5958
  • Country: us
    • LowPowerLab
Re: Interrupt Driven Reed Switch Circuit
« Reply #4 on: June 07, 2016, 08:26:28 AM »
That should be D3 set as INPUT so it must be a bug, although all pins are INPUTs when powering up so it still works in that sketch.
SPI.h is needed for the RFM69 lib.

TomWS

  • Hero Member
  • *****
  • Posts: 1892
Re: Interrupt Driven Reed Switch Circuit
« Reply #5 on: June 07, 2016, 01:37:46 PM »
Thank you for the reply.  I understand, and agree that the 8s timer can be pretty efficient if it is only going to increment a counter.  I saw someone posting about the TI WDT chip that could be used, but in my model I want the sensor to send "heartbeats" at user defined intervals, so the 8s timer works for me.

In my case, I do need the distinct notifications for "open" and "closed" and so I need to have something that gives me a way to measure the "closed" condition within a 1/4" ... hence the reed switch.   I've got it working now, but then as john4444 states I do have a issue I'll have to deal with related to power consumption through the reed switch when the door is closed ... or open.  I'm testing to see what the maximum size resistor that I can use and still keep the current minimal, or as small as possible.

If you can periodically check the state, using WDT rather than interrupts, then you can check with virtually no power.  Tie the reed switch between two GPIOs and then drive one pin to ground while the other pin is set to INPUT_PULLUP.  If the input pin follows the output then the switch is closed, if not then it's open.  Takes about 250uA for about 10uS...  When done reading, drive the output high so there is no current through the switch.
Tom

OSBSS

  • Newbie
  • *
  • Posts: 29
  • Country: us
    • OSBSS - Open Source Building Science Sensors
Re: Interrupt Driven Reed Switch Circuit
« Reply #6 on: June 07, 2016, 06:36:39 PM »
I'm testing to see what the maximum size resistor that I can use and still keep the current minimal, or as small as possible.

I've tested reed switches with a Moteino and found out that if you use a 1MΩ pull-down resistor on the pin with a 0.1uF capacitor in parallel to it (and the normally-open reed switch connected to VCC of course), you can bring down the current draw of the closed reed switch to about ~5uA. Enable internal pull-up on the interrupt pin. The cap allows for hardware debouncing.  I had a node running for several months and the battery was still good. You might be able to use a 10MΩ resistor and potentially get down to under 1uA, but I haven't tested that.

I used it on a door with a self-closing hinge, so the open and close events were always guaranteed. It's not the best solution for monitoring a mailbox door state as mentioned by others, but for something certain, like a door or a window, it works fairly well.

joelucid

  • Hero Member
  • *****
  • Posts: 869
Re: Interrupt Driven Reed Switch Circuit
« Reply #7 on: June 08, 2016, 04:00:32 AM »
Why not use a reed switch that has both a normally close and normally open pin, like this one:

http://www.littelfuse.com/~/media/electronics/datasheets/reed_switches/littelfuse_reed_switches_mdsm_dt_datasheet.pdf.pdf

Then you power the pin that's not closed in the current state to output/low, set a pull-up on the common lead and set the IRQ for the falling edge. In the IRQ you set the pin connected to the closed loop to input and now the other, now open pin, to output/low.

That should cover both cases and only use the use 250nA sleep currents of 328p / radio.

If you want to have regular heartbeats the radio can be used as timer via the PLL-Lock irq. Which uses around 1.3uA in total. However the types of application where you can wake without timer are so rare that in this case I would probably go for it.

If you want to go all the way, power the Moteino via the common lead and use a latching relay to connect the battery to the currently open connector. If it closes the Moteino starts up, sends an update and switches the relay which cuts the power to the Moteino. There are relays that can be switched using two gpio pins and that would create a really low power solution - back of the envelope calculation around 5nA - Or what I'd call a lifetime Mote.

Joe

joelucid

  • Hero Member
  • *****
  • Posts: 869
Re: Interrupt Driven Reed Switch Circuit
« Reply #8 on: June 08, 2016, 04:24:59 AM »
Quote
Then you power the pin that's not closed in the current state to output/low, set a pull-up on the common lead and set the IRQ for the falling edge. In the IRQ you set the pin connected to the closed loop to input and now the other, now open pin, to output/low.

Or and I guess much easier: just connect the NO pin to 3.3V, the NC pin to gnd and attach a CHANGED irq to the common lead.

Humancell

  • Newbie
  • *
  • Posts: 25
  • Country: us
  • The future is awesome!
Re: Interrupt Driven Reed Switch Circuit
« Reply #9 on: June 09, 2016, 02:20:49 PM »
Thanks ... you guys are all great!

Tom ... I started to look at this solution, and was wondering what the "acceptable" time is to wake and poll.  I started to think about designing a PCB that I can stuff in either configuration ... polling with your solution and also interrupt with the pull-up.  Then I could build a few of each and test in both configurations.

OSBSS ... thanks for the numbers and actual experience.  I was about to start with a 1MΩ and see how it went.  I was worried about what I've read on larger pull up values, and possibly not providing enough current to trip the level.  I know that ~5uA is pretty good.

joelucid ... great find on the DT reed switch ... I hadn't seen those!  And then the thought on just tying it to the interrupt input with Vcc and GND ... I'm testing today with the CHANGE interrupt.  Now I have a third option I'm trying to consider.  And with the input pin directly tied to Vcc or GND is there any/much current draw when the Mote sleeps?

Now I'm looking at a way to create a PCB that would accommodate all of these possible implementations using stuff options so that I could do some real world testing ...  :-)


TomWS

  • Hero Member
  • *****
  • Posts: 1892
Re: Interrupt Driven Reed Switch Circuit
« Reply #10 on: June 09, 2016, 02:42:49 PM »
joelucid ... great find on the DT reed switch ... I hadn't seen those!  And then the thought on just tying it to the interrupt input with Vcc and GND ... I'm testing today with the CHANGE interrupt.  Now I have a third option I'm trying to consider.  And with the input pin directly tied to Vcc or GND is there any/much current draw when the Mote sleeps?
Joe's solution is the easiest to implement from a SW/power consumption perspective.  I'd recommend a medium valued capacitor, eg 0.001uF, connected between the common and ground.  This will eliminate any switch bounce from causing multiple interrupts as well as provide a bit of contact cleaning energy on each switchover.

Tom