[contest] My 4-in1 LED-dimmer/motion/temp-hum sensor



  • Nothing new but just my implementation of great examples found on this forum and made 'my own' 🙂
    A LED-dimmer triggered with a motion-sensor and managed via PLEG on a VeraLite. The box is mounted under my bathtub as floor-lighting. PLEG takes care of the intensity depending on day- or night. The sketch has a watchdog routine for taking care of switching of might there never be an 'off' command coming in from Vera.

    extern.jpg
    intern.jpg



  • Very nice, care to post your sketch and parts list? I am looking to add some similar devices in my basement home theater.



  • @NotYetRated
    The components are:

    • Arduino nano
    • DC/DC 3.3V AMS1117-3.3V Power Supply Module
    • 10uF capacitor on 3.3v regulator
    • MosFET 50N06 with 10K pull-down resistor on gate
    • NRF24L01+PA+LNA
    • DHT22 Temp/Humidity Sensor
    • 12v DC wall-wart
    • Motion-sensor HC-SR501
    • Case and piece of perfboard

    The LEDstrip and DHT are connected via 3mm plugs and jacks.
    The DHT and NRF are fed by the 3.3v regulator. The nano and FET are using the raw 12v.

    The sketch:
    (sorry, no working codebender account yet)

    /***
     * 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.
     * 
     * 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.
     *
     * REVISION HISTORY
     * Version 1.0  - February 15, 2014 - Bruce Lacey
     * Version 1.1  - August 13, 2014 - Converted to 1.4 (hek) 
     * Version 1.1a - August 14, 2014 - Added MotionSensor (Ed)
     * Version 1.1b - December 22, 2014 - Added DHT-Sensor (Ed)
     * Version 1.1c - December 29, 2014 - Added LED timeout (Ed)
     * Version 1.1d - Januari 4, 2015 - Updated lib to 1.4.1 (Ed)
     *
     ***/
    #define SN "DimmableLED"
    #define SV "1.1d"
    
    #include <MySensor.h> 
    #include <SPI.h>
    #include <DHT.h>
    
    #define Motion_PIN 3           // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define INTERRUPT Motion_PIN-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define LED_PIN 5              // Arduino pin attached to MOSFET Gate pin
    #define DHT_PIN 4
    #define FADE_DELAY 30          // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    #define Motion_CHILD 1         // Id of the MotionSensor child
    #define Dimmer_CHILD 2         // Id of the DimmableLED child
    #define Hum_CHILD 3            // Id of the DHT-humidity child
    #define Temp_CHILD 4           // Id of the DHT-temperature child
    boolean lastMotion = false;
    
    MySensor gw(9,10);
    DHT dht;
    float lastTemp;
    float lastHum;
    static int currentLevel = 0;       // Current dim level...
    long previousMillisDHT = 0;        // stores last time DHT was updated
    long previousMillisLED = 0;        // stores last time LED was updated
    long intervalDHT = 60000;          // read DHT every ....
    long durationLED = 1800000;        // Max 'On'-time for LED's (watchdog)
    MyMessage motionMsg(Motion_CHILD, V_TRIPPED);
    MyMessage dimmerMsg(Dimmer_CHILD, V_DIMMER);
    MyMessage lightMsg(Dimmer_CHILD, V_LIGHT);
    MyMessage humMsg(Hum_CHILD, V_HUM);
    MyMessage tempMsg(Temp_CHILD, V_TEMP);
    
    void setup()  
    { 
      // Serial.println( SN ); 
      gw.begin( incomingMessage );    // Listen to dimmer-info from Vera
      dht.setup(DHT_PIN);
      
      // Register all sensors to gw (they will be created as child devices)
      gw.present( Motion_CHILD, S_MOTION );
      gw.present( Dimmer_CHILD, S_DIMMER );
      gw.present( Hum_CHILD, S_HUM );
      gw.present( Temp_CHILD, S_TEMP );
      
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sender node power-up
      gw.request( Dimmer_CHILD, V_DIMMER );
    }
    
    
    void loop() 
    {
      gw.process();
      
      // Read digital motion value
      boolean motion = digitalRead(Motion_PIN) == HIGH; 
      if (lastMotion != motion) {
        lastMotion = motion;     
        gw.send(motionMsg.set(motion ? "1" : "0" ));  // Send motion value to gw
      }
    
      // DHT22:
      unsigned long currentMillisDHT = millis();
      if(currentMillisDHT - previousMillisDHT > intervalDHT) {
        previousMillisDHT = currentMillisDHT;
        
        float temperature = dht.getTemperature();
        if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
        } else if (temperature != lastTemp) {
          lastTemp = temperature;
        
          gw.send(tempMsg.set(temperature, 1));
          Serial.print("T: ");
          Serial.println(temperature);
        }
      
        float humidity = dht.getHumidity();
        if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
        } else if (humidity != lastHum) {
          lastHum = humidity;
          gw.send(humMsg.set(humidity, 1));
          Serial.print("H: ");
          Serial.println(humidity);
        }
      }
    
      // Limit LED 'On'-time.
      unsigned long currentMillis = millis();
        
        if (( currentLevel > 0 ) && ((currentMillis - previousMillisLED) > durationLED)) {
          analogWrite( LED_PIN, 0 );              // Kill LED's after max 'On'-time
          gw.send(dimmerMsg.set(0));
          currentLevel=0;
        }
      
    } 
    
    
    void incomingMessage(const MyMessage &message) {
      if (message.type == V_LIGHT || message.type == V_DIMMER) {
    
        //  Retrieve the power or dim level from the incoming request message
        int requestedLevel = atoi( message.data );
    
        // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
        requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
    
        // Clip incoming level to valid range of 0 to 100
        requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
        requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
    
        // Serial.print( "Changing level from " );
        // Serial.print( currentLevel );
        // Serial.print( " to " ); 
        // Serial.println( requestedLevel );
    
        fadeToLevel( requestedLevel );
    
        // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
        // gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
        
        }
    }
    
    // This method provides a graceful fade up/down effect
    void fadeToLevel( int toLevel ) {
      if (toLevel > 0){ previousMillisLED = millis();}
      int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
    
      while ( currentLevel != toLevel ) {
        currentLevel += delta;
        analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
        delay( FADE_DELAY );
      }
    }
    

    LEDdimmer.ino
    The DHT part in the sketch is not in use because I damaged the sensor by using the wrong voltage 😞 but this part is 'standard' code borrowed from the MySensors libraries. The project is a succes in my eyes; the box is not visable ander the bath but the motionsensor is sensitive enough to trigger everytime someone enters the room. The sensor gets untripped after 30 seconds but I have PLEG switch off the LED's 90 seconds after still being untrapped. This way I can change the timeout without opening the box. Only the watchdog part is my doing and I'm the worst coder on this forum so be warned 🙂



  • Finally a new DHT22 arrived from China and now this project is completely finished.
    badkamer.png
    Bad1.jpg
    Bad3.jpg
    Bad2.jpg



  • Nice work! Thanks for sharing all that you've done.



  • To C4Vette
    How did You manage to display the data of last trip?



  • You have a sharp eye!
    Have a good read at http://forum.micasaverde.com/index.php?topic=19263.15
    It is all in there.



  • Hi!
    Nice project, and I'm really interested on it!!
    Unfortunately, when I copy/paste your script, I've an error on line 44 during the check in Arduino 1.6.7 for "MySensor gw(9,10);", the message is:

    error: no matching function for call to 'MySensor::MySensor(int, int)'

    Could you please tell me what could I do to make this script working?
    Thanks for your answer!



  • Small question. I want to try this but some errors. Library is installed with library manager. Ubuntu 16.04 and last Arduino ide.
    First try IDE not find library and i change in line 29 #include <MySensor.h> to #include <MySensors.h> add only s
    and then this error.

    motion_dht22_led_dimmer:44: error: 'MySensor' does not name a type
    MySensor gw(9,10);


Log in to reply
 

Suggested Topics

  • 15
  • 8
  • 1
  • 44
  • 3
  • 7

9
Online

11.4k
Users

11.1k
Topics

112.7k
Posts