I have an LED strip that I am trying to turn on with a magnetic switch when a door opens. I want the LEDs to fade on when I open the door and fade off when I close it, which is where the Arduino comes in. I am using PWM for the fading. I can get each component to work separately. The fading LEDs work with the MOSFET without the switch. I tested the switch with serial output on the Arduino and it works fading a single LED without the MOSFET.

I am using:

  • Arduino Uno R3
  • Magnetic Switch
  • N-Channel MOSFET IRLB3034
  • 5 Meter White LED Strip
  • 12VDC 30W Power Supply
  • 10k Ohm Resistors

My setup looks like this: breadboard diagram

Schematic: schematic

Code (if it helps):

const int buttonPin = 2;     // the number of the switch pin
const int ledPin =  9;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the switch status
int brightness = 0;
int fadeAmount = 5;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     

void loop(){
  // read the state of the switch value:
  buttonState = digitalRead(buttonPin);

  // check if the switch is ON.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {     
    // fade LED on:    
    analogWrite(ledPin, brightness);  
    if (brightness < 255) {
      brightness = brightness + fadeAmount;
  else {
    // fade LED off:
    analogWrite(ledPin, brightness); 
    if (brightness > 0) {
      brightness = brightness - fadeAmount;

I have a feeling the problem is with the common ground on the MOSFET and the switch. Any solutions/suggestions are appreciated.

  • are you able to turn on the FET via Pin D9 continuously on? With out PWM.. When D9 is high can you pls measure the voltage across drain and source of the NMOSFET? Check the voltage at D2 when the switch is closed and opened. Measure voltage at D9 when the Output of D9 pin is high. can you share the details of 5 meter LED strip? How are you powering on the Arduino? I have a feeling that issue is with the 5 meter strip LED...
    – Umar
    Jul 16 '15 at 3:30
  • I would expect R2, near the MOSFET, in the order of 100 ohm. You'll drive it hot otherwise. Also change the minimum and maximum tests from 255 resp. 0 to 250 and 5.
    – jippie
    Jul 16 '15 at 5:15
  • You do not say what problem you are having. What does it DO? | LED strip will power up over a voltage range near 12V. ie is probably off at about 9V and on at 12V. So IF voltage on strip is analog due to any filtering it will power on over end part of PWM range. If circuit IS as shown LED strip should modulate on and off at PWM frequency. This SHOULD work. Circuit as shown looks OK and code seems OK so you may6 not be doing what you think you are. Check everything. | And, what does it n fact do? Jul 16 '15 at 7:45
  • @As Jippie says- MOSFET gate resistor can be small 100 Ohms or even 10 Ohms OK. 10k as shown is probably not cause of problems but is too large. Jul 16 '15 at 12:37
  • THat's an AWESOME FET. Data sheet here Jul 16 '15 at 12:41

I think you should keep the R2 resistor, but make it 1k and put before it a pull-down resistor of 10k so that any charges stored in the NMOS Cgate capacitance are discharged, whenever there is High-Z at the port D9. Also never forget the resistor limiting the diode forward current, which is typically 20 mA, but you should check that every time in datasheet. In case of LED strip there are usually internal resistors, but if you are using higher voltage still you need to restrict the current. Could you tell us more about why you are using PWM not a simple output port with an integrator for example and what you exactly observe and measure when you turn-on-off (fade-on-off) the LED?


simulate this circuit – Schematic created using CircuitLab

How to make the diagram a bit smaller?

  • 1
    He's driving a 12V rated strip with internal resistors - no external LED r needed. He's using PWM because he wants a digital solution and not an analog one. Jul 16 '15 at 7:39
  • @lalamer To scale the diagrams on ESE you need to make them wider, I do this by placing a dot/segment/text to the left and/or right so it shrinks the whole diagram, works well.
    – KalleMP
    Jul 16 '15 at 8:40
  • @RussellMcMahon yes you are right, this is a led strip and there is a internal resistor. I have updated my answer. Thank you for committing.
    – lalamer
    Jul 16 '15 at 10:46

There is no need for the 10KOhm. MOSFETs are voltage controlled devices unlike transistors which need controlled current. I think your problem might be in debouncing of switch. You are probably getting some false positives which might be making your software go awry. I recommend having a small debouncing routine. For eg - only if you find 3 successive positives will the switch press be termed a true positive or so.

  • When switch is seen as active it does one action and then delays 30 mS before retesting so he effectively has a 30 mS debounce. Jul 16 '15 at 7:40
  • It is a good practice to have a resistor in series with gate to limit the inrush current. Remember the gate of MOSFET acts like a capacitor and needs to be charged/discharged.
    – Golaž
    Jul 16 '15 at 10:19
  • To achieve this u should connect the gate of the MOSFET to the gnd(in case of NMOS).This will discharge the MOSFET gate capacitance quickly. Also, you should never leave a gate floating as it is susceptible to noise and can switch on causing a lot of damage. Always pull down the NMOS gate.
    – Vinod Karuvat
    Jul 16 '15 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy