💬 Motion Sensor



  • @Graham Thank you for your reply

    SR501 seem to work fine on two fronts:

    • the arduino IDE serial monitor shows the SR501 going ON then OFF after 5s (as per dial)
    • same serial monitor shows that sensor gets motion when it occurs...

    I'll have a go with another node just to be sure

    Thanks again for your help (both of you :) )



  • 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.



  • @ben999 Did you get your sensor sorted? I have the same issue with the sensor reporting tripped after going out of sleep, same goes for wait. Putting the exact same logic in a sketch without MYS and I have no issues... going crazy soon



  • Does anyone know why my sensor only reacts when I move my hand just in front of it. From a distance it does not work. Has tested with different HC-SR501 PIR and adjusted also and various radio but all gave the same result.

    /**
     * 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 <SPI.h>
    #include <MySensors.h>
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID 1   // Id of the sensor child
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Motion Sensor", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_MOTION);
    }
    
    void loop()     
    {     
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
      Serial.println(tripped);
      send(msg.set(tripped?"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), CHANGE, SLEEP_TIME);
    }
    
    

  • Mod

    @miro
    have you tried to power he sensor with external 5v instead of 5v pin from arduino?



  • Hi all,

    The motion sensor project is my first using Arduino and MySensors! I've managed to get the Gateway all up and running and the motion sensor to communicate with it (using the standard ino file from the project page), but I seem to have a couple of issues:

    • Despite the default sleep time it seems to be communicating with the Gateway constantly.
    • Permanently displayed as in an 'On' state in Domoticz.

    I've tried adjusting the sensitivity and time but it doesn't seem to make a difference.

    I don't know which bit is supposed to contain the values which I can decipher with the serial protocol guide so apologies if the answer is staring me in the face!

    Gateway received
    0;255;3;0;9;TSF:MSG:READ,1-1-0,s=1,c=1,t=16,pt=0,l=1,sg=0:1
    1;1;1;0;16;1

    Node sent
    0
    1383086 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    1383106 MCO:SLP:MS=120000,SMS=0,I1=1,M1=1,I2=255,M2=255
    1383120 MCO:SLP:TPD
    1383127 MCO:SLP:WUP=1

    Any help would be appreciated :)


  • Mod

    try to use a 10uF or bigger capacitor on the 5v and GND, I had issues with mine triggering randomly and after using a stable 5v supply it got back to normal: in fact later on when I added the capacitor to help the NRF24 radio it also stabilized the 5V for the sensor and it is now working fine.
    Did you try to use just a sleep(2000) without the interrupt? Just to check if it is going to sleep correctly



  • Hi,

    i'm trying to get my motionsensor working, but i see only 0 in the terminal what send to the gateway. Does anyone know why i only see that 0 and not a 1? I'm asuming that 1 is a movement.



  • I have found in my experiments with the PIR motion sensor that at least the type that I am using is extremely sensitive to ripple in the Vcc voltage.

    First of all, it is unstable as the feed voltage approaches 5V and then it triggers spontaneously again and again. I then tried to rise the voltage to around 11V which is the voltage that the battery eliminator gives. But that voltage has a ripple of 0.1 volt amplitude so this triggers the sensor again and again also since the arduino introduces small changes in the voltage when it processes the script.

    The solution was to install a LM317 voltage regulator to bring down the voltage to 8V which is then ripple free and stable. Now the PIR works very well and does not give false triggers.

    The arduino is fed from a separate voltage regulator giving 5V. I tried to connect the battery eliminator directly to RAW but that released the smoke from the on-board voltage regulator and as you know, when you let the smoke out from components, they no longer work. :grinning:

    Apparently the battery eliminator also produces some voltage spikes that are not healthy.


  • Mod

    I solved my random pir triggers with a Capacitor on the 5v pin.



  • @Kieren-Perry said in 💬 Motion Sensor:

    Hi all,

    The motion sensor project is my first using Arduino and MySensors! I've managed to get the Gateway all up and running and the motion sensor to communicate with it (using the standard ino file from the project page), but I seem to have a couple of issues:

    • Despite the default sleep time it seems to be communicating with the Gateway constantly.
    • Permanently displayed as in an 'On' state in Domoticz.

    I've tried adjusting the sensitivity and time but it doesn't seem to make a difference.

    I don't know which bit is supposed to contain the values which I can decipher with the serial protocol guide so apologies if the answer is staring me in the face!

    Gateway received
    0;255;3;0;9;TSF:MSG:READ,1-1-0,s=1,c=1,t=16,pt=0,l=1,sg=0:1
    1;1;1;0;16;1

    Node sent
    0
    1383086 TSF:MSG:SEND,1-1-0-0,s=1,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=OK:0
    1383106 MCO:SLP:MS=120000,SMS=0,I1=1,M1=1,I2=255,M2=255
    1383120 MCO:SLP:TPD
    1383127 MCO:SLP:WUP=1

    Any help would be appreciated :)

    Thanks for all your comments! Tried the capacitor route but no dice. In the end, connected a 9v battery to the jack and the PIR to the VIN as the voltage from the 5v pin and USB was a little below what it should have been.



  • If I want something to run for 10 seconds after a motion have been detected, how would I make this happend?


  • Mod

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



  • 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);
      }
    }
    

  • Mod

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


  • Hardware Contributor

    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.



  • @LastSamurai
    Would you do that through "rules" ?


  • Mod

    @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.



  • 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?



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


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.