PWMming high powered leds smoothly

Hi - I wasn't sure whether this is the right place to ask this so feel free to tell me I'm wrong. I'm basically trying to dim some high powered leds using an rf520 (these are 3v 1A leds) and and am coming across two problems:

  1. The leds are not fading to black entirely. At around 2% brightness they just switch off. This is pretty low but still noticeable. I'd like to try and achieve a smoother fade to black if possible.
  2. I don't seem to be able to achieve a non-linear dim that looks better to the eye. I have both log and sine wave equations in there and they work a lot better than just straight linear but I can't seem to make the dim curve steeper (i.e. dim more slowly at the start and faster at the end). So my leds look as though they are at full brightness until halfway through the fade. :frowning:

I basically don't know if this is because of the hardware or code.

Here are snippets of what I am doing:
LOG

const int _pwmIntervals=14660;
float _R;
static int _interval=0;
unsigned int _logxfactor=2;
static float _auto_power=0;

if (_interval<=_pwmIntervals){
    _interval++;
    _auto_power=pow(_logxfactor,(_interval/_R))-1;
    analogWrite(led,auto_power); 
}

SINE

while dimming:
  _sinfactor=_sinfactor+(4.712/(twilight_length*3000));
  auto_power=(sin(_sinfactor)*127.5+127.5);
  analogWrite(led,auto_power);

Thanks very much for your help!

I basically don't know if this is because of the hardware or code.

So you post the code but not the schematic?

it's just an arduino and some leds connected to a transistor. I have no schematic for that.

skreech:
it's just an arduino and some leds connected to a transistor. I have no schematic for that.

Then the circuit is wrong,

  1. You have no resistor. So what limits the current? The impedance of the supply?

  2. You don't need a resistor actually you need a constant current supply.

  3. No schematic means you have wired it up wrong anyway.

The LEDs are working so I'm not sure if the circuit is 'wrong'. I'm just not able to exercise as fine a control over pwm as I'd like.

Here is the schematic anyway. I am using an off-the-shelf constant current driver for leds.

LED.png

You mean an IRF520, don't you? Otherwise there would be a problem with no base resistor. Suggest you give us a link for it.

And your constant current driver is definitely only a 2-terminal device?

Yes I meant IRF520. here's the link. http://www.vishay.com/docs/91017/91017.pdf

The driver has 4 leads coming out of it (2 for ac and 2 for dc) so I have updated the schematic a little.

LED.png

skreech:
The driver has 4 leads coming out of it (2 for AC and 2 for DC) so I have updated the schematic a little.

Oh, shivers, no!

I realised that just after I posted, have since been searching for my previous explanation.

You mean a constant current power converter.

You absolutely cannot use these with PWM.

Simple as that.

hmm I have to admit that I don't really understand the post and just the like the poster there, it does seem to work... If you don't mind spelling out the dangers clearly more for me I would appreciate it.

In the meantime - okay. I'll just get a driver that accepts pwm inputs and wire it up like this.

My initial question still remains though, which is how can I improve my control over the pwm but maybe we can save that until I try it with the new driver.

LEDv2.png

skreech:
hmm I have to admit that I don't really understand the post and just the like the poster there, it does seem to work... If you don't mind spelling out the dangers clearly more for me I would appreciate it.

I did in the further post there.

It is pretty straightforward. By definition, a constant current supply is doing its darndest to supply a constant current. If you attempt to fight this by switching that current on and off - called "PWM" - and making it a non-constant current, then you are deliberately asking for trouble and that "trouble" will take the form either of being unable to control it "properly" - or sooner or later something burns.

Also note as in the previous discussions on this topic - plenty of them - that if you use a ballast designed for a 0 to 10V control input, you generally have to feed it with a smoothed voltage if you are using PWM to synthesise that voltage. Unless it specifically says that you can feed it with PWM.

Yes I meant IRF520. here's the link. http://www.vishay.com/docs/91017/91017.pdf

And you will see that this in not a logic level FET, that one requires 10V on the gate to fully switch it on.

thanks - 'fully switch on' means 'let all current flow' right? what do i need to look at on the datasheet to ascertain this particular characteristic of a fet? the gate-source voltage / drain current relationship? If so is it a matter of making sure I supply enough voltage to reach the desired current? In my case I am only looking at 1A.

hat do i need to look at on the datasheet to ascertain this particular characteristic of a fet?

It is the on resistance called Ron - it is specified in ohms and it will have an associated Gate voltage with it. In that data sheet it says 10V, for a logic FET this will say 5V.

But like Paul says the problem it you are trying to PWM a constant current drive by interrupting the current.
You could try shunting the current with the PWM FET, it is a bit of a hack.

shunting.pdf (15.1 KB)

Grumpy_Mike:
You could try shunting the current with the PWM FET, it is a bit of a hack.

Sure is!

If it is a switchmode converter, it would have the same problems.

Paul__B:
If it is a switchmode converter, it would have the same problems.

Well no not quite, the converter will output the same current but the voltage will be lower than turn on voltage for the LEDs. This will prevent the voltage being forced to the maximum when the LEDs are off so there will not be so much variation in the voltage output.

However, this is pure speculation but I think it is worth a try. A lot depends on the way the constant current driver responds to the step change. The thinking is that the step change is smaller and so it might not give as much trouble.

While the arguments are valid, perhaps we can shorten the discussion in order to complete this simple project.

so get yourself one of these Constant Current LED drivers through eBay for example.

Connect GND to the Arduino GND and the DIM pin to a PWM pin on your Arduino.
Connect a 1.5A 12V power supply to the driver. Connect Arduino to USB.
Run proper fading sketch.
Done!

Thanks for this. Yes I came to this conclusion in one of the earlier posts. I have another question regarding this build which i will post in a new thread.

By the way I ended up modifying the pwm code as follows and got a very nice non linear dim:

if (_interval<=_pwmIntervals){
    _interval++;
    _auto_power=pow(_logxfactor,(_interval/_R))-1;
    auto_power=255-(_auto_power*(power_percentage/100));
    analogWrite(led,auto_power); 
}

Here are the results of the code plotted on a graph (I got the idea of doing this somewhere in this forum and would recommend it to anyone implementing their own dimming ).

Very informative guide here(Redirecting…) and also where the above code comes from.

Now I am facing one problem, however, and that is that at very low levels (the first 8 steps) the 'steps' are very conspicuous so I would like to find a way to smoothen that out. Would that mean increasing the resolution of the arduino pwm?

skreech:
Now I am facing one problem, however, and that is that at very low levels (the first 8 steps) the 'steps' are very conspicuous so I would like to find a way to smoothen that out. Would that mean increasing the resolution of the Arduino PWM?

It would indeed.

One - albeit tedious - approach is to resort to software PWM for such values, turning the PWM on for a few ms, then off for some ms, repeating this every 16 ms. Say when your PWM value gets down to 32, you instead set it to 256 and switch it repeatedly on for 2 ms and off for 14 ms. You can then continue to count it down but the intensity will be divided by 8.

Grumpy_Mike:
Well no not quite, the converter will output the same current but the voltage will be lower than turn on voltage for the LEDs. This will prevent the voltage being forced to the maximum when the LEDs are off so there will not be so much variation in the voltage output.

how would I calculate the value of the resistor required in the shunt circuit? I understand the function is to divert current away from the leds but am not sure how to calculate the value that would result in 'lower than turn on voltage' without leading to a short. Does it just need to provide less resistance than the led path?

Paul__B:
It would indeed.

okay i will try this solution: PWM resolution - Programming Questions - Arduino Forum failing that i will try the manual approach. i just don't like using delays and will need to come up with some code..