IRF520N Mosfet and IR Remote do not work together

Hi everyone !

I am developing a project to change the intensity of three led strips and create environments for my modell railroad layout. The idea is to use an IR remote control and pressing each number change the intensity of the LEDs (warm- cold-blue) and the ambient atmosphere.

I use the module IRF520 N-Channel Power MOSFET PWM to control the LEDs and this IR Receiver.

To control leds I follow the last droneboxworkshop example, but instead potentiometers I use de IR receiver.

My test sketch for IR receiver works perfect !! and the MOSFET works right as well !!

BUT... When I put all together something is wrong.

This is a piece of my final sketch over Arduino Uno

// Include Libraries
// -------------------------------------------

#include <IRremote.h>


// IR Sensor
// -------------------------------------------

const int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);
decode_results results;


// Variables
// -------------------------------------------

// Pines

int pin_cold = 3;
int pin_warm = 5;
int pin_blue = 6;

// Current values for each strip

int state_cold;
int state_warm;
int state_blue;

// Values to go for each strip

int value_cold;
int value_warm;
int value_blue;

// State sistem on = true ; off = false

bool state_sistem;


void setup(){
  
  Serial.begin(9600);
  
  // Enable the IR Receiver
  
  irrecv.enableIRIn();

  // Pines

  pinMode(pin_cold, OUTPUT);
  pinMode(pin_warm, OUTPUT);
  pinMode(pin_blue, OUTPUT);

  // First leds states
  
  state_cold = 0;
  state_warm = 0;
  state_blue = 0;

  // Sistem Off
  
  state_sistem = false;

  // Values to go at setup

  value_cold = 0;
  value_warm = 0;
  value_blue = 0;
  
}


void loop(){
        
  
  if (irrecv.decode(&results)){

        // Read Hex code and switch case
        
        switch (results.value) {

          // On button
          
          case 0xFF08F7:

            if(!state_sistem) {
              
              value_cold = 50;
              value_warm = 50;
              value_blue = 50;
              state_sistem = true;
              
              Serial.println("sistem ON");
            
            } else {

              value_cold = 0;
              value_warm = 0;
              value_blue = 0;
              state_sistem = false;
              
              Serial.println("sistem OFF");
              
            }
        
          break;


          // Rest of the IR sender values
          
         
        }

        // Write values to go

        if(state_sistem) {
          analogWrite(pin_cold, value_cold);
          analogWrite(pin_warm, value_warm);
          analogWrite(pin_blue, value_blue);
        }
           
        irrecv.resume();
  }
}

The problem is in the line where code says: irrecv.enableIRIn();

When this line exists I can read IR codes, by nothing is written in analogWrite.
If I delete the line, of course I cannot read IR codes, but analogWrite runs perfect.

Need some Help to solve it !!! Thanks in advance.

1 Like

The IRF520 is an extremely poor choice to use with Arduino, as it generally requires 10V on the gate to be fully switched on.

Use a logic level MOSFET instead.

The IRF520 is powered by 12v and when IR receive code line is not present, works perfect !!

I think you should drop the condition.

The way you have written it, the off state would never be transferred to the hardware.

state_sistem go from false go to true when IR receive the code 0xFF08F7. if state_sistem is true, the analogWrite might be writing.

You don't have to believe me.

Good luck for your project.

Solved !!!

Just write this line at the end of setup section and all working fine !!!!

But not because you moved that statement, you changed something else too.

Read this