Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. gw.sleep on battery powered magnet door switch

gw.sleep on battery powered magnet door switch

Scheduled Pinned Locked Moved My Project
42 Posts 13 Posters 13.2k Views 16 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • slingS Offline
    slingS Offline
    sling
    wrote on last edited by
    #33

    @siod

    I'm assuming your batteries are empty when you say fail ?
    I built 5 of these last week for my dads place.
    I set internal pullups to LOW and soldered a 3,3MOhm resistor between VCC and Pin2.
    I'm seeing around 30µA when sleeping on two fresh AA batteries.
    With internal pullups that would be around 1ma and that would indeed drain the batteries quite fast.

    1 Reply Last reply
    0
    • siodS Offline
      siodS Offline
      siod
      wrote on last edited by
      #34

      no Sir, batteries are still at around 90% charged.

      still learning...

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nicklas Starkel
        wrote on last edited by
        #35

        @siod , I had the same problem.
        I actually ordered a original NRF 24 mini and this seemed to solve some of my problems.
        i think another problem I'm having is that my batteries has a huge self discharge (old 3.7v 18650)

        On a side note.. I'm with @sling .
        Loose the pullups and set them LOW´.
        I have a 1MOhm resistor between VCC and PIN 2 and PIN 3 (also 2 switches).
        Using the pullup HIGH draws around 130uA versus LOW it draws between 7 to 10uA!

        Nca78N 1 Reply Last reply
        2
        • siodS Offline
          siodS Offline
          siod
          wrote on last edited by
          #36

          No gentlemen, I don´t think it is a battery problem, maybe a radio problem, but not a battery problem.

          But my sketch seems to be ok?

          still learning...

          1 Reply Last reply
          0
          • N Nicklas Starkel

            @siod , I had the same problem.
            I actually ordered a original NRF 24 mini and this seemed to solve some of my problems.
            i think another problem I'm having is that my batteries has a huge self discharge (old 3.7v 18650)

            On a side note.. I'm with @sling .
            Loose the pullups and set them LOW´.
            I have a 1MOhm resistor between VCC and PIN 2 and PIN 3 (also 2 switches).
            Using the pullup HIGH draws around 130uA versus LOW it draws between 7 to 10uA!

            Nca78N Offline
            Nca78N Offline
            Nca78
            Hardware Contributor
            wrote on last edited by
            #37

            @Nicklas-Starkel said:

            @siod , I had the same problem.
            I actually ordered a original NRF 24 mini and this seemed to solve some of my problems.
            i think another problem I'm having is that my batteries has a huge self discharge (old 3.7v 18650)

            On a side note.. I'm with @sling .
            Loose the pullups and set them LOW´.
            I have a 1MOhm resistor between VCC and PIN 2 and PIN 3 (also 2 switches).
            Using the pullup HIGH draws around 130uA versus LOW it draws between 7 to 10uA!

            That's the best solution with a normal reed switch.
            But the best way is to use a "double" reed switch, with both normally opened and normally closed contacts. I use that connected between GND and pin 2 & 3 and set only the unconnected pin to high (so, with pullup). Current will flow only a very short time through the pullup while the node is waking up from sleep, then first thing I do is set connected input low. So nearly 100% of the time input is not connected and current through the reed switch is 0.
            I've put a test node on my entrance door, it's been running for over 3 months on a CR2032 cell and has lost only 0.02V meaning it will last at least 2 years, maybe 3.

            /**
             * The MySensors Arduino library handles the wireless radio link and protocol
             * between your home built sensors/actuators and HA controller of choice.
             * The sensors forms a self healing radio network with optional repeaters. Each
             * repeater and gateway builds a routing tables in EEPROM which keeps track of the
             * network topology allowing messages to be routed to nodes.
             *
             * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
             * Copyright (C) 2013-2015 Sensnology AB
             * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
             *
             * Documentation: http://www.mysensors.org
             * Support Forum: http://forum.mysensors.org
             *
             * This program is free software; you can redistribute it and/or
             * modify it under the terms of the GNU General Public License
             * version 2 as published by the Free Software Foundation.
             *
             *******************************
             *
             * DESCRIPTION
             *
             * Interrupt driven binary switch example with dual interrupts
             * Author: Patrick 'Anticimex' Fallberg
             * Connect one button or door/window reed switch between 
             * digitial I/O pin 3 (BUTTON_PIN below) and GND and the other
             * one in similar fashion on digital I/O pin 2.
             * This example is designed to fit Arduino Nano/Pro Mini
             * 
             * NCA78: Updated to work with one dual normally open/normally closed reed switch on both interrupt pins.
            
             */
            
            
            // Enable debug prints
            //#define MY_DEBUG 
            
            // Enable and select radio type attached
            #define MY_RADIO_NRF24
            
            #include <SPI.h>
            #include <MyConfig.h>
            #include <MySensors.h>
            #include <SystemStatus.h>
            #define SKETCH_NAME "NCA Door Sensor"
            #define SKETCH_MAJOR_VER "0"
            #define SKETCH_MINOR_VER "7"
            
            #define PRIMARY_CHILD_ID 3
            
            #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
            
            #define MY_PARENT_NODE_ID 0
             
            
            // 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);
            
            
            // Parameters for VCC measurement
            const int VccMin        = 2000;  // Minimum expected Vcc level, in Volts. At 2V the cell should be dead
            const int VccMax        = 2900;  // Maximum expected Vcc level, in Volts.
            SystemStatus vcc();
            int LastBatteryPercent = 200; // so we are sure to send the battery level at first check
            bool isEven = false; // to check+send battery level only for each open+close cycles
            
            bool revertValue = false;  // to change this put a jumper on D4/D7 and it is tested at startup
            
            
            // This is the activated pin, on which the interrupt is set
            byte connectedPin = PRIMARY_BUTTON_PIN;
            byte connectedPinAtLastSending = 0; // Initialized at 0 so we will always send the first time
            
            void setup()  
            {  
              // First thing to do: change clock prescaling to 8 to change from 8MHz to 1MHz
              //  of course not necessary if you already have updated fuses and bootloader...
              #ifndef MY_DEBUG           // only if we are not in debug mode, so we can keep the fast baudrate in debug
                clock_prescale_set (clock_div_8);   
              #endif
            
              
              
            }
            
            void presentation() {
              sleep(250); // sleep to let capacitor recharge a bit
              // Send the sketch version information to the gateway and Controller
              sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
              sleep(250); // sleep to let capacitor recharge a bit
              // Register binary input sensor to sensor_node (they will be created as child devices)
              present(PRIMARY_CHILD_ID, S_DOOR);
              sleep(250); // sleep to let capacitor recharge a bit
            }
            
            // Loop will iterate on changes on the BUTTON_PINs
            void loop() 
            {
              // Short delay to allow buttons to properly settle
              sleep(5);
            
              deActivatePin(PRIMARY_BUTTON_PIN);
              deActivatePin(SECONDARY_BUTTON_PIN);
            
              // Check if the previously connected pin is now connected. We do that because it's the most likely to be unconnected now
              //  so it's the best way not to lose any current
              byte newConnectedPin = GetNonConnectedPin();
              if (checkPinIsConnected(connectedPin)) {
                // If pin is still connected we set back the value to connected pin
                newConnectedPin = connectedPin;
                #ifdef MY_DEBUG
                  Serial.println("Connected pin is connected !");
                #endif
                
              }
              connectedPin = newConnectedPin;
              // If connected pin is different that the one during the last sending of status, we send again
              #ifdef MY_DEBUG
                Serial.print("Connected pin = ");
                Serial.println(connectedPin);
                Serial.print("New Connected pin = ");
                Serial.println(newConnectedPin);
              #endif
              
              if (connectedPin != connectedPinAtLastSending) {
                 // Value has changed from last transmission, send the updated value
                 send(msg.set(connectedPin==PRIMARY_BUTTON_PIN ? (revertValue ? 0 : 1) : (revertValue ? 1 : 0)));
                 connectedPinAtLastSending = connectedPin;
                 isEven = !isEven;
              }
              
              if (isEven) {  // send only every two changes for a full open + close cycle
                int currentBatteryPercent = SystemStatus().getVCCPercent(VccMin, VccMax);
                if (currentBatteryPercent != LastBatteryPercent) {
                    sleep(100); // sleep 100ms (+65ms wake up time) to let capacitor recharge a bit
                    LastBatteryPercent = currentBatteryPercent;
                    sendBatteryLevel(currentBatteryPercent);
                }
              }
            
              #ifdef MY_DEBUG
                Serial.print("Preparing to sleep, pin ");
                Serial.println(GetNonConnectedPin());
                wait(50);
              #endif
            
              // Activate the non connected pin before setting up interrupt
              activatePin(GetNonConnectedPin());    
              // Sleep until something happens with the door sensor
              sleep(GetNonConnectedPin()-2, CHANGE);
            } 
            
            void activatePin(byte pin) {
              // Set pin as input
              pinMode(pin, INPUT);
              // Activate internal pull up
              digitalWrite(pin, HIGH);
            }
            
            void deActivatePin(byte pin) {
              // Set back pin as output, low
              pinMode(pin, OUTPUT);
              digitalWrite(pin, LOW);
            }
            
            // Will check if pin is grounded (returns true) or not
            boolean checkPinIsConnected(byte pin) {
              activatePin(pin);
            
              // Read value
              byte valPin = digitalRead(pin);
              deActivatePin(pin);
            
              #ifdef MY_DEBUG
                Serial.print("checkPinIsConnected pin = ");
                Serial.print(pin);
                Serial.print(", value = ");
                Serial.println(valPin);
              #endif
              
              return valPin != HIGH;
            }
            
            // Returns the pin that is not connected
            byte GetNonConnectedPin() {
              return (connectedPin == PRIMARY_BUTTON_PIN) ? SECONDARY_BUTTON_PIN : PRIMARY_BUTTON_PIN;
            }
            
            

            https://www.aliexpress.com/item/20pcs-lot-3-pin-Reed-Switch-2-5X14MM-GLASS-Green-3-pin-Normally-Open-and-Normally/32507762756.html?spm=2114.13010608.0.0.4LJb9I

            1 Reply Last reply
            2
            • siodS Offline
              siodS Offline
              siod
              wrote on last edited by siod
              #38

              Ok guys you convinced me, I will set them LOW instead of HIGH.

              So now the reed switches are connected: GND | Reed Switch | PIN 2
              Doing it your way I must connect them: VCC | 1M Ohm Resistor | Reed Switch | PIN2

              Correct?

              I just did it the way described here: https://www.mysensors.org/build/binary
              There is nothing abaout any resistor...

              But will this solve my problem? I am not sure. I had the feeling that the sensors won´t wake up from sleep or so...

              still learning...

              1 Reply Last reply
              0
              • L Offline
                L Offline
                LastSamurai
                Hardware Contributor
                wrote on last edited by LastSamurai
                #39

                I would be interested too as I have some battery problems with my reed switches too. I guess its VCC | Reed Switch | GND and Reed Switch |1M Ohm Resistor | inputpin, right?

                N 1 Reply Last reply
                0
                • L LastSamurai

                  I would be interested too as I have some battery problems with my reed switches too. I guess its VCC | Reed Switch | GND and Reed Switch |1M Ohm Resistor | inputpin, right?

                  N Offline
                  N Offline
                  Nicklas Starkel
                  wrote on last edited by
                  #40

                  @siod and @LastSamurai
                  Connect the REED Switch with one leg to GND and the other leg to PIN2.
                  And then you put a 1MOhm resistor between VCC and PIN2.

                  1 Reply Last reply
                  0
                  • dbemowskD Offline
                    dbemowskD Offline
                    dbemowsk
                    wrote on last edited by
                    #41

                    @siod
                    @Nicklas-Starkel said:

                    put a 1MOhm resistor between VCC and PIN2.

                    This is used as a pull-up resistor to keep the signal to PIN2 active HIGH until the reed switch is closed.

                    I have never tested and don't know how much of a difference it would make, but you can do the reverse of that too and connect it like this:
                    GND -- 1MOhm -- PIN2 -- Reed Switch -- VCC

                    With this setup PIN2 would stay active LOW and in your code you would check to see if it goes HIGH for the switch contact. My thought is that doing it with the resistor to VCC from PIN2, which would be active HIGH, would use a slight bit more power than keeping it active LOW with the resistor from PIN2 to GND . As I said, I have never tested this, so I may be wrong.

                    Vera Plus running UI7 with MySensors, Sonoffs and 1-Wire devices
                    Visit my website for more Bits, Bytes and Ramblings from me: http://dan.bemowski.info/

                    1 Reply Last reply
                    0
                    • slingS Offline
                      slingS Offline
                      sling
                      wrote on last edited by
                      #42

                      @siod

                      new observation: I have two temp nodes with red Htu21d in them. One keeps locking up randomly. Have to go outside and reset the arduino. I'll try another sensor today and see if it fixes things. Have completely rebuild the node twice now with new parts except the sensor.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      22

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.1k

                      Posts


                      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular