Motion and lux meters combined in a single device


  • Hero Member

    Hello,

    I always wanted a motion sensor that could also detect light levels.
    Until now I used two sensors attached to one arduino to achieve that, but now I make a hardware combination that works and also looks more discreet.

    This is the device I used as a base, adding the light sensor.

    _DSC8283.jpg

    First of all I removed the white dome.
    Then a little adjustment was needed becasue the light sensor is too long so the sensor chip fits properly inside the dome.

    _DSC8284.jpg

    Also the white plastic needed some cutting to allow the circuit to enter the dome.

    _DSC8285.jpg

    The light sensor circuit has to be on the opposite side of this little metal pit in order to fit properly.

    _DSC8286.jpg

    Here is the assembly almos done, with the light sensing chip facing up inside the dome.

    _DSC8287.jpg

    Little hotglue on the corners to hold it toghether

    _DSC8290.jpg

    And that's all to it.

    _DSC8291.jpg

    Here's the code. Just a simple combine of the 2 sensors.

    #include <MySensor.h>  
    #include <SPI.h>
    #include <BH1750.h>
    #include <Wire.h> 
    
    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 0
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 1   // Id of the sensor child
    
    BH1750 lightSensor;
    
    MySensor gw;
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    MyMessage msg2(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    uint16_t lastlux;
    
    void setup()  
    {  
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("MotionLuxSensor", "1.0");
    
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_MOTION);
      gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    
      lightSensor.begin();
    }
    
    void loop()     
    {     
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
        
      Serial.println(tripped);
      gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw 
    
      uint16_t lux = lightSensor.readLightLevel();// Get Lux value
      Serial.println(lux);
      if (lux != lastlux) {
          gw.send(msg2.set(lux));
          lastlux = lux;
      }
    
    
      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    }

  • Admin

    @ferpando

    Very nice solution and pictures!


  • Admin

    @ferpando Nice!


  • Hero Member

    Thank you @hek and @blacey
    It works pretty well.
    It updates light level every 30 seconds and also when movement is detected.
    Also the dome helps average the light level of the room making it more consistent.


  • Hero Member

    @ferpando, do you run this sensor on battery? Any idea about for how long the battery will last?


  • Hero Member

    @rvendrame
    Not at the moment. It is designed to be on a wall socket.
    If you plan to use it on battery, probably would be a good idea to by pass both voltage regulators on the devices and run on 3.3V.
    Also consider increasing the time between light reads.



  • @ferpando

    very nice setup. What pins are the Lux sensor wired to the Arduino? In the sketch, only pin A0 is defined but on wiring diagram A4 & A5 is used.


  • Hero Member

    @mikemayers
    You need to connect the arduino to pins 4 and 5 for the digital BH1750.
    I don't know why the sketch references pin 0. Must be something left behind from the analog version.



  • @ferpando got it!



  • Thanks! just what i was looking for!
    Floris



  • is it posible to add an LED indicator to tells there's motion. If so what has to be done?


  • Hero Member

    @mikemayers You could either use the output from the motion sensor directly to operate the LED or write a few lines of code and use an Arduino pin for it. And a Led with resistor of course..



  • @AWI said:

    You could either use the output from the motion sensor directly to operate the LED

    I didn't know you could do that. Should I include a resistor in series? For the Adruino pin, do you have a sample code?


  • Hero Member

    @mikemayers Yes put a resistor in series with the LED (330 ohm will do). A piece of code can be as simple or complicated as you want. There are plenty of examples in Arduino tutorials. Just take care to take specific measures for power management if using battery power. You need to switch off the LED before you enter sleep mode (or be creative).

    // in setup
    pinMode(LED_PIN,OUTPUT);					// define output (LED_PIN = Arduino pin)
    
    // in loop (at the right place)
    if (tripped){    // from the "Blink" example sketch :-)
      digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                         // wait for a second
      digitalWrite(LED_PIN, LOW);    // turn the LED off by making the voltage LOW   
    }
    


  • @mikemayers this might work based on @AWI 's blink ... blink idea.

    #include <MySensor.h>  
    #include <SPI.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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 31   // Id of the sensor child
    #define LED_PIN 5
    
    
    MySensor gw;
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Motion Sensor", "1.0");
    
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      
      pinMode(LED_PIN, OUTPUT); 
      
      
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_MOTION);
      
      
      
    }
    
    void loop()    
     { 
    
      // Read digital motion value
      
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
      Serial.println(tripped);
      gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw 
     
       // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME); 
      
    
      if (tripped){    // from the "Blink" example sketch :-) 
      digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(100);        // wait for a second
      digitalWrite(LED_PIN, LOW);  // turn the LED off by making the voltage LOW
      delay(100);
          
    
      
      }
      
       }
    


  • @jeylites

    Thank you!
    It seems to be working I think..... Question: When motion is triggered there is no activity on the LED, but when the motion is turned OFF the LED blinks once. Is it suppose to do that? I was wondering if could it be the other way, blink LED when there's motion and OFF LED when there's no motion. Can that be done? What do I have to change to have it do that? Thanks again guys!



  • @mikemayers

    I get what you're saying and I've no clue on how to do that. 😞


  • Hero Member

    @mikemayers put the gw.sleep(... line at the end of the loop. Everytime motion is detected or released there is an interrupt. At this time the loop starts again and you should check the status and blink the led if on or change.



  • @AWI I did as you say on gw and it did turn on the LED when motion is triggered, but the motion remained ON even after the set duration. I think, it's creating a continues loop.


  • Hero Member

    @mikemayers can you post your sketch?



  • @AWI
    @jeylites

    This is what I have done....moved "gw.sleep" to the end.

    #include <MySensor.h>  
    #include <SPI.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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 31   // Id of the sensor child
    #define LED_PIN 5
    
    
    MySensor gw;
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Motion Sensor", "1.0");
    
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      
      pinMode(LED_PIN, OUTPUT); 
      
      
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_MOTION);
      
      
      
    }
    
    void loop()    
     { 
    
      // Read digital motion value
      
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
      Serial.println(tripped);
      gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw 
    
    
      if (tripped){    // from the "Blink" example sketch :-) 
      digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(100);        // wait for a second
      digitalWrite(LED_PIN, LOW);  // turn the LED off by making the voltage LOW
      delay(100);
          
       // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME); 
      
      
      }
      
       }
    

  • Hero Member

    Try moving the gw.sleep outside your if statement like this:

        if (tripped){    // from the "Blink" example sketch :-) 
           digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
           delay(100);        // wait for a second
           digitalWrite(LED_PIN, LOW);  // turn the LED off by making the voltage LOW
           delay(100);
        }
    
    // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
    gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);   
    
    }


  • With the help from @korttoma & @AWI , I put together this sketch. I've tested it and it works. Have fun!

    #include <MySensor.h>  
    #include <SPI.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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 1   // Id of the sensor child
    #define LED_PIN 5
    
    
    MySensor gw;
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Motion Sensor", "1.0");
    
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      
      pinMode(LED_PIN, OUTPUT); 
      
      
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_MOTION);
      
      
      
    }
    
    void loop()    
     { 
    
      // Read digital motion value
      
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
      Serial.println(tripped);
      gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw 
    
    {
      if (tripped){    // from the "Blink" example sketch :-) 
      digitalWrite(LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(1000);        // wait for a second
      digitalWrite(LED_PIN, LOW);  // turn the LED off by making the voltage LOW
      delay(1000);
      
      
     }
           
            
       // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
      gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME); 
      
      
          }
     }
    
    

Log in to reply
 

Suggested Topics

  • 87
  • 7
  • 1
  • 8
  • 3
  • 5

58
Online

11.4k
Users

11.1k
Topics

112.6k
Posts