Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: skreech on Jan 10, 2015, 07:33 am

Title: PWMming high powered leds smoothly
Post by: skreech on Jan 10, 2015, 07:33 am
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. :-(

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

Here are snippets of what I am doing:
LOG
Code: [Select]

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
Code: [Select]

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!
Title: Re: PWMming high powered leds smoothly
Post by: Grumpy_Mike on Jan 10, 2015, 05:35 pm
Quote
I basically don't know if this is because of the hardware or code.
So you post the code but not the schematic?
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Jan 10, 2015, 06:27 pm
it's just an arduino and some leds connected to a transistor. I have no schematic for that.
Title: Re: PWMming high powered leds smoothly
Post by: Grumpy_Mike on Jan 10, 2015, 09:07 pm
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.
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Jan 11, 2015, 08:03 am
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.
Title: Re: PWMming high powered leds smoothly
Post by: Paul__B on Jan 11, 2015, 08:48 am
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?
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Jan 11, 2015, 09:39 am
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.

Title: Re: PWMming high powered leds smoothly
Post by: Paul__B on Jan 11, 2015, 10:30 am
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 (http://forum.arduino.cc/index.php?topic=288392.msg2018367#msg2018367) use these with PWM.

Simple as that.
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Jan 11, 2015, 11:43 am
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.
Title: Re: PWMming high powered leds smoothly
Post by: Paul__B on Jan 11, 2015, 01:42 pm
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 (http://forum.arduino.cc/index.php?topic=288392.msg2020062#msg2020062).

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.
Title: Re: PWMming high powered leds smoothly
Post by: Grumpy_Mike on Jan 11, 2015, 04:33 pm
Quote
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.
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Jan 11, 2015, 05:36 pm
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.
Title: Re: PWMming high powered leds smoothly
Post by: Grumpy_Mike on Jan 11, 2015, 05:52 pm
Quote
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.
Title: Re: PWMming high powered leds smoothly
Post by: Paul__B on Jan 11, 2015, 09:46 pm
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.
Title: Re: PWMming high powered leds smoothly
Post by: Grumpy_Mike on Jan 11, 2015, 10:09 pm
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.
Title: Re: PWMming high powered leds smoothly
Post by: Headroom on Jan 15, 2015, 11:42 pm
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 (http://www.ledsee.com/index.php/led-accessories/led-driver-9-26v-3507001000ma-with-dim-function-detail) 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!
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Jan 16, 2015, 02:34 am
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.
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Jan 20, 2015, 04:22 pm
By the way I ended up modifying the pwm code as follows and got a very nice non linear dim:
Code: [Select]

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 ).

(http://imageshack.com/a/img661/7841/gY7fkR.jpg)

Very informative guide here(https://diarmuid.ie/blog/post/pwm-exponential-led-fading-on-arduino-or-other-platforms) 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?
Title: Re: PWMming high powered leds smoothly
Post by: Paul__B on Jan 21, 2015, 12:40 pm
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.
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Jan 25, 2015, 02:28 pm
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?

It would indeed.
okay i will try this solution: http://forum.arduino.cc/index.php?topic=288262.0 failing that i will try the manual approach. i just don't like using delays and will need to come up with some code..
Title: Re: PWMming high powered leds smoothly
Post by: Grumpy_Mike on Jan 25, 2015, 06:55 pm
Quote
how would I calculate the value of the resistor required in the shunt circuit?
Take the current you want in your LED and subtract it in the current you get from the constant current supply. This extra current has to go down the resistor with only the LED's forward voltage across it.
So you know the voltage you have on the resistor, you know the current you want it to shunt, then ohms law will tell you the resistor value.
Then work out the power in the resistor, double it for luch and use a resistor of at least that power rating.
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Jan 26, 2015, 06:24 pm
sorry I am missing something. I'll just lay out my thinking so please correct it if you can.

I thought the purpose was to pwm the shunt circuit so the current gets diverted from the leds to the shunt and thereby achieve dimming without interrupting the current output from the constant current driver. is this right? The duty cycle between the circuits gives me the led brightness I want.

If so, then when the current is switched i would want 0 current in the leds and all the current in the shunt.

But if I chose a resistor with a value calculated from the same voltage drop and current as the leds wouldn't the current just get split between these two circuits when the shunt is triggered, because both circuits now have the same resistance? I'm sure I've gone wrong somewhere...
Title: Re: PWMming high powered leds smoothly
Post by: Grumpy_Mike on Jan 26, 2015, 07:56 pm
Quote
I thought the purpose was to pwm the shunt circuit so the current gets diverted from the leds to the shunt and thereby achieve dimming without interrupting the current output from the constant current driver. is this right?
No.

The purpose of the shunt is the current produced by your LED driver is 700mA, your LED actually needs 350mA so the shunt will take that excess current.

However, if the resistor turns out to not drop more voltage than the LED needs it won't work at all.

You are clearly not getting this and it is not a proven design it is just an idea, so I don't think you are not up to trying this. Just forget it and get a constant current drive of the right current value and get one that is designed to be controlled by PWM.
Title: Re: PWMming high powered leds smoothly
Post by: Chagrin on Jan 27, 2015, 09:24 am
so get yourself one of these Constant Current LED drivers (http://www.ledsee.com/index.php/led-accessories/led-driver-9-26v-3507001000ma-with-dim-function-detail) through eBay for example.
That particular chip (PT4115) doesn't do well on the low side when fading it. Tends to shut off at about 20%. I was kinda impressed that OP got his to 2% but then I suppose that was due to burning all that power in the FET.
Title: Re: PWMming high powered leds smoothly
Post by: Magicj on Jan 28, 2015, 02:22 am
Meanwell LDD's are nice and compact and relatively cheap, and they do exactly what you require.
Title: Re: PWMming high powered leds smoothly
Post by: Headroom on Feb 01, 2015, 01:41 pm
That particular chip (PT4115) doesn't do well on the low side when fading it. Tends to shut off at about 20%. I was kinda impressed that OP got his to 2% but then I suppose that was due to burning all that power in the FET.
I don't remeber details as I just used that driver to prove a concept, however the reduction in dimming range for most LED driver chips comes from using a higher PWM frequency. The PT4115 has a dimming range of 5000:1 but only at 100Hz PWM frequency. I drove the PT4115 ( well, actually three of them) from a PCA9685 I2C LED driver that has 16 12-bit PWM outputs where I could change the PWM frequency from 40Hz to 1KHz from my library.

IIRC the normal Arduino PWM frequency is 500Hz so some reduction in dimming range can be expected.
I may have said this before but when trying to control things directly with PWM the standard Arduino boards are not the best boards. The Teensy 3.0 and 3.1 have 10/12 PWM pins with PWM resolution up to 16bit and configurable PWM frequency. The Teensy LC that will be released in March will offer a few less PWM pins but also likely at up to 16bit for about $12.
The Teensy 3.1 uses a ARM cortex M4 32bit MCU and the Teensy LC will use an M0. These boards offer many more things at full Arduino compatibility that you'll be pressed hard to find on any other Arduino board and definitely not at such low cost or size.

Title: Re: PWMming high powered leds smoothly
Post by: Paul__B on Feb 02, 2015, 03:00 am
Rooting around in my bin of eBay acquisitions I came across a couple of 300 mA LED drivers/ power converters (i.e., 85-265V input).

These dirt-cheap units are in a fairly flimsy case; the underneath "lid" flipped off easily for the circuit module (loose inside) to be inspected.  The isolation between primary and secondary of the transformer is somewhat dubious but of particular interest is that the secondary circuit consists of a diode and an electrolytic capacitor.  This means that attempts to use PWM, either by a switch in series with the LED or a switched shunt, are quite clearly not going to work.
Title: Re: PWMming high powered leds smoothly
Post by: skreech on Feb 05, 2015, 12:56 pm
The Teensy 3.0 and 3.1 have 10/12 PWM pins with PWM resolution up to 16bit and configurable PWM frequency. The Teensy LC that will be released in March will offer a few less PWM pins but also likely at up to 16bit for about $12.
The Teensy 3.1 uses a ARM cortex M4 32bit MCU and the Teensy LC will use an M0. These boards offer many more things at full Arduino compatibility that you'll be pressed hard to find on any other Arduino board and definitely not at such low cost or size.
The teensy sounds great but china made unos are still around a quarter of its price. But I'll try the lc when its available. I think the greater pwm resolution is worth it.
Title: Re: PWMming high powered leds smoothly
Post by: pjrc on Mar 14, 2015, 10:45 am
But I'll try the lc when its available.
It's available now.  :)