Multisensor PIR and Lux

  • I was finally able to combine the two example sketches (BH1750 Lux sensor and the HC-SR501. I thought I would share it in case it might help out another newbie like me 😉

     * 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 <>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list:
     * Documentation:
     * Support Forum:
     * 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.
     *Light/Motion sensor using sketches from idefix and Henrik Ekblad
     * Arduino BH1750FVI Light sensor
     * communicate using I2C Protocol
     * this library enable 2 slave device addresses
     * Main address  0x23
     * secondary address 0x5C
     * connect the sensor as follows :
     *   VCC  >>> 5V
     *   Gnd  >>> Gnd
     *   ADDR >>> NC or GND  
     *   SCL  >>> A5
     *   SDA  >>> A4
     * Motion Sensor example using HC-SR501 
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    #include <SPI.h>
    #include <MySensors.h>  
    #include <BH1750.h>
    #include <Wire.h>
    #define CHILD_ID_LIGHT 0
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID_Motion 1   // Id of the sensor child
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    BH1750 lightSensor;
    // V_LIGHT_LEVEL should only be used for uncalibrated light level 0-100%.
    // If your controller supports the new V_LEVEL variable, use this instead for
    // transmitting LUX light level.
    MyMessage msgLight(CHILD_ID_LIGHT, V_LEVEL);
    // Initialize motion message:
    MyMessage msgMotion(CHILD_ID_Motion, V_TRIPPED);
    uint16_t lastlux;
    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("Lux/Motion Sensor", "1.0");
      // Register all sensors to gateway (they will be created as child devices)
      present(CHILD_ID_Motion, S_MOTION);
    void loop()      
      uint16_t lux = lightSensor.readLightLevel();// Get Lux value
      if (lux != lastlux) {
          lastlux = lux;
     // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
      send(msgMotion.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);

  • Hey Kcest

    Nice work!
    Can you help me if I would like two PIR sensors in the same sketches.

    thank you in advance, Tom

Log in to reply





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