PWM for 4-pins FAN

Moderator: leecollings

Post Reply
doodoovane
Posts: 11
Joined: Friday 11 December 2015 3:15
Target OS: Raspberry Pi / ODroid
Domoticz version: v2.3530
Location: French Polynesia
Contact:

PWM for 4-pins FAN

Post by doodoovane »

Hello everyone,

following another post I did to control the speed of a fan using PWM, I now have the solution using Mysensors.

My problem now is that I can't get the switch more than 99% in domoticz...
The only way to get 100% is to turn it off and turn it back on. As long as you touch the slider of the dimmer, no more 100%...
The log says "Set Level: 99 %" even if I push it to 100%.

Here is the code I use:

Code: Select all

// Enable debug prints to serial monitor
#define MY_DEBUG


// Enable and select radio type attached
//#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95

// Set LOW transmit power level as default, if you have an amplified NRF-module and
// power your radio separately with a good regulator you can turn up PA level.
//#define MY_RF24_PA_LEVEL RF24_PA_LOW

// Enable serial gateway
#define MY_GATEWAY_SERIAL

// Define a lower baud rate for Arduinos running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 38400
#endif

// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
//#define MY_INCLUSION_BUTTON_FEATURE

// Inverses behavior of inclusion button (if using external pullup)
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP

// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
//#define MY_INCLUSION_MODE_BUTTON_PIN  3

// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300

// Inverses the behavior of leds
//#define MY_WITH_LEDS_BLINKING_INVERSE

// Flash leds on rx/tx/err
// Uncomment to override default HW configurations
//#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
//#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED

#include <MySensors.h>
#include <TimerOne.h>

#define SN "Fan_PWM"
#define SV "1.0"

#define LED_PIN 9      // 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)


static int16_t currentLevel = 0;  // Current dim level...
MyMessage dimmerMsg(0, V_DIMMER);
MyMessage lightMsg(0, V_LIGHT);

void setup()
{
  // Setup locally attached sensors
 request( 0, V_DIMMER );
 #define PWM_FREQ_HZ (25000) 
 #define PWM_CYCLE_US (1000000/PWM_FREQ_HZ)

Timer1.initialize(PWM_CYCLE_US);
Timer1.pwm(LED_PIN, 1023); 
}

void presentation()
{
  // Present locally attached sensors
 present( 0, S_DIMMER );

    sendSketchInfo(SN, SV);
}

void loop()
{
  // Send locally attached sensor data here
}

void receive(const MyMessage &message)
{
    if (message.getType() == V_LIGHT || message.getType() == 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.getType() == 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...
        send(lightMsg.set(currentLevel > 0));

        // hek comment: Is this really nessesary?
        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 );
    }
}
I can't get what's wrong. Every bit of help would be appreciated :D
iguaan
Posts: 15
Joined: Thursday 21 May 2020 7:40
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: PWM for 4-pins FAN

Post by iguaan »

Led pin 9 is used by radio, change it to 3.

Sent from my MI 9 using Tapatalk

doodoovane
Posts: 11
Joined: Friday 11 December 2015 3:15
Target OS: Raspberry Pi / ODroid
Domoticz version: v2.3530
Location: French Polynesia
Contact:

Re: PWM for 4-pins FAN

Post by doodoovane »

If I do this my fan start to make a little noise.
(the one that I got rid off using

Code: Select all

#define PWM_FREQ_HZ (25000) 
 #define PWM_CYCLE_US (1000000/PWM_FREQ_HZ)

Timer1.initialize(PWM_CYCLE_US);
Timer1.pwm(LED_PIN, 1023);
)

I think it is because TimerOne library only work with pin9 and 10 on the arduino nano.
Let me know if you have any solution
iguaan
Posts: 15
Joined: Thursday 21 May 2020 7:40
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: PWM for 4-pins FAN

Post by iguaan »

You could try:
#define MY_RF24_CE_PIN 5
And rewire that pin on radio.
Interrupt pins on nano/micro are 2, 3.

Sent from my MI 9 using Tapatalk

iguaan
Posts: 15
Joined: Thursday 21 May 2020 7:40
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: PWM for 4-pins FAN

Post by iguaan »

doodoovane wrote:If I do this my fan start to make a little noise.
(the one that I got rid off using

Code: Select all

#define PWM_FREQ_HZ (25000) 
 #define PWM_CYCLE_US (1000000/PWM_FREQ_HZ)

Timer1.initialize(PWM_CYCLE_US);
Timer1.pwm(LED_PIN, 1023);
)

I think it is because TimerOne library only work with pin9 and 10 on the arduino nano.
Let me know if you have any solution
You could try:
#define MY_RF24_CE_PIN 5
And rewire that pin on radio.
Interrupt pins on nano/micro are 2, 3.

Sent from my MI 9 using Tapatalk

doodoovane
Posts: 11
Joined: Friday 11 December 2015 3:15
Target OS: Raspberry Pi / ODroid
Domoticz version: v2.3530
Location: French Polynesia
Contact:

Re: PWM for 4-pins FAN

Post by doodoovane »

Ok I see what you mean.
But I'm using my gateway without any communication module (sensors dyrectly on the gateway wich is plug via USB on my Pi) so no need to do that? Right?

Code: Select all

//#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95
Any idea why the domoticz switch does not dim to 100%?
doodoovane
Posts: 11
Joined: Friday 11 December 2015 3:15
Target OS: Raspberry Pi / ODroid
Domoticz version: v2.3530
Location: French Polynesia
Contact:

Re: PWM for 4-pins FAN

Post by doodoovane »

Up for any help :)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests