PWM Not working?

I have a circut with the following wiring and it works for what I have been doing, but I wan't to be able to dim the LED with PWM on the arduino.  Right now the light is running at about 50 for the length on the analogWrite.  There is really no difference between 50 and 255 in my opinion so I think something may be wrong.  When the length drops below 20 the light doesn't even come on.  This is for a bike light project and electronics aren't my strongest area.  I am guessing there is some period of time the MOSFET needs to switch on and off and the arduino's PWM is just too fast for this but I am not sure.  I have access to an oscilloscope so I can test to see if everything is working correctly as well.  I just get meaningless jitter with an inner oblong sine wave when I have attempted to use it, so where would be the best points to attach the scope to get some meaningful data. 
(anywhere  I wrote PMW i meant PWM).
I would like to point out that the LED is Rated for 13V and I am running it on  11.1 V, so no problems there.
Aslo I got it working using a 10k resistor between the gate and ground, the arduino, on PWM mode doesn't sink the current very well.
I have included the correct diagram.

Picture of PWM Not working?
new diagram.PNG
sort by: active | newest | oldest
seandogue2 years ago

An N-Channel Mosfet such as this requires the use of a load resistor on the gate. Without it, the gate charge has no where to go. Because of the relatively high capacitance of the Gate, it will retain an applied voltage even after the voltage is removed via S1, often leading to incorrect operation of the circuit. This is, again because of the intrinsic capacitance of the gate. You have basically charged a battery wihtout giving it a path to leak out.

BTW, do not presume you can simply wire a resistor in series with the gate to the I/O source that's toggling it (as is often shown in datasheets). This works ONLY if the source can sink current when in a logic low state.

Additionally, if you take someones advise and try to directly connect to an Arudino, using it in that configuration puts a burden on the Arduino (if that was ever your intention), which may cause issues downstream. For that reason, the load resistor (or bleed resistor more correctly stated) should be connected between the FET's Gate and logic ground.

Use a value in the range of 10-25K

seedorfj (author)  seandogue2 years ago

Thanks! I had a resistor between the Arduino and the MOSFET which I forgot to include, but not between the gate and ground. I now have two up and running for my 30 Watt RGB LED. You wouldn't happen to know how to switch a common anode LED that needs two different voltages controlled by one microcontroller would you?

1) use two I/O channels, say D0 and D1, each responsible for one part of the led. (after all they use a common ground)

2) idk...more complicated stuff I really don't want to pursue.

seedorfj (author)  seandogue2 years ago

sorry, I meant a common positive. That's why it's more difficult.

