Dimmer issues.



  • Hi guys, i'm trying to put together a dimmer circuit as well as a temp/voltage sensing and motion as well. I've got it all hooked up bar the motion side of things and the temp works great, voltage is sort of ok although not that accurate but can live with it but the dimmer side of things doesn't work great. I have used a STP16NF06L rather than the IRLZ44N but i thought this should be ok since i've used this in a ESP8266 dimmer project along with mysensors and Domoticz and it works great.

    What is happening is it's not actually dimming, if i put it above %50 it turns on but then it's a constant brightness and doesn't get brighter or dim going the other way. Is it just down to the mosfet substitution? I've tried with the standard sketch and it does this. My modified code is below. Any help would be much appreciated.

    /**
     * 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
     */
    
    #define SN "DimmableLED"
    #define SV "1.1"
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_VOLTAGE 2
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    
    #include <MySensor.h> 
    #include <SPI.h>
    #include <DHT.h> 
    
    #define LED_PIN 4      // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
    
    MySensor gw;
    
    DHT dht;
    float lastTemp;
    boolean metric = true;
    float lastHum;
    float lastVolt;
    int oldBatteryPcnt = 0;
    int analogInput = A0;
    float vout = 0.0;
    float vin = 0.0;
    int value = 0;
    float R1 = 30000.0;  
    float R2 = 7500.0;
    
    static int currentLevel = 0;  // Current dim level...
    MyMessage dimmerMsg(0, V_DIMMER);
    MyMessage lightMsg(0, V_LIGHT);
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgVolt(CHILD_ID_VOLTAGE, V_VOLTAGE); 
    
    /***
     * Dimmable LED initialization method
     */
    void setup()  
    { 
      Serial.println( SN ); 
      gw.begin( incomingMessage );
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      // Register the LED Dimmable Light with the gateway
      gw.present( 0, S_DIMMER );
      
      gw.sendSketchInfo(SN, SV);
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      gw.request( 0, V_DIMMER );
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);
      gw.present(CHILD_ID_VOLTAGE, V_VOLTAGE);
      gw.request( 0, V_DIMMER );
      pinMode(analogInput, INPUT);
    }
    
    /***
     *  Dimmable LED main processing loop 
     */
    void loop() 
    {
      gw.process();  
      readTempHum();
      measureBattery();
    }
    
    
    
    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 to " );
        Serial.print( requestedLevel );
        Serial.print( ", from " ); 
        Serial.println( currentLevel );
    
        fadeToLevel( requestedLevel );
        
        // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
        gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
    
        // hek comment: Is this really nessesary?
        gw.send( dimmerMsg.set(currentLevel) );
    
        
        }
    }
    
    /***
     *  This method provides a graceful fade up/down effect
     */
    void fadeToLevel( int toLevel ) {
    
      int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
      
      while ( currentLevel != toLevel ) {
        currentLevel += delta;
        analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
        delay( FADE_DELAY );
      }
    }
    
    void readTempHum() {
     delay(dht.getMinimumSamplingPeriod());
    
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp) {
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        gw.send(msgTemp.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(msgHum.set(humidity, 1));
          Serial.print("H: ");
          Serial.println(humidity);
      } 
    }
    
    void measureBattery() {
       // read the value at analog input
       value = analogRead(analogInput);
       vout = (value * 5.0) / 1024.0; // see text
       vin = vout / (R2/(R1+R2)); 
       int batteryPcnt = value / 10;
          
       #ifdef DEBUG
       Serial.print("Battery Voltage: ");
       Serial.print(vin);
       Serial.println(" V");
    
       Serial.print("Battery percent: ");
       Serial.print(batteryPcnt);
       Serial.println(" %");
       #endif
       
       if (oldBatteryPcnt != batteryPcnt) {
         // Power up radio after sleep
         gw.sendBatteryLevel(batteryPcnt);
         gw.send(msgVolt.set(vin, 1));
         oldBatteryPcnt = batteryPcnt;
       }
    }
    
    


  • Rude to reply to your own posts or so i'm told but for the benefit of others i was being an idiot and pin 4 isn't pwm (facepalm) i was looking at this for a while last night as well. Ah well never mind it's working brilliantly now after switching to pin 5 🙂


  • Mod

    Great! Thanks for telling us the solution.


Log in to reply
 

Suggested Topics

  • 3
  • 3
  • 2
  • 2
  • 24
  • 2

28
Online

11.2k
Users

11.1k
Topics

112.5k
Posts