Sunday, September 23, 2012

Dimming a 12V LED strip with a mosfet and PWM

Dimming a 12V LED strip with an N-channel power mosfet is pretty straightforward. In this experiment I'm using 5 meter of cool white LED strip running at about 800 mA giving 10 Watt.

the basic setup
 An Arduino Duemilanove (my trusty lucky white one) is used to provide pulse with modulation ( PWM ). Digital pin 9 and 10 (also known as OC1A and OC1B) are used. They both use the Timer2 facility of the atmega328p microcontroller which means I can have them output exactly the same PWM signal. Digital 9 is used to drive the mosfet and digital 10 is only used for displaying the unloaded PWM signal on the scope.

Basic PWM setup schematic


PWM on the scope
On the scope you can see the yellow PWM signal driving the mosfet and the blue one that is not connected to any load.

The following Arduino sketch does a simple PWM sweep in 10 steps per second:

 1 void setup()
 2 {
 3   pinMode( 9, OUTPUT);
 4   pinMode(10, OUTPUT);
 5 }
 6 
 7 byte b = 0;
 8 
 9 void loop()
10 {
11   analogWrite(9, b);
12   analogWrite(10, b);
13   delay(100);
14   ++b;
15 }

This gives a PWM signal as shown in the (crude) video below.


Not shown in the video is that this also nicely dims the LED strip.

This is actually good enough for simple dimming, but lets look at it more in detail. More specific lets look at the rise and fall of the PWM signal at the gate of the mosfet.

a detailed look at the voltage at the rising edge at the gate of the mosfet

Whats interesting to see is that it takes about 600ns in total to go from 0V to 5V at the mosfet gate.
The first sharp rise is from 0V across the threshold voltage of the mosfet to the Miller plateau voltage and takes about 100ns. At this point the current through the mosfet is already at the peak but the voltage across the mosfet from drain to source still has to drop from the 12V it starts at. This drop happens across the relatively flat part of the peak in the middle called the Miller plateau region. After this drop the Drain-Source voltage for this particular mosfet will be a bit above 150mV and the remaining 11.85V will be across the LED strip. Finally the voltage at the gate rises to 5V which will slightly lower the on-resistance of the mosfet and the final Drain-Source voltage should be about 100mV.
As mentioned all this takes about 600ns which is not very fast. Looking at the specification of the IRF540 mosfet a rise time of about 35ns should be possible.

I got quite some interesting info concerning mosfet gate drivers from this Texas Instruments paper.

The basic idea to get a faster transition is driving more current in the mosfet gate then the measly 50mA that the digital pin of the Arduino/atmega is capable of providing.

One solution suggested by the paper is to use a Bipolar totem-pole driver. The idea behind that is that by using the very fast operation and large gain of bipolar transistors the gate can be charged and discharged quickly.

adding the bipolar totem-pole mosfet driver 
When the PWM signal is high the NPN transistor is on and the PNP transistor is off. This allows a large current to flow from the 5V power supply and the tank capacitor C into the mosfet gate.

PWM high and NPN on
When the PWM signal is low the PNP transistor is on and the NPN transistor is off allowing for a quick sink of the charge on the mosfet gate into ground.

PWM low and PNP on

For the transistors a BC639 NPN and a BC640 PNP do the job. R is 3.3Ω. RB is 22Ω. RGATE is 4.7Ω. C is a big 1000μF electrolytic.

final setup
improved rise time
A rise time of 44ns using the bipolar totem-pole driver is not bad at all.

Lets take a look at the other side of the wave form.

fall ringing
There is some nasty ringing on the fall side of the pulse. This won't affect the dimming but it's interesting anyway to take a look what is happening.

First thing I checked is RGATE. Increasing the resistance off RGATE does lower the ringing but it also kills the just gained speed improvement.

Then I noticed something weird: if I unrolled part of the LED strip the peaks of the ringing got larger. I then replaced the LED strip by a single LED and surely the ringing is all but gone.

I assume the capacitance/inductance of the 5 meter LED strip and/or all its LEDs cause the ringing.

Adding a shunt resistor across the LED strip lowers the amplitude of the ringing but it requires quite a low ohm resistor which in turn gets very hot when there is 12V across it, and it wastes a bit of energy.

Measurement with the scope shows that the ringing is at about 3.5Mhz. A simple shunt capacitor of 100nF should nicely act as a low-pass filter. If we presume a parasitic resistance of 1Ω, using f = 1/(2*pi*R*C) gives about 1.5Mhz cut-off frequency for the filter.

schematic with CSHUNT added


with 100nF filter
That certainly looks nice. I still don't think it is more then aesthetics though..

 I'd love to hear comments on this! Either here or on G+.

Sunday, June 24, 2012

a LED retrofit

Context


A couple of months ago a friend of me come to me with an interesting problem. He has some old lamps that run on 12V and use small halogen lamps. The problem with these lamps is that they're fragile and are getting more and more rare to get. This also makes that these lamps get more and more expensive.
The idea is to make a LED replacement that fits in the existing housing.

LED


I wanted to get enough light to have descent illumination. About 200 lumen should be good.
I ended up choosing the Osram Golden Dragon + LED LW W5AM-KXLX-5K8L. I first used a 5600K white LED which can handle up to 1A of current and gives 100 lm at 350 mA. I used two of those running them at about 350 mA giving about 200 lm. Running them at 350 mA means that the LED runs relatively cold and nicely within specification. Accidentally it is also the place where the lumen per mA curve has it's peak.


After testing the the 5600K white LEDs we decided that a warmer color temperature would be nicer. The 3500K warm white Osram LCWW5AM-JXKX-4O9Q still gives a nice 82 lm at 350 mA. They cost about 2.5€ a piece.

A simple 110° twist on lens completes the LED. (Intelligent LED solutions FL-70)

Driver


Chosing a LED driver is not easy mostly because there is so much choice.


I ended up choosing the National (Now TI) LM3407 buck LED driver. These are the key reasons for this:

  • easily solder-able eMSOP package should pose no problem hand soldering
  • build-in FET and minimal external parts required
  • 92% efficiency when powering 2 LED from 12V
  • good readable specification with all the calculations clearly provided
  • will work with the existing 22 uH surface mount inductors I have lying around from earlier experiments
After I started working with them I noticed this chip as a very nice surprise feature. It contains an internal 4.5 voltage regulator. The specifications say you're not supposed to use it but I figured drawing ~ 100 uA from it shouldn't be that bad. This means I can add a small dimming circuit that drives the PWM pin of the driver IC and have nice dimming.

This is the basic driver circuit:


It is a quite simple buck driver. The diodes are actually fast skottky diodes.
The 0.56Ω current sense resistor programs the driver for 350 mA.
The 82.5kΩ resistor programs the buck switching frequency at 525 kHz. 
The DIM and VCC pins are connected to the dimming circuit.

Dimming


The dimming is done by means of a 1M linear potentiometer. A simple CMOS 555 based circuit converts the potentiometer setting into a PWM dimming signal. I choose the 555 components to have a 500 Hz PWM frequency and a minimal current usage. One odd thing about the specific 555 I'm using (ST TS555IDT) is that it doesn't have a dot that indicates pin one. One side of the IC is slightly curved which is the only orientation hint.



On/Off


In order to have the circuit use really no power when it is off I use a potentiometer with a build-in switch, like used to be done with radios where you turn then off by completely turning down the volume.



The design


There are some oddness there mostly due to being a single sided design but it works fine.


The etched PCB, with the LM3407 already soldered on:


The fully populated PCB:


Mounted in the base of the case:



The result


It turned out pretty good. It is also very nice that it uses very little (only a few miliwatt) of power when the LED is minimally dimmed and it still gives a nice dimmed illumination.

Fully off:


Fully On:


Half-dimmed:



Almost completely dimmed:



Possible improvements


I think the knob is a little big, I should look into a smaller solution for that.

Monday, April 30, 2012

shipping of electronic parts from USA to europe: UPS vs DHL/FEDEX

When buying electronic parts it sometimes is cheaper to buy in the USA. There are some caveats concerning shipping though.


UPS

While sometimes a bit cheaper (but I usually end up with free shipping because I cluster my buys), there is one very big drawback, and that is import tax.
With UPS you have to pay the import tax in cash to the driver. Because you don't know the exact amount at order time you have to guess how much money you need to have in house and then some more because the driver usually can't give change back.

DHL & FEDEX

With DHL & FEDEX you don't pay the import tax to the driver, you get a separate bill via snail mail, which you can pay via a normal wire transfer. Much better!

Consequences


That being said, this starts to influence my buying choices. For example digikey usually ships with UPS, while mouser allows me to choose FEDEX, which makes mouser my preferred choice for buying parts from the USA.

When buying in Europe it doesn't matter so for farnell and rsonline I don't mind either. If I can buy at either of those I still prefer that but both digikey and mouser have a somewhat larger and different selection.

Others


I basically buy most parts at those 4, and sometimes sparkfun or one of sparkfun's resellers of course. Are there any other shops you recommend ?

Thursday, April 26, 2012

3D printing a LED diffuser directly on PCB

"3D printing a LED diffuser directly on PCB"

Yes, it is possible. Hence this quick blog post.



This lamp is a simple proto PCB with a whole lot (>30) of high brightness 0603 white LEDs soldered on them with a few layers of 3D printed yellow PLA on top of them.

I had to add some DRAM heat-sinks to the back to deal with the massive heat production, especially given that PLA gets liquid at relatively low temperatures. Light production is quite poor, but it is cool for mood lighting.

To be continued.

Sunday, April 1, 2012

3D printing and openSCAD

Yesterday, March 2012, I gave a short talk about 3D printing and openSCAD at Newline^2, the conference at 0x20.

The slides and embedded pictures are available under the Creative Commons Attribution-ShareAlike 3.0.

Click here to get them as PDF.

Saturday, March 10, 2012

Arduino, limited RAM and PROGMEM

And so I was happily adding more and more descent error handling code to a piece of code running on an atmega328p (the Atmel AVR micro-controller also typically used in Arduino). And then the thing started acting up. Now I've had that happen to me before so I immediately thought that it was running out of RAM.