A bit, but not all that much. If your source voltage is the same, then peg a bleed resistor to the lead that takes less forward voltage to turn on and use an hex inverter (7404?) to act as logic hi sink (ie, you set the Arduino DIO to hi or true, the octal sets low, and since it's a buffer it sucks up the 10-20mA of current for that led..).

They are cheap and it's a good thing to have a few around, since they (or similar ICs, like quad pack optoisolators, transient supressipors, etc) can go a long way to protecting your digital IO on the arduino or other micro.

that is, connect the bleed resistor to the individual cathode. This is one case where a ground (cathode) connected resistor would be favored over the normal method of placing the bleed from source to anode

BTW, you *could just use the arduino's DI/O ports as sinks (using negative logic) set when you meant to clear, clear when you meant to set) , but it's safer to load an intermediary IC. and easier in terms of thought to make the change using a hardware solution...

-max- seedorfj2 years ago

Interesting: I thought the output of most modern stuff is push-pull, especially a arduino microcontroller! But I guess that is a programmable setting somewhere, as well.

To switch the different dyes in a common anode LED, connect the anode to the positive voltage, as usual, and have separate current-limiting resistors go down to 2 separate MOSFETs, and drive the 2 FETs with 2 different outputs on the arduino, and have your code output things to the 2 outputs.

You can learn how to choose the right resistor for the LED on my video about volts, ohms, and amps, here:

seedorfj (author)  -max-2 years ago

Its a 30 watt led, picking up those resistors isn't quite that easy hahaha.

-max- seedorfj2 years ago

Ahh, then you may need to use a switch mode converter to lower the voltage at the anode relative to ground, to just above the voltage needed by the LED. So that way the MOSFET can freely be switched ON and OFF without possibility of burning the LED out.

They say it is not possible to run LEDs with constant voltage sources. That is false. If you have, say, a well regulated 3.3V supply powering up a power LED, there is no need for a current limiting resistor, if the led voltage at full power is 3.3V or more. I have even not had issues with connecting Cree XML's directly to a fully charged 4.2V 18650 without major issues! Although that is BAD practice, I generally do not overdrive the LEDs for long times like that, even with plentiful heatsinking. Still though, it is not like some black hole will open up in the center of the LED die that destroys the earth if you do that, like some people make it out to be if you attempt to use a constant voltage regulated supply across the LED.

seedorfj (author)  -max-2 years ago

the problem is more that I have red and green blue red needs something like 15 to 19 volts blue and green need 20 to 24. They have a common positive, I have a plan using Diodes and Putting the transistors at the positive end

If that's your circuit, you are in grave danger of destroying your LED if you can turn it on properly.

There is NO way the Arduino PWM is too fast. You can try adding a 100K resistor to ground on the Mosfet gate.

Hopefully this LED needs slightly more than 11 V to work, or you will fry it without some current limiting means.

seedorfj (author)  steveastrouk2 years ago

The led can take up to 13V, the battery is 11.1

No, the LED, when running on full current, has a forward drop of 13V, it isn't a "take" voltage. Next time you run one on 13V, measure the actual diode current.

So you don't want the LED to turn on fully then ? OK, provided you can keep it running on an 11.1 V supply, and it never gets hot, you should be OK. If your battery ever reaches 13V just after it charges or whatever, your LED will blow instantly.

seedorfj (author)  steveastrouk2 years ago

I would love to have it turn on fully, but this project doesn't have the capability to produce the full voltage. The main goal of using the arduino type dimmin is to limmit the current without greatly affecting brightness. You can have an 80% duty cycle and have a negligle brightness change. I have taken these LEDS to there max voltage before strapped on heatsinks with no problems, no resistors.

Steve has a point.


For these simple mosfets you will need a proper gate driver and not just a PWM signal.

The saturation voltage must be raising and falling very fast.

I doubt the PWM output from an arduino is directly suited for this task.

You might want to check the signals with an oscilloscope.

And as mentionend by Steveastrouk the LED must be rated for the voltage you use.

In any case you will need a current limiting resistor or suitable constant current driver for the LED.

IIRC the PWM from the Arduino is around 400Hz, it should work fine.

-max-2 years ago

Does the MOSFET turn the LED on to full brightness when there is 5V to the gate? and turn completly off when the gate is grounded? If not, make sure it is wired correctly, you got the drain/source pins right, and the MOSFET has not been damaged. It is easy to kill a MOSFET with ESD and static electricity, and putting too much voltage to the gate.

Also, you may need a special logic-level MOSFET. THe IRF540 is just a jellybean FET that is designed for like 7V+ to the gate for saturation. Though I am not sure, look at the curves on the datasheet for resistance compared to the gate voltage.


If you want to use the arduino to dimm a 10W LED you'll need a driver that is capable of dimming. This instructable should give you some insight.


If you really want to do it through Arduino then check this video out.

Wired_Mist2 years ago

I've used a TIP120/122 with no problems :)

Wire it up just as you have done here, Source to ground, Drain to the cathode of the Led (That is then tied to the + end of you power source) And the gate to your arduino pin (Via a 100 Ohm resistor to limit the current from the Arduino to 40ma)

Two thoughts...

On pins 5 and 6 I have had problems with PWM (When running on the internal oscillator) Try #3 or 9-11

And are you using a separate power source for the Arduino then the Led? Try hooking up the 11.1V to the Arduino's V-in pin. Sometimes using Multiple Power Sources causes weird things to happen.

Also Steve and Iceng mentioned Limiting the Current to the Led; This Is A Must! Personally, I don't like to use resistors for this much current and would use a LM317 on constant current mode. Assuming a 12V input, the 1.2V drop will set you at 10.8V (Close enough)

iceng2 years ago

You have the wrong mind set for

A real electronic hobbyist does not test to see if it is working correctly

He or more often she tests to find out what is WRONG !!