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. Announcements
  3. 💬 Motion Sensor

💬 Motion Sensor

Scheduled Pinned Locked Moved Announcements
63 Posts 30 Posters 19.6k Views 31 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.
  • gohanG Offline
    gohanG Offline
    gohan
    Mod
    wrote on last edited by
    #33

    Set a timer in the code, that's shouldn't be very hard to do.

    1 Reply Last reply
    0
    • Cliff KarlssonC Offline
      Cliff KarlssonC Offline
      Cliff Karlsson
      wrote on last edited by
      #34

      I have never used timers but I looked at the blink without delay sketch from the arduino website. Do I replace the if -statement with a while loop?

      const int ledPin =  LED_BUILTIN;// the number of the LED pin
      
      // Variables will change :
      int ledState = LOW;             // ledState used to set the LED
      
      // Generally, you should use "unsigned long" for variables that hold time
      // The value will quickly become too large for an int to store
      unsigned long previousMillis = 0;        // will store last time LED was updated
      
      // constants won't change :
      const long interval = 1000;           // interval at which to blink (milliseconds)
      
      void setup() {
        // set the digital pin as output:
        pinMode(ledPin, OUTPUT);
      }
      
      void loop() {
        // here is where you'd put code that needs to be running all the time.
      
        // check to see if it's time to blink the LED; that is, if the
        // difference between the current time and last time you blinked
        // the LED is bigger than the interval at which you want to
        // blink the LED.
        unsigned long currentMillis = millis();
      
        if (currentMillis - previousMillis >= interval) {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
      
          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW) {
            ledState = HIGH;
          } else {
            ledState = LOW;
          }
      
          // set the LED with the ledState of the variable:
          digitalWrite(ledPin, ledState);
        }
      }
      
      1 Reply Last reply
      0
      • gohanG Offline
        gohanG Offline
        gohan
        Mod
        wrote on last edited by
        #35

        Just leave it as it is, just put a condition that will trigger the if statement only once the pir sensor got triggered

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

          I would actually not do this in a node but in the controller (at least if I get what you are trying to do right). I have done that with some scripting in domoticz. I have lights that that are switched on for 5 minutes when motion is detected and shut down 5 minutes after the last motion stopped. Motion sensor and rgbw controller are different mysensor nodes. Much more versatile then hardcoding this in you node's code.

          ben999B 1 Reply Last reply
          0
          • L LastSamurai

            I would actually not do this in a node but in the controller (at least if I get what you are trying to do right). I have done that with some scripting in domoticz. I have lights that that are switched on for 5 minutes when motion is detected and shut down 5 minutes after the last motion stopped. Motion sensor and rgbw controller are different mysensor nodes. Much more versatile then hardcoding this in you node's code.

            ben999B Offline
            ben999B Offline
            ben999
            wrote on last edited by
            #37

            @LastSamurai
            Would you do that through "rules" ?

            gohanG 1 Reply Last reply
            0
            • ben999B ben999

              @LastSamurai
              Would you do that through "rules" ?

              gohanG Offline
              gohanG Offline
              gohan
              Mod
              wrote on last edited by
              #38

              @ben999 yes.

              @LastSamurai I know that hardcoding is less flexible, but at least it can work even if controller is down or disconnected, of course it depends how critical is the actuator.

              1 Reply Last reply
              0
              • maghacM Offline
                maghacM Offline
                maghac
                wrote on last edited by
                #39

                So, I notice that the HC-SR501 has a 7133-1 voltage regulator on-board which I guess means that the logic is actually driven with 3.3V and not 5V.

                Is it possible to bypass the regulator and power the sensor directly with the 3.3V I am using to power my nano with?

                maghacM 1 Reply Last reply
                0
                • maghacM maghac

                  So, I notice that the HC-SR501 has a 7133-1 voltage regulator on-board which I guess means that the logic is actually driven with 3.3V and not 5V.

                  Is it possible to bypass the regulator and power the sensor directly with the 3.3V I am using to power my nano with?

                  maghacM Offline
                  maghacM Offline
                  maghac
                  wrote on last edited by
                  #40

                  @maghac Aha, guess I should read old posts first :)

                  1 Reply Last reply
                  1
                  • gohanG Offline
                    gohanG Offline
                    gohan
                    Mod
                    wrote on last edited by
                    #41

                    Just make sure the power source is stable otherwise you will end up with random triggers from pir sensor

                    ben999B 1 Reply Last reply
                    1
                    • gohanG gohan

                      Just make sure the power source is stable otherwise you will end up with random triggers from pir sensor

                      ben999B Offline
                      ben999B Offline
                      ben999
                      wrote on last edited by
                      #42

                      @gohan @maghac

                      From my little experience : when running from 2 AA batteries with sketch as it is I got random triggers continuousely

                      Adding a delay() before entering sleep() sorted things out (something like 3 sec is necessary to get power stable again)

                      Not sure it's the smartest way of doing it, but it works...

                      Any comment on the security side of it? Could my node miss some action during these 3 sec ? Or the interrupt pin would still listen to the PIR sensor pin ?

                      1 Reply Last reply
                      0
                      • gohanG Offline
                        gohanG Offline
                        gohan
                        Mod
                        wrote on last edited by
                        #43

                        I think it will miss the pir trigger

                        ben999B 1 Reply Last reply
                        0
                        • gohanG gohan

                          I think it will miss the pir trigger

                          ben999B Offline
                          ben999B Offline
                          ben999
                          wrote on last edited by
                          #44

                          @gohan

                          Thank you for your reply

                          Could you please explain a bit further ?

                          Interrupt pin would still read the trigger during delay()?
                          Data would be lost as the sketch is frozen during delay() ?
                          Or data is saved in some buffer, ready to be used as soon as sketch comes back to life ?

                          My view is that it shouldn't matter too much if i miss a trigger after a trigger : the first trigger will wake my node and transmit to gateway. So job done. Alarm goes off.

                          On the other hand, I guess a millis() would be neater ?

                          Thanks a lot for your input

                          1 Reply Last reply
                          0
                          • gohanG Offline
                            gohanG Offline
                            gohan
                            Mod
                            wrote on last edited by
                            #45

                            I am saying that during delay no code is executed and interrupt only attaches to pin when executing the sleep function, so if motion if triggered during the delay it will not be detected by interrupt; maybe it could detect the CHANGE when pir goes from high to low but it will then report "no motion" and it will not trigger alarm if it set to go off on "motion" only.

                            ben999B 1 Reply Last reply
                            0
                            • gohanG gohan

                              I am saying that during delay no code is executed and interrupt only attaches to pin when executing the sleep function, so if motion if triggered during the delay it will not be detected by interrupt; maybe it could detect the CHANGE when pir goes from high to low but it will then report "no motion" and it will not trigger alarm if it set to go off on "motion" only.

                              ben999B Offline
                              ben999B Offline
                              ben999
                              wrote on last edited by
                              #46

                              @gohan

                              Thanks a lot

                              Got it now

                              delay() to be avoided then !

                              I will have a look at millis() instead

                              1 Reply Last reply
                              0
                              • gohanG Offline
                                gohanG Offline
                                gohan
                                Mod
                                wrote on last edited by
                                #47

                                Delay is not bad, depends how you use it. Maybe you could use an if statement to check pin state is low before entering sleep

                                1 Reply Last reply
                                1
                                • skywatchS Offline
                                  skywatchS Offline
                                  skywatch
                                  wrote on last edited by
                                  #48

                                  Millis won't work with the SLEEP funtion either. REcently added to the API....

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

                                    Hi, I´ve read this above

                                    "however this sensor requires 5 VDC so you will need to use a step-up regulator if you are running the 3.3V version of the Arduino Pro Mini."

                                    So I am using 3 V, could you propose a fitting step up regulator so I can use this PIR (best from ebay)? Thank you!

                                    still learning...

                                    maghacM 1 Reply Last reply
                                    0
                                    • siodS siod

                                      Hi, I´ve read this above

                                      "however this sensor requires 5 VDC so you will need to use a step-up regulator if you are running the 3.3V version of the Arduino Pro Mini."

                                      So I am using 3 V, could you propose a fitting step up regulator so I can use this PIR (best from ebay)? Thank you!

                                      maghacM Offline
                                      maghacM Offline
                                      maghac
                                      wrote on last edited by
                                      #50

                                      @siod said in 💬 Motion Sensor:

                                      a

                                      You have a HC-SR501 sensor? I had that same question before and then I realized that the HC-SR501 actually has a 3.3 V regulator and if you bypass it you can run it on 3.3V.

                                      1 Reply Last reply
                                      0
                                      • Cliff KarlssonC Cliff Karlsson

                                        If I wanted to add two PIRs to one sensor-node would this sketch work?

                                        /**
                                         * 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.
                                         *
                                         *******************************
                                         *
                                         * REVISION HISTORY
                                         * Version 1.0 - Henrik Ekblad
                                         *
                                         * DESCRIPTION
                                         * Motion Sensor example using HC-SR501
                                         * http://www.mysensors.org/build/motion
                                         *
                                         */
                                        
                                        // Enable debug prints
                                        // #define MY_DEBUG
                                        
                                        // Enable and select radio type attached
                                        #define MY_RADIO_NRF24
                                        //#define MY_RADIO_RFM69
                                        
                                        #include <MySensors.h>
                                        
                                        unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
                                        #define DIGITAL_INPUT_SENSOR_1 2
                                        #define DIGITAL_INPUT_SENSOR_2 3  // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
                                        #define CHILD_ID_1 1   // Id of the sensor child
                                        #define CHILD_ID_2 2   // Id of the sensor child
                                        
                                        // Initialize motion message
                                        MyMessage msg(CHILD_ID_1, V_TRIPPED);
                                        
                                        
                                        void setup()
                                        {
                                            pinMode(DIGITAL_INPUT_SENSOR_1, INPUT);      // sets the motion sensor digital pin as input
                                            pinMode(DIGITAL_INPUT_SENSOR_2, INPUT);      // sets the motion sensor digital pin as input
                                        }
                                        
                                        void presentation()
                                        {
                                            // Send the sketch version information to the gateway and Controller
                                            sendSketchInfo("Motion Sensor_Dual", "1.0");
                                        
                                            // Register all sensors to gw (they will be created as child devices)
                                            present(CHILD_ID_1, S_MOTION);
                                            present(CHILD_ID_2, S_MOTION);
                                        }
                                        
                                        void loop()
                                        {
                                            // Read digital motion value
                                            bool tripped_1 = digitalRead(DIGITAL_INPUT_SENSOR_1) == HIGH;
                                            bool tripped_2 = digitalRead(DIGITAL_INPUT_SENSOR_2) == HIGH;
                                        
                                            Serial.println(tripped_1 || tripped_2);
                                            if(tripped_1){
                                            send(msg.set(tripped_1?"1":"0"));  // Send tripped value to gw
                                            }
                                            if(tripped_2){
                                            send(msg.set(tripped_2?"1":"0"));  // Send tripped value to gw
                                            }
                                            // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                                            sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR_1 || DIGITAL_INPUT_SENSOR_1), CHANGE, SLEEP_TIME);
                                        }
                                        

                                        I know PIN 2 is supposed to be connected to the radio but I read somewhere that it is not used.

                                        sellierS Offline
                                        sellierS Offline
                                        sellier
                                        wrote on last edited by gohan
                                        #51

                                        @Cliff-Karlsson
                                        For dual Sensors

                                        one error is present on the line sleep:
                                        it is not : sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR_1 || DIGITAL_INPUT_SENSOR_1), CHANGE, SLEEP_TIME);

                                        but : sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR_1 )|| digitalPinToInterrupt(DIGITAL_INPUT_SENSOR_2), CHANGE, SLEEP_TIME);

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          MCF
                                          wrote on last edited by
                                          #52

                                          hi,
                                          i want to ad a motion sensor to my project (3 dimmers on a arduino uno using nrf24).
                                          My first problem is that the 2 and 3 pins are not available (nrf24 and pwm dimmer), so i will take the pin 1 for testing
                                          second is the sleeping time during i can't send any light message to my dimmers...

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


                                          8

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.0k

                                          Posts


                                          Copyright 2019 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