The atmega328p has 32k flash, which is typically plenty, but it has only 2k RAM, which is not very much at all.

I was doing things like:

Serial.println("FAIL invalid argument");

Now, you might be thinking, why does it have to store the "FAIL invalid argument" in RAM?
It has all to do with how braindead C/C++ is/are.

"FAIL invalid argument" is a "const char *" which although it seems read-only it actually isn't (and no "const char const *" isn't either).
Thing is in C it is perfectly legal to cast away any types and just start changing characters in the string.
And the compiler can't know you're not doing that as you could be doing it via some very scary indirect pointer arithmetic to get to the characters in the string.

Bottom line is that every "constant" string in the code is copied to RAM because it could theoretically be used.

Now, on to PROGMEM.

It is a way to use strings directly as contained in the program code, but without the copying to RAM.

Okay, I'll first give the short answer which works fine on arduino 0.22 or newer:

Serial.println(F("FAIL invalid argument"));

This does some casting and other magic in the back and does the right thing.

Just for educational purposes lets take a look how to do it without arduino tricks.

the <avr/pgmspace.h> header contains a useful macro, called PSTR which looks like we could use it just like this:

Serial.println(PSTR("FAIL invalid argument"));

Unfortunately that doesn't work. PSTR returns a PROGMEM pointer and it turns out (even though the atmega has a flat address space) we can't use PROGMEM pointers just like regular pointers.

It will nicely compile and give garbage.

The only way is copying the data from PROGMEM to RAM and then using it.

This could be done byte-by-byte with pgm_read_byte (this is what arduino does internally, take a peek in arduino-1.0/hardware/arduino/cores/arduino/Print.cpp ) or to a buffer in RAM with strcpy_P.

All this is quite awkward and error-prone and makes me long for a nicer language for microcontrollers ;)

Friday, January 20, 2012

arduino mega and multiple hardware serial devices

For a project I use an arduino mega (Atmel AtMega1280 8-bit micro-controller to be precise)  to access multiple other hardware devices via hardware serial.

I'm running the Arduino stack on the hardware, more specific the arduino 1.0 release.

Now the atmega1280 (or atmega2560) has 4 hardware serial devices (called Serial, Serial1, Serial2, Serial3 in arduino) so using multiple serial devices is easy eh?

Not really.

For one use case I have a device connected on Serial1 at 57600 baud.
I want to be able to route serial traffic from that device to the Serial device connected to the PC.

Sounds easy enough, the follow arduino sketch does the job:

void setup(){
  Serial.begin
(57600);
  
Serial1.begin(57600);}
void loop() {
    
// from external device to serial console
    
if (Serial1.available() > 0) {
        
byte incomingByte = Serial1.read();
        
Serial.write(incomingByte);
    
}
    
// from serial console to external device
    
if (Serial.available() > 0) {
        
byte incomingByte = Serial.read();
        
Serial1.write(incomingByte);
  
}
}


Thing is, for short messages this works fine. However for longer messages only one direction is stable, the direction from the external device (Serial1) to the serial port (Serial).

I could run the serial port (Serial) at a slower speed then the device (Serial1). However then the other way around will start to drop bytes!

Let's have a look at HardwareSerial.cpp as provided in the arduino distribution.

It seems to work with an RX and TX buffer for each hardware serial device.

#define SERIAL_BUFFER_SIZE 64

Maybe that buffer is not large enough. The AtMega1280 I'm using has 8K SRAM and thats quite some more then I need. Let's double the buffer.

#define SERIAL_BUFFER_SIZE 128

Surely the behavior is somewhat better, but it is still dropping bytes in one direction.

Let's have a look at the write function.


size_t HardwareSerial::write
(uint8_t c){
  
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
  
  
// If the output buffer is full, there's nothing for it other than to
  // wait for the interrupt handler to empty it a bit
  // ???: return 0 here instead?
  
while (i == _tx_buffer->tail)
    ;
  
  
_tx_buffer->buffer[_tx_buffer->head] = c;
  
_tx_buffer->head = i;
  
  
sbi(*_ucsrb, _udrie);
 
  
return 1;}



Look and behold:

while (i == _tx_buffer->tail)
    ;

it's a busy loop! If Serial1's transfer buffer is full the entire micro-controller will block and this keeps happening all the time.

The solution I introduced is only move data from one serial device to the other when both are ready. One should be readable (available() in arduino jargon) and the other should be writeable.

A writeable function is not available in Arduino so I added the following method to the HardwareSerial code:


bool HardwareSerial::writeable(){
  
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
  
return (i != _tx_buffer->tail);}


The loop from the sketch now looks like this:

void loop() {
    
if (Serial1.available() > 0 && Serial.writeable()) {
        
byte incomingByte = Serial1.read();
        
Serial.write(incomingByte);
    
}
    
if (Serial.available() > 0 && Serial1.writeable()) {
        
byte incomingByte = Serial.read();
        
Serial1.write(incomingByte);
  
}
}


This helps in the sense that it smooths the flow between the serial devices.

No more dropped bytes!