2

While working for the automation side of my new house, I'm currently in the process of installing several reed switches on top of external doors and windows.

Even if this could be used as a "security" infrastructure, I'm more interested, at the moment, at the "automation" side (checking the status of door and windows without the need to physically watch them) and as such, I'm going to extend the arduino-based infrastructure I've already planned and prototyped [more details here].

This is the reed-switch I've installed (1€ coin added for size reference):

reed switch photo

and this is the related circuits:

reed switch circuits

As you can see, it comes with two circuits: one related to the "switch" funcionality; another one as a safety-measure to catch some "cuts" along the wires.

To check its status, currently, I've configured:

  • "contact circuit":

    • one end to an Arduino GROUND_PIN;
    • the other end to Digital PIN 52 (REED1_SW_Pin);
  • "tamper circuit":

    • one end to an Arduino GROUND_PIN (same GND pin as above);
    • the other end to Digital PIN 50 (REED1_AT_Pin);
  • Arduino setup:

    • pinMode(REED1_SW_Pin, INPUT_PULLUP)
    • pinMode(REED1_AT_Pin, INPUT_PULLUP)
  • Arduino code:

    • the main code-block handling REED events is reported below

Even tough everything is working fine, I wonder if there are better approaches, expecially inchecking the status of door and windows without the need to physically watch them terms of POWER-CONSUMPTION. So, here are my questions:

  1. TAMPER-Circuit: due to its nature, it will always be a "closed" circuit, with current flowing inside, from REED1_AT_Pin to GND. Currently I'm checking its state every 1 second (#define REED_AT_CHECKDELAY 1000) but a frequency of once every 5 seconds, or even 10 seconds would be acceptable (for me). So I would like to have the current flowing ONLY during the "checking-timeframe". Something like:
    • every 5 seconds...
    • power-up something, getting the current flowing along the circuit;
    • check if the current flows;
    • if yes => OK
    • if no => set/keep the ALARM state
    • power-down the "something"

Is it feasible? If yes, how? Also: is it worth the effort?

  1. REED-switch circuit: same as above. Polling approach is still acceptable (for me) but frequency need to be relatively high (5 seconds max), in order to keep of sort of "near-real-time feedback" when opening/closing windows. Best would be 2 secs polling-interval. With such a timing, does the polling-approach make sense?

  2. from an "electronic" point of view, what is the "to-be-preferred-scenario" for the Arduino board:

    • having a small, fixed, steady current, flowing out from a PIN, for YEARS on a 24/7/365 basis or...
    • having a continuosly transition from LOW->HIGH->LOW for a Digital PIN, say once every 5 seconds, again, for YEARS?

Every comment, sidenotes, hints, critics, whatever... is REALLY welcomed/appreciated.


Arduino code-block handling REED events

BTW: the full-code, where the REED switch is handled togheter with pushbuttons and relays, it's available on GitHub

    [...]
    if (reed_sw1_state != reed_sw1_previous_state) {
        // a change occurred. Are we in a "boucing" window?
        if (millis() - last_reed_sw1_debounce > REED_DEBOUNCE) {
          // no. So it's a real change
          // Let's handle the event...
          Serial.print(millis());
          Serial.print("[REED] - SW1 state CHANGED from [");
          Serial.print(reed_sw1_previous_state);
          Serial.print("] to ["); 
          Serial.print(reed_sw1_state);
          Serial.println("]");
          reed_sw1_previous_state = reed_sw1_state;
          last_reed_sw1_debounce = millis();
        } else {
          // yes. Let's ignore it
        }   
      }

      // Let's check REED Switches anti-tamper state
      if (millis()-reed_at1_lastcheck > REED_AT_CHECKDELAY) {
         reed_at1_state = ! digitalRead(REED1_AT_Pin);
         if (reed_at1_state == HIGH ) {
            Serial.print(millis());
            Serial.print("[REED] - AT1 state OK [");
            Serial.print(reed_at1_state);
            Serial.println("]");
         } else {
            Serial.print(millis());
            Serial.print("[REED] - AT1 state ALARM - OnGoing TAMPER!!! [");
            Serial.print(reed_at1_state);
            Serial.println("]");          
         }
         reed_at1_lastcheck = millis();
      }
      [...]
3

So I would like to have the current flowing ONLY during the "checking-timeframe".

Turn off the pull-up until just before checking. Allow a few milliseconds for the pull-up to raise the voltage.

With such a timing, does the polling-approach make sense?

Certainly, if you are trying to save battery power.


Bear in mind the voltage drops over long cable runs (and also possibly induced spikes). You might want a more powerful pull-up (eg. 10k) rather than the inbuilt 50k. You can still organize that by having an output pin turn on the voltage to the pull-up. Or, have the pull-up wired in permanently but instead of running the other side to ground, set the "ground" wire to OUTPUT and write LOW to it - and then set it to INPUT when you want it to be inactive.

See my post about Power saving - you could use a watchdog timer to wake the device at (say) every 4 seconds, to check things.

You may want to add in input-circuit protection (eg. a couple of diodes) to dampen down any incoming spikes. Even something simple like this:

Input protection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.