Everything works fine till I close the lid of the box

Hi, I've tried with different Arduinos and always get the same problem. I've connected to the Arduino a 12V fan with Mosfet, resistor and a momentary push button. My Sketch allows the button to have 4 stages.

Press once (Stage 1), the fan turns slow
press again (Stage 2), the fan turns faster
press again (Stage 3), the fan turns fastest
press again (Stage 4), the fan stops.

Everything works fine, until I close the lid of the metal encasing box, then the fan starts sometimes by itself or skips stages by itself or when I press the button. When I open the lid it works normal again. I tried to isolate the complete Arduino with components, but it does not help.

What can the problem be please.
PS: I'm very new to Arduino, sorry

And here my code:

#define fan 9
#define btn 3

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int state, lastState;
int fan_speed = 0;
void setup() {
  Serial.begin(9600);
  pinMode(fan, OUTPUT);
  pinMode(btn, INPUT_PULLUP);
  digitalWrite(fan,LOW);
  setPwmFrequency(9, 1024);
}

void loop() {
  state = digitalRead(btn);

  if ( (millis() - lastDebounceTime) > debounceDelay) {
    if (state != lastState) {
      lastDebounceTime = millis();

      if ( (state == LOW)  ) {
        Serial.println("Button pressed");
        fan_speed++;
        if (fan_speed > 3)
          fan_speed = 0;

        if (fan_speed == 0) {
          Serial.println("Fan = 0%");
          analogWrite(fan, 0);
        }
        else if (fan_speed == 1) {
          Serial.println("Fan = 50%");
          analogWrite(fan, 128);
        }
        else if (fan_speed == 2) {
          Serial.println("Fan = 75%");
          analogWrite(fan, 192);
        }
        else if (fan_speed == 3) {
          Serial.println("Fan = 100%");
          analogWrite(fan, 255);
        }
      }
      lastState = state;
    }
  }


}
void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x07; break;
      default: return;
    }
    TCCR0B = TCCR0B & 0b11111000 | mode;
  }
}

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.

The first thing that comes to my mind is that you did not properly solder all the connections. Rework them all.
The second thing is trimming the excess wire ends after soldering.
The third thing is tightening all the screw terminals and trimming any excess wire strands.

Finally, perhaps your project does not like to have it's lid closed.

I'm surprised that it works at all, if you really are using an IRF530 transistor. That is not a logic level MOSFET.

Thanks for your reply. Luckily I can say that I did solder it properly :slight_smile: and I always cut off the excess wire ends, I like it to look neat. There was nothing touching each other or metal (the metal box is powder coated). The other thing is, that I completely wrapped up the Arduino, so that if if there was a way for it to touch metal, that it would not make any contact. And it also does not move in any way when I close the lid. It has at least 3cm space in all directions from any metal sides.

I also think it does not like a closed lid, maybe it's afraid of the dark :wink:

Did you test AFTER wrapping and before putting box?

Oh really, that is interesting, I did not know about logic level or any difference. I spoke to someone that seemed to know what they were talking about, and they said I should use the IRF530. Which one would be the correct one to use then please?

Yes sir, I tested every change without the lid to see if it works before closing the lid

You should be using a logic level MOSFET. The IRF530 requires 10 volts on the gate to be fully turned on. The IRL series (e.g. IRL540) will work much, much better.

Another example

Thank you very much, I will give that a go :slight_smile: I only find the IRL540N, ist that also ok or is the "N" bad?

Logic Level MOSFETs

https://aws1.discourse-cdn.com/arduino/original/3X/7/2/7253db9f14a32e3568824488ca7d3ba8cae7aea6.pdf


Let’s see the actual wiring.

Your frizzy is far from complete. An annotated schematic would be much better. In the end that would have been much faster solution. No idea of where you have connected the wires. Also 3V probably will not run the fans. I could not determine that because there is no link to technical information on them. If I were to take a SWAG you run it with the box open and connected to your pc, you unhook the pc, close the lid and it quits. Your frizzy does not show a power connection to the Arduino or what pins things are connected to. This would be a one or two question thing with the schematic you did not post. I expect you also burn your fingers on the MOSFET which if installed properly would stay cold.

Sorry, I forgot to add the power to the Arduino. I use a powersupply that has 12v connected to the fans and 3v going to the Arduino. I don't need to remove any cables and the PC was only connected once to apply the sketch to the Arduino.

Still looking for the schematic and TECHNICAL data on the components.

LEt me tell you about an experience with cheap plastic button switches. No I won't. But if you soldered the wires to the switch that I used, then the plastic body allowed the internal contact to move and make only momentary contact.. When I replaced that switch with an identical switch, I used a heat sink on the pin and soldered the wire VERY quickly. Still working. The plastic is cheap soft plastic.

1 Like

Hmmm, I don't have anything more than what I posted in my first message. Sorry, I'm completely new to this world

That is true, I have seen this problem with the platic melting and the pins moving. I have a switch with a plug, so I only solder the cables of the plug to the other cables. But I also tried different switches to see if it will help. I also had a switch where I have 2 screws to attach the cables without soldering

I give up! Post a few pictures.

That is fine.

@BraamLee have you tried temporarily replacing the fan(s) with an LED and series resistor, just to see the logic work and if it still goes wonky when you close the lid?

a7