LED Fading WIth a MOSFET and a Voltage Converter

Hey, everyone! I'm Stanimir from Bulgaria and I'm new to the Arduino world :slight_smile:

I recently got a WeMos D1 R2 Board and I'm trying to fade a led strip with it. My LEDs are white and I have a 12v power supply @ 100W to power them.

I found out how to fade leds that here: starlight: Dimming a 12V LED strip with a mosfet and PWM

I wasn't table to find a MOSFET that will be open at 3.3v (that's what Wemos D1 R2 outputs on its digital output), so I decided to use a voltage converter, which I found here: Bi-Directional MOSFET Voltage Level Converter 3.3V to 5V

Since Wemos has an output pin of 3.3v and 5v I am using them for the voltage converter

I combined both schemes and this is what I came up with:

Explaining the scheme:
At the top I have a power source, whose + is connected directly to the LED Strip. The - passes trough Q1 (MOSFET IRLZ44N - pdf attached), which should control the dimming.
Q1's gate is connected to the output of the voltage converter scheme on the right.

This is my code (the default example for fading):

int ledPin = 6;    // LED connected to digital pin 9

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 10) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 10) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

Attached are the specifications for the two MOSFETs used.

My problem is that after wiring according to the scheme above, my lights are ON all the time. They are not fading. Does anyone see the issue ? Can you help me, please ?

2N7000-D.PDF (92.5 KB)

irlz44n.pdf (107 KB)

This circuit is wired completely wrong.

Q1- The Source and Drain are reversed.
Q2- The Gate is tied to 3.3V and the Source is tied to the D6 output not ground but because the ground of the 12V PS is not tied to the ground of the Wemos you have a floating circuit. The 220Ω resister will then always keep Q1 on.

Here's two versions that I put together very quickly. You may not need the 100Ω Resisters and you will have to dabble with the others. Obviously the resisters that have no connection on the one side will go to your PWM Output.

The top circuit has one big problem and that it's INVERTING so at 100% PWM the LED's will be off and at 0% PWM they will be at full brightness. Also if you were to drive them with a digital out, HIGH would be OFF and LOW would be ON.

The bottom one uses a P-Channel MOSFET to drive the LED's and is NON-INVERTING so 0% PWM = OFF and 100% PWM = FULL ON.

Okay here's a new version with the Wemos Connections to the PS shown.

Oops the last one I put up had the VIN and GND reversed.

This on is right, hopefully

Why do you think the IRLZ44N will not be on enough at 3.3V? There is a 3V curve in the datasheet and according to that, even if you max your power supply (what kind of LED are we talking about?), the MOSFET would dissipate about 3W.
I get an RDS(on) of about 0.04Ohm at 3Vgs and 10A DS. At 10Vgs it is only twice as good, ans since you are using 3.3V, you will even be a little better.
AFAIR, a TO-220 alone can take 2W, so perhaps a heatsink would be a better option?

Just take a look at the Schematic that I drew.

I have purposely set them up on the 12 Volt Rail so that the Gate to Source Voltage will be about 12V.
On the top one when the first FET is turned off and on the bottom one when the FET is turned on.

Now the IRLZ44N will work in this situation if the first FET handles a Gate voltage of 3.3V.
Since he is using a 2N7000 small signal FET this should all work.

Thank you so much for your response.
I am sorry I am replying two days later but for some reason I did not get notified that I had a reply.

I wired everything according to your scheme and it successfully works for turning the lights ON and OFF.

However, fading does not work. I created a single LED to negative and the other end to the 2n7000's gate. The LED fades (although it's almost invisble).

I tried removing the 100OHM resistor and connecting the WeMos pin directly to the 2n7000. That also did nothing.

Do you have an idea why fading is not working ?

DreamWaveBG:
I created a single LED to negative and the other end to the 2n7000's gate. The LED fades (although it's almost invisble).

I tried removing the 100OHM resistor and connecting the WeMos pin directly to the 2n7000. That also did nothing.

I don't quite understand what you mean in the first line above could you post a schematic of how you have wired the entire circuit. It doesn't make sense that one end of the LED would be connected to the gate of the 2N7000.

Also are you setting the output pin that drives the 2N7000 to Analog so that it will do PWM and how are you varying the PWM Duty Cycle.

i.e. analogWrite(ledpin, variable);

I'd imagine that the Wemos is like most of the Arduino's/Clones where the input ADC is 10bit and the Output PWM is 8bit. If so you will need to divide the analog input value by 4 before you send it to your output pin if that is how your varying the PWM.

