Get PWM frequency of LED backlight
Moderators: adafruit_support_bill, adafruit

Get PWM frequency of LED backlight

by pdknsk on Sat Nov 10, 2012 4:55 pm

LED backlights use PWM for dimming, with a typical frequency of 0.1kHz to 1kHz. What I'd like is to detect the frequency using photo cells, have the arduino calculate the current average frequency over the past second(s), and display it on a simple LCD. The device should be completely self-contained, requiring no computer (other than for programming, obviously).

The main question I have is about the photo cells. I'm not sure if photo cells are fast enough to detect such a rapid change. I have noticed a "rise time" and "fall time" attribute in photo cell specs, ranging from ns to ms. Does this indicate reaction time?

The device would be put in a box, directly facing the LED backlight of a monitor, so there would be little to no interference.

If the project is feasible, I'd appreciate some advice on which arduino to use. Can I use the new arduino micro? Thanks!
pdknsk
 
Posts: 7
Joined: Sat Nov 10, 2012 4:52 pm

Re: Get PWM frequency of LED backlight

by adafruit_support_mike on Sat Nov 10, 2012 7:43 pm

The devices are easily fast enough.

Phototransistors are easiest to use because they work like regular NPN bipolars.. hook a resistor to the collector and you get a light-controlled inverter. Most have rise and fall times between 5 uS and 20 uS, meaning they can detect changes in the 25-100 kHz range.

Photodiodes are a little harder to use, but they make up for it by being faster. The rise and fall times for those tend to be in the 5-50 nS range, meaning they can detect changes in the 10-100 MHz range. You generally use them to drive the base of a fast transistor, but in this case 'fast' includes the cheap-and-plentiful 2N3904.. it's rated for speeds up to 300 MHz.

As for platform suggestions, simple phototransistor and photodiode sensors should work with just about anything, especially for an application where the sensors swing between 'full ON' and 'full OFF'.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10777
Joined: Thu Feb 11, 2010 2:51 pm

Re: Get PWM frequency of LED backlight

by pdknsk on Sat Nov 10, 2012 10:07 pm

How difficult is this to build for someone with not much EE experience, if any. If this can be read up somewhere, I will. I'm ready to invest some time into it. The programming I can manage more easily.
pdknsk
 
Posts: 7
Joined: Sat Nov 10, 2012 4:52 pm

Re: Get PWM frequency of LED backlight

by adafruit_support_mike on Sun Nov 11, 2012 12:44 am

It's pretty easy.. two parts and a wire to one of your Arduino's pins. This application note from Sharp has a good list of basic circuits:

http://physlab.lums.edu.pk/images/1/10/ ... ircuit.pdf

but honestly even that is overkill.

For simplicity's sake, I'd suggest you go with a phototransistor.. that circuit is simpler and should still suit your needs.

A phototransistor is basically a light-controlled switch. When you shine light on it, current can flow through. When you take the light away, the current stops flowing. This one made by Kingbright costs 8c at Mouser:

http://www.mouser.com/ProductDetail/Kin ... NWB8le0%3d

Connect a resistor to that, and you have a light sensor. The sensitivity will depend on the value of the resistor you use.. the device's datasheet assumes you'll use a 1000 ohm (called a '1k') resistor and a 5v supply. If you use higher resistor values (10k, 100k), it will take less light to see the effects of the switch opening. You may need to play around a little to find the value that works best for you, but resistors are cheap too.

Connect one end of the resistor to 5v, the other end to the phototransistor's 'collector' (basically the 'electricity comes in here' pin), and the remaining phototransistor pin to GND. Run a wire from the point where the resistor and phototransistor meet to one of your Arduino's pins, and set that pin to be a digital input.

The signal the pin sees will depend on which device conducts electriticy better.. the phototransistor or the resistor. When the light is off, the phototransistor doesn't conduct at all, so the input pin will 'see' the 5v supply through the resistor. When the light comes on, the phototransistor will conduct much better than the resistor, so the input pin will 'see' GND through that more easily. By the time it reaches your code, the microcontroller will have converted it to a logical HIGH or LOW, and from there it's a coding problem.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10777
Joined: Thu Feb 11, 2010 2:51 pm

Re: Get PWM frequency of LED backlight

by pdknsk on Thu Nov 15, 2012 12:40 am

Thank you very much for your help. I'm purchasing parts shortly.

I've got an additional question. Most of the photo transistors are rated at 5V "emitor-to-collector" voltage, but some at 7V. Is this relevant?

PS. It's amazing that selling for 8c is profitable for Mouser, or anyone involved.
pdknsk
 
