optoisolator driven mosfet turns on on power up and freezes arduino

Hi, here again looking for some wisdom…

I have a nano (chinese clone with CH340) running a polishing tumbler… so I have several modules / elements: H-bridge, current sensor, analog keypad, LCD …

but the last element is module with a IRF540 mosfet, driven by a sharp PC817 optoisolator, freewheel diode… Transistor DC Relay, 5V logic, DC 24V/30A - RobotDyn

The mosfet is driving a 24v cooling fan (a small 3010) and is supposed to be On/Off I’m not using PWM on it

The problem is that when I power up, the mosfet gets “powered” and the arduino freezes, so it never actually boots.
some precisions:

  • it happens randomly, sometimes I can power up 10 times and it will freeze 10 times. sometimes everything boots normally…
  • when the problem occurs, I measure ~2.4v on the optoisolator input == ~2.4v from the nano pin D4
  • when the problem occurs, the Rx led on the nano remains powered on.
  • I tried a 10kOhm pull-down between D4 and GND and it didn’t have any effect. (also tried with a 4.7kOhm)
  • when the problem occurs, I can reset the nano via the reset pin, and then it will boot normally.
  • The problem never happens if I unplug this mosfet module.

Just in case this is relevant, I burned MiniCore bootloader GitHub - MCUdude/MiniCore: Arduino hardware package for ATmega8, ATmega48, ATmega88, ATmega168, ATmega328 and ATmega328PB and the mosfet module is plugged on the D4 pin.

my code is quite long, so I will only put my setup tab here (and I don’t believe that the rest of the code might be responsible for the problem…)

const byte pin_fan = 4; // the pin to drive the transistor module powering the fan that cools down the H-bridge

sorry I couldn’t post the full declaration tab, the post will exceed he maximum length…

// ----------------------------------  RUNNING SETUP --------------------------------
void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  // --------------------------- Initializing pins -------------------------------
  pinMode(pin_fan, OUTPUT);
  pinMode(pin_keypad, INPUT);
  pinMode(pin_speed, INPUT);
  pinMode(pin_IRsig, INPUT);
  pinMode(pin_IRpwr, OUTPUT);
  digitalWrite(pin_IRpwr, LOW);
  pinMode(extbutton, INPUT_PULLUP);
  //pinMode(pin_current, INPUT);
  //pinMode(pin_temperature, 
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);  // Used for serial Com
  //lcd.begin(16,2);   // initialize the lcd for 16 chars 2 lines, turn on backlight
  // ----------------------------- INIT PWM ------------------------------------------
  //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe();
  //sets the frequency for the specified pin
  SetPinFrequencySafe(RPWM_Output, frequency);
  SetPinFrequencySafe(LPWM_Output, frequency);
  //some debugging for the PWM frequency
  /*
    bool success = SetPinFrequencySafe(RPWM_Output, frequency);

    if(success)
    {
    Serial.println ("RPWM OK");
    delay (1000);
    }
    success = SetPinFrequencySafe(LPWM_Output, frequency);
    if(success)
    {
    Serial.println ("LPWM OK");
    delay (1000);
    }
  */
  /*
    // ------- Quick 3 blinks of backlight  -------------
    for(int i = 0; i< 3; i++)
    {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
    }
  */

  // initizializes the arrays
  for (int thisReading = 0; thisReading < numr_current; thisReading++) {
    array_current[thisReading] = 0;
  }
  for (int thisReading = 0; thisReading < numr_tmprtr; thisReading++) {
    array_tmprtr[thisReading] = 1023; //array of values set to 1023 to set the temperature mean value at the beginning at 0°C, note that it's a NTC thermistor, so 1023-->0°C
  }
  total_tmprtr=1023*numr_tmprtr;
  /*
    for (int thisReading = 0; thisReading < numr_pot; thisReading++) {
    array_pot[thisReading] = 0;
    }
  */
  LCDonoff(); // Turns on the LCD
    lcd.setCursor(0, 0); //Start at character 4 on line 0
    lcd.print(F("ToPo version 6"));
    delay(250);
    lcd.setCursor(0, 1);
    lcd.print(F(" << AUDACIEUX >>"));
    delay(250);
  
}/*--(end setup )---*/

Please show how you've wired everything together, what the power source is, link to datasheet of the fan perhaps, all the information about the hardware.

Have you checked the function of the MOSFET switch by itself?

Its best to test each component separately before combining, as troubleshooting is much easier this way.

If I understand correctly, you should put a resistor (10K should do) from the gate of the mosfet (assuming N-channel here) to ground.
This way you ensure an off state for the transistor.

If the above doesn't do the trick, post a schematic

Hope this helps

First of all, thank you for your answers and sorry for this late late reply.

MarkT:
Please show how you've wired everything together, what the power source is, link to datasheet of the fan perhaps, all the information about the hardware.

Have you checked the function of the MOSFET switch by itself?

Its best to test each component separately before combining, as troubleshooting is much easier this way.

I tried every component separately and everything seems to work as it should.

senderene:
If I understand correctly, you should put a resistor (10K should do) from the gate of the mosfet (assuming N-channel here) to ground.
This way you ensure an off state for the transistor.

If the above doesn't do the trick, post a schematic

Hope this helps

The Chinese module has a pull-down 10kOhm resistor, I double triple check it :wink:
Not happy with that I tried adding another one on my own. The problem persisted.

I just burnt my code without a bootloader (using another nano as ISP programmer) and the problem is gone (I've been using it for a while now)