Thanks for your help and patience!

technogeekca:
I don't quite understand what you mean in the first line above could you post a schematic of how you have wired the entire circuit. It doesn't make sense that one end of the LED would be connected to the gate of the 2N7000.

I wired the circuit using your schematic:

About the LED: I used a LED that is not in the schematic to test if everything works in different parts of the circuit.
For an example, I connected it to GND and D6 on my WeMos board and discovered that pin "6" is not "D6" on the board - it's actually D1. It looks like the Chinese didn't get the labels on the board just right ..
However, using that LED I found out that the default Blink code works on D1 and connected D1 to your circuit. This made the LED strip blink, so I yelled something in the lines of "Eureca!".

Then I uploaded this Fade code to the WeMos, at the same pin:

int ledPin = 6;    // LED connected to digital pin 9

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

I connected that single LED to GND and D1 (which is again, actually 6 from the code) and the single LED fades.
Then I connected the same pin to your circuit, but nothing happens.

To my understanding, while the above code goes up to 255, it should still pass trough the values 0,5,10,15, etc., so I should still see fading, even though some of the values are out of range of the 8-bit PWM.

I can post a video of how everything is wired on the breadboard tonight (in about 9 hours).

What voltage are you running my schematic at.
If it's 5V or less the power FET may not be able to switch on properly.
Are you connecting the ground of the driver circuit to the ground of the Wemos.
Also when using power FET's they usually have a minimum current that they can drive.
If the current is below this it may not switch on.

If you are driving just one LED then remove the IRLZ44N and connect the LED from the Drain of the 2N7000 to V+ and of course put a current limiting Resister in series.
One thing to remember is that with only the one FET the circuit is now non-inverting.
You can calculate the Resister value, LED's have a forward voltage of roughly 1.7 to 3V depending on colour, design etc. so
R = (VSource - VForward) / Amps
If your LED's Foward Voltage is 2V, the PS Voltage is 12V and you want to drive the LED at 15ma then here's what you will get.
(12 - 2) / .015 = 666.666666....... so use a 680 Ohm Resiter which is the nearest common value.

One last note, none of the values go out of the 8 bit range since the Sketch limits them from 0 - 255 which is what 8 bits can contain.

I am running the schematic at 12 volts from a power supply.
WeMos' ground is connected to the ground coming from the power supply.
I am using the single LED just for testing - I am driving a 5meter LED strip.

I made a video of me putting the schematic together. I don't know if you have the time to watch it (its 15 minutes) but in the end you can see that blink works but fade doesn't.

The video is in this link:

Thanks for your time again.

Ok, so I took a look at your video and I can see that the fading is not working.

First thing I would do is change the delay time in the sketch to delay(100) or (150).
This is only so that it's easier to see the fade because it's slower.

First make absolutely sure that the 2N7000 is connected correctly and run this test. Disconnect the gate on the power FET then take an LED and put it in series with the Resister that goes to the 12V and the Drain of the 2N7000. Now test both the blinking and fading sketch to see if they both work. You may have to reduce the Resister a bit if your using more than 1k ohm for this to work. If the blink works but not the fade then it may be related to something like capacitance and the low voltage from the Wemos so try my next suggestions.

MOSFET's are not always the answer in these circuits so next remove the 2N7000 and replace it with a 2N3904, 2N2222 or similar NPN Transistor. Change The Resister that goes from the Wemos to the Base of the Transistor to something like 220, 330 or 470 Ohms and see if that works and please let me know what happens.

Hello,
It finally worked and here's the result:

I tried your first suggestion, disconnected the power MOSFET and connected a LED in serial. It was fading, but I noticed it wasn't full brightness.
I connected a multimeter to the source and drain of the small MOSFET and discovered that for analog values 0 to 65 (or 255), the voltage between them was changing from 8.7 to 9.7, but for digital output it was 0 do 9.7 (when blinking)

So I started playing with the analog value. It turns out that in order to be able to fade from 0 to 9.7 the values for analogWrite have to be -1000 to 0 .. and the lower it gets, the higher the voltage is at the 2n7000

I currently have no explanation aside from the very cheap Chinese WeMos board .. maybe there's something wrong with it.

Anyway, it works.
Thank you so much, now I can do the lighting in my room :slight_smile:
Is there any way I can buy you a beer?

Great to hear that its working.

Only other thing I can think of is that the Wemos does 10 or 12bit PWM.

That would then be 0 - 1023 or 0 to 4095 but I don't know of any Arduino's or clones that do more than 8bit PWM.