Posts: 7
Joined: Sat Nov 10, 2012 4:52 pm

Re: Get PWM frequency of LED backlight

by adafruit_support_mike on Thu Nov 15, 2012 1:08 pm

pdknsk wrote:I've got an additional question. Most of the photo transistors are rated at 5V "emitor-to-collector" voltage, but some at 7V. Is this relevant?

Nah. The datasheet writers just chose different numbers to use as examples. The choice of voltage might say something about the market they want to reach (portable devices, automotive, etc) but the transistors should work either way.

pdknsk wrote:PS. It's amazing that selling for 8c is profitable for Mouser, or anyone involved.

That's Moore's Law in action. We've gotten really good at making transistors over the past 50 years or so, and compared to the ones in a microcontroller, these are huge and easy.

The transistor itself is probably the cheapest part of the whole device. I'd guess the lead frame costs as much as the chip, and that the truly expensive part is the clear epoxy envelope.

The thing that still gets me is the fact that regular transistors (I use 2N3904s and 2N3906s a lot.. general-purpose designs that date back to the mid-1960s) are sometimes cheaper than resistors. Both tend to float in the 1-2c range at Mouser, and depending on the weather and phase of the moon, either one can be out in front.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10777
Joined: Thu Feb 11, 2010 2:51 pm

Re: Get PWM frequency of LED backlight

by pdknsk on Tue Dec 25, 2012 6:17 pm

Alright, I've built this (without LCD for now). It seems to work quite well, for a first draft.

I couldn't make it work with a digital pin, because it didn't flip unless I put the photo transistor very close to a candle. I only had a 10k and 1k resistor, which is apparently not enough to get decent sensitivity. I've put a 100k potentiometer on the shopping list.

So I tried an analog pin instead, and got values between 900 and 1023 from the backlight. I used 1023 as LOW and < 1023 as HIGH, and was able to measure a seemingly accurate frequency from two different monitors. To have a fixed frequency for comparison and calibration I've put another arduino and some LEDs on the shopping list. The internal PWM supposedly runs at 490Hz.

What I've noticed though, is that the arduino can only analogRead() at a rate of about 8000 per second. Plenty for 1kHz, but still somewhat limiting. It seems that digitalRead() doesn't have this limitation.

So to make digitalRead() work automatically I had the idea to use multiple resistors in a row, and then run a connection from each resistor to a different pin on the arduino. Then I'd implement a short calibration phase to figure out which pin returns good results.

This does probably work, but only provides a few steps. So I had another idea, to make this seamless. What if there was sth. like a software controlled resistor. Like a regular resistor basically, but with an additional pin that connects to an arduino pin, which can be written to in order to set the resistance. It turns out such a device was already invented! :D They called it a digital potentiometer.

I've put this on the shopping list.

http://www.analog.com/en/digital-to-ana ... oduct.html

If this doesn't work, I'll just use a normal potentiometer to manually adjust for different monitors.
pdknsk
 
Posts: 7
Joined: Sat Nov 10, 2012 4:52 pm

Re: Get PWM frequency of LED backlight

by adafruit_support_mike on Wed Dec 26, 2012 9:01 pm

pdknsk wrote:Alright, I've built this (without LCD for now). It seems to work quite well, for a first draft.

Yay! Zen high-five!

pdknsk wrote:I couldn't make it work with a digital pin, because it didn't flip unless I put the photo transistor very close to a candle. I only had a 10k and 1k resistor, which is apparently not enough to get decent sensitivity. I've put a 100k potentiometer on the shopping list.

You'll probably want to go up to 1M or so, though doing an analogRead() was a great fallback strategy.

pdknsk wrote:To have a fixed frequency for comparison and calibration I've put another arduino and some LEDs on the shopping list. The internal PWM supposedly runs at 490Hz.

Somewhere around there. You can also use delayMicroseconds() or the internal timers to generate whatever frequency/duty cycle you want.

pdknsk wrote:What I've noticed though, is that the arduino can only analogRead() at a rate of about 8000 per second. Plenty for 1kHz, but still somewhat limiting. It seems that digitalRead() doesn't have this limitation.

Correct.

The analog input is a 'successive approximation' ADC, which means it has an built-in comparator and digital potentiometer. It sets the pot to Aref/2 and compares that to the input, then decides whether it needs to adjust upwards or downwards. The adjustment step size decreases by half with each test, and after 10 tests it's narrowed the range down to Aref/1024. It takes a certain amount of time to adjust the test voltage and do each comparison, and that limits the sampling rate.

