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.7k 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.
  • 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
      • M Offline
        M Offline
        MCF
        wrote on last edited by
        #53

        this is my code

        /**
         * 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 - February 15, 2014 - Bruce Lacey
         * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
         *
         * DESCRIPTION
         * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
         * <henrik.ekblad@gmail.com> Vera Arduino Sensor project.
         * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
         *
         * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
         * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
         * to the LED negative terminal and the MOSFET Source pin is connected to ground.
         *
         * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
         * http://www.mysensors.org/build/dimmer
         * 
         * 
         * LISTE DE POINTS:
         * D0  DISPO
         * D1  MOTION
         * D2  IRQ NRF
         * D3  LED1
         * D4  DISPO
         * D5  LED2
         * D6  LED3
         * D7  DISPO
         * D8  DISPO
         * D9  CE NRF
         * D10 CSN/CS NRF
         * D11 MOSI NRF
         * D12 MISO NRF
         * D13 SCK NRF
         * D14 DISPO
         * 
         * 
         */
        
        // Enable debug prints to serial monitor
        #define MY_DEBUG 
        
        // Enable and select radio type attached
        #define MY_RADIO_NRF24
        //#define MY_RADIO_RFM69
        
        //#define MY_NODE_ID 153 
        
        #include <SPI.h>
        #include <MySensors.h> 
        
        #define SN "BUREAU PARENTS"
        #define SV "1.2"
        
        #define noLEDs 3
        const int LED_Pin[] = {3, 5, 6}; 
        
        #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
        
        static int currentLevel1 = 0;  // Current dim level...
        static int currentLevel2 = 0;  // Current dim level...
        static int currentLevel3 = 0;  // Current dim level...
        
        MyMessage dimmer1Msg(1, V_DIMMER);
        MyMessage light1Msg(1, V_LIGHT);
        MyMessage dimmer2Msg(2, V_DIMMER);
        MyMessage light2Msg(2, V_LIGHT);
        MyMessage dimmer3Msg(3, V_DIMMER);
        MyMessage light3Msg(3, V_LIGHT);
        
        // MOTION SENSOR
        uint32_t SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
        #define DIGITAL_INPUT_SENSOR 1   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
        #define CHILD_ID 4   // Id of the sensor child
        MyMessage msg(CHILD_ID, V_TRIPPED);
        
        /***
         * Dimmable LED initialization method
         */
        void setup()  
        { 
          // LEDS
          // Pull the gateway's current dim level - restore light level upon sendor node power-up
        for (int sensor=1; sensor<=noLEDs; sensor++){
          request( sensor, V_DIMMER );
         }
         // MOTION
         pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
        }
        
        void presentation() {
          // Register the LED Dimmable Light with the gateway
         for (int sensor=1; sensor<=noLEDs; sensor++){
         present(sensor, S_DIMMER);
         wait(2);
         }
          // MOTION
          present(CHILD_ID, S_MOTION);
          
          sendSketchInfo(SN, SV);
        }
        
        /***
         *  Dimmable LED main processing loop 
         */
        void loop() 
        {
          // Read digital motion value
            bool tripped = digitalRead(DIGITAL_INPUT_SENSOR)== HIGH;
            
            Serial.println("coucou");
            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);
        }
        
        
        
        void receive(const MyMessage &message) {
          if (message.type == V_LIGHT || message.type == V_DIMMER) {
        
            if (message.sensor == 1) {
        
             //  Retrieve the power or dim level from the incoming request message
            int requestedLevel1 = atoi( message.data );
            
            // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
            requestedLevel1 *= ( message.type == V_LIGHT ? 100 : 1 );
            
            // Clip incoming level to valid range of 0 to 100
            requestedLevel1 = requestedLevel1 > 100 ? 100 : requestedLevel1;
            requestedLevel1 = requestedLevel1 < 0   ? 0   : requestedLevel1;
        
              
             fadeToLevel1( requestedLevel1, message.sensor );
            send(light1Msg.set(currentLevel1 > 0 ? 1 : 0)); 
            send(dimmer1Msg.set(currentLevel1) );}
           
            
            
             
           if (message.sensor == 2) {
           //  Retrieve the power or dim level from the incoming request message
            int requestedLevel2 = atoi( message.data );
            
            // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
            requestedLevel2 *= ( message.type == V_LIGHT ? 100 : 1 );
            
            // Clip incoming level to valid range of 0 to 100
            requestedLevel2 = requestedLevel2 > 100 ? 100 : requestedLevel2;
            requestedLevel2 = requestedLevel2 < 0   ? 0   : requestedLevel2;
            
            fadeToLevel2( requestedLevel2, message.sensor );
            send(light2Msg.set(currentLevel2 > 0 ? 1 : 0));
            send(dimmer2Msg.set(currentLevel2) );}
             
            if (message.sensor == 3) {
           //  Retrieve the power or dim level from the incoming request message
            int requestedLevel3 = atoi( message.data );
            
            // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
            requestedLevel3 *= ( message.type == V_LIGHT ? 100 : 1 );
            
            // Clip incoming level to valid range of 0 to 100
            requestedLevel3 = requestedLevel3 > 100 ? 100 : requestedLevel3;
            requestedLevel3 = requestedLevel3 < 0   ? 0   : requestedLevel3;
              
             fadeToLevel3( requestedLevel3, message.sensor );
            send(light3Msg.set(currentLevel3 > 0 ? 1 : 0));
            send(dimmer3Msg.set(currentLevel3) );}
            }
        
            
        }
        
        /***
         *  This method provides a graceful fade up/down effect
         */
        void fadeToLevel1( int toLevel1, int ledid1 ) {
        
          int delta1 = ( toLevel1 - currentLevel1 ) < 0 ? -1 : 1;
          
          while ( currentLevel1 != toLevel1 ) {
            currentLevel1 += delta1;
            analogWrite(LED_Pin[ledid1-1], (int)(currentLevel1 / 100. * 255) );
            wait( FADE_DELAY );
          }
        }
        void fadeToLevel2( int toLevel2, int ledid2 ) {
        
          int delta2 = ( toLevel2 - currentLevel2 ) < 0 ? -1 : 1;
          
          while ( currentLevel2 != toLevel2 ) {
            currentLevel2 += delta2;
            analogWrite(LED_Pin[ledid2-1], (int)(currentLevel2 / 100. * 255) );
            wait( FADE_DELAY );
          }
        }
        void fadeToLevel3( int toLevel3, int ledid3 ) {
        
          int delta3 = ( toLevel3 - currentLevel3 ) < 0 ? -1 : 1;
          
          while ( currentLevel3 != toLevel3 ) {
            currentLevel3 += delta3;
            analogWrite(LED_Pin[ledid3-1], (int)(currentLevel3 / 100. * 255) );
            wait( FADE_DELAY );
          }
        }
        
        1 Reply Last reply
        0
        • gohanG Offline
          gohanG Offline
          gohan
          Mod
          wrote on last edited by
          #54

          Since it is a powered node, you don't need to put it to sleep. As for the pir you can do as you like: check pin status during loop, use an attach interrupt function on pin change, etc

          M 1 Reply Last reply
          0
          • gohanG gohan

            Since it is a powered node, you don't need to put it to sleep. As for the pir you can do as you like: check pin status during loop, use an attach interrupt function on pin change, etc

            M Offline
            M Offline
            MCF
            wrote on last edited by
            #55

            @gohan ok thank you for answering. if i do this:

            void loop() 
            {
              // Read digital motion value
                bool tripped = digitalRead(DIGITAL_INPUT_SENSOR)== HIGH;
                
                Serial.println("coucou");
                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);
            }
            

            the node never stop to send message to the gateway. i would like to send something only if D1 goes to high, is that possible ? (i'm starting combinating exemple and i want to understand what i do now!!)

            1 Reply Last reply
            0
            • wesW Offline
              wesW Offline
              wes
              wrote on last edited by
              #56

              @siod The HC-SR501 actually runs on 3.3VDC but has a 5VDC-3.3VDC regulator on board.

              There are several ways to bypass this regulator, but the easiest is to connect your 3.3VDC supply to pin 3 of JP3 (the one marked "H").

              You must make sure that your 3.3VDC supply is very stable, otherwise you will get false activations.

              See:
              https://randomnerdtutorials.com/modifying-cheap-pir-motion-sensor-to-work-at-3-3v/
              https://www.youtube.com/watch?v=5jhTQAV-hg0
              https://www.mpja.com/download/31227sc.pdf

              Blog: https://www.wes.id.au/
              Nodes: Arduino Pro Mini ATMega328P 3.3V 8MHz, RFM69 433MHz, Canton Power CE024 0.8-3.3V regulator & single AA battery
              Gateway & Controller: Raspberry Pi 3 + Home Assistant

              1 Reply Last reply
              1
              • joaoabsJ Offline
                joaoabsJ Offline
                joaoabs
                wrote on last edited by
                #57

                Hi,

                The code

                sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME); 
                

                doesn't need a ISR (Interrupt Service Routine) function? Should we assume that if no ISR is defined, the node will wake in the loop function?

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

                  Yes, it will wake and run the loop once

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    patrikr76
                    wrote on last edited by
                    #59

                    So i was testing out the motionsensor sketch and it kept giving me "!TSF:MSG:SEND" after the initial 0 had been sent.

                    Finding no answers on here i tested loads of things until i tried a delay after the sleep command.
                    Running fine now with a 5msec delay.

                    Just thought i would share.

                    B 1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mitsured
                      wrote on last edited by
                      #60

                      I connected a 12v DC input and no matter what I do the output always shows 12V. It never goes to a "low" 0V output. From what I read these can use between a 5v and 20v input so I'm assuming a 12v input should not be an issue and still produce a High when triggered and Low 0V when not triggered output. What am I doing wrong?

                      1 Reply Last reply
                      0
                      • P patrikr76

                        So i was testing out the motionsensor sketch and it kept giving me "!TSF:MSG:SEND" after the initial 0 had been sent.

                        Finding no answers on here i tested loads of things until i tried a delay after the sleep command.
                        Running fine now with a 5msec delay.

                        Just thought i would share.

                        B Offline
                        B Offline
                        bereska
                        wrote on last edited by
                        #61

                        @patrikr76 thank you so much. I had the same issue with my motion sensor and spent 2 days pulling my hair out)

                        1 Reply Last reply
                        1
                        • E Offline
                          E Offline
                          etlizzie
                          wrote on last edited by
                          #62

                          Was anyone able to reduce the delay time? 5 seconds is too long for me.

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            EileenWen
                            Banned
                            wrote on last edited by
                            #63
                            This post is deleted!
                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            16

                            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