You can increase the sampling rate by making the ADC clock faster and dropping to 8-bit accuracy rather than 10-bits, but that involves tweaking internal registers away from the Arduino's default settings.

digitalRead() just involves turning a mosfet on or off, which is as fast as the system clock.

pdknsk wrote:This does probably work, but only provides a few steps. So I had another idea, to make this seamless. What if there was sth. like a software controlled resistor. Like a regular resistor basically, but with an additional pin that connects to an arduino pin, which can be written to in order to set the resistance. It turns out such a device was already invented! :D They called it a digital potentiometer.

Neat idea.

pdknsk wrote:If this doesn't work, I'll just use a normal potentiometer to manually adjust for different monitors.

You can also use a transistor, a capacitor, a couple of resistors, and the Arduino's PWM.

All transistors are voltage-controlled resistors. The term 'transistor' is a contraction of 'transconductance varistor', where 'transconductance' means 'a relationship between input current and output voltage' and 'varistor' means 'variable resistor'. The upshot is that increasing the current through the base decreases the resistance between collector and emitter. Mosfets are truly voltage-controlled resistors, while BJTs are current-controlled devices. Thing is, the voltage across the base-emitter junction rises as the current does, so we treat them as voltage-controlled devices anyway.

To make a PWM-controlled resistance, connect a 100k resistor between a PWM pin on your Arduino and a 1uF capacitor. They make a low-pass filter that averages the PWM down to a DC voltage. From that, connect a 1M resistor to the base of a BJT.

For an NPN phototransistor, an PNP bipolar would be the best load. As you raise or lower the PWM value, the resistance across the transistor will rise and fall as well, and the light sensor will become more or less sensitive.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10777
Joined: Thu Feb 11, 2010 2:51 pm

Re: Get PWM frequency of LED backlight

by pdknsk on Sun Dec 30, 2012 2:54 pm

I've got the 1M potentiometer and was able to get some excellent results. I'm actually shocked by the accuracy. Using tone() for the LED, which is an excellent and seemingly very accurate PWM function, I measured the frequency precise to 1Hz at 10kHz! This is turning out much better than expected.

I measured 200Hz for the backlight. I have no means to verify this is accurate, but it'd be too much of a coincidence to get such an exact value if it wasn't accurate. Using the previous setup with analogRead() and 10K resistor, I had measured ~240Hz, so not so bad a result.
pdknsk
 
Posts: 7
Joined: Sat Nov 10, 2012 4:52 pm

Re: Get PWM frequency of LED backlight

by adafruit_support_mike on Sun Dec 30, 2012 9:21 pm

pdknsk wrote:I've got the 1M potentiometer and was able to get some excellent results.

Glad to hear it's working.

pdknsk wrote:I'm actually shocked by the accuracy. Using tone() for the LED, which is an excellent and seemingly very accurate PWM function, I measured the frequency precise to 1Hz at 10kHz! This is turning out much better than expected.

If you're measuring the signal with the same device that's generating the signal, any errors will be shared and will cancel out. Even so, the results you're seeing should be pretty close to what you'd see from a calibrated source.

pdknsk wrote:I measured 200Hz for the backlight. I have no means to verify this is accurate, but it'd be too much of a coincidence to get such an exact value if it wasn't accurate. Using the previous setup with analogRead() and 10K resistor, I had measured ~240Hz, so not so bad a result.

Try measuring a flourescent lamp. Those flicker with the AC power, and line frequency is a pretty solid 60Hz.

It sounds like you have the sensor side of things working well. Congratulations.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 10777
Joined: Thu Feb 11, 2010 2:51 pm

Re: Get PWM frequency of LED backlight

by pdknsk on Sun Dec 30, 2012 10:59 pm

mstone@yawp.com wrote:If you're measuring the signal with the same device that's generating the signal, any errors will be shared and will cancel out. Even so, the results you're seeing should be pretty close to what you'd see from a calibrated source.

Actually I bought a second arduino specifically for this. Mostly because I wasn't sure I could PWM the LED and measure it in the same loop. It can probably be done, but rather than ponder about it, getting a second device was the much simpler solution.

mstone@yawp.com wrote:Congratulations.

Thanks for your help.
pdknsk
 
Posts: 7
Joined: Sat Nov 10, 2012 4:52 pm

Re: Get PWM frequency of LED backlight

by pdknsk on Sun Dec 30, 2012 11:21 pm

PS. That was before I learned of the tone() function. I wanted to use delayMicroseconds() first.
pdknsk
 
Posts: 7
Joined: Sat Nov 10, 2012 4:52 pm