about ambient light sensing

Go To Last Post
23 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

Not sure if this is the right place to ask but if not, please move to the appropriate location. Was working on a project that uses  a phototransistor ambient light sensor to control some led [not homework of any kind].

 

Now, in the scope screenshot I posted below there's a square wave representing led blinking 100 ms on, 100 ms off. The waveform below it represents the response of the ambient light sensor in response to the led blinking (about 30 ms). 

 

Now, I had been checking the ambient light sensor every 1 second with a timer, to see if ambient light is enough for determining whether to have light turn on or not, but then I was suggested to instead check the ambient light sensor every 30 minutes and then do something with the ambient light values (I'm guessing it's to turn on and off the led lights) , but what would would be the use of checking the ambient light sensor value just every 30 minutes? 

 

 

 

 

 

Last Edited: Fri. Mar 16, 2018 - 10:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How do we know what you want to achieve?
If you’re power constrained (eg: battery powered), then you might want to sample the least as possible. If you’re not power constrained, then the more you sample, the better you can determine the actual lighting conditions. With more data, you can filter it and tailor your response time. You can also combine some heuristics like time of day to influence the response.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

30ms   = approx response time of light sensor.

100ms = Blink rate of LED.

1s       =   Checking the light sensor (From the blinking led light input ??? )

30 min = Some vague suggestion for an interval to check the lights ???

 

So you have some measurements, and some ideas.

But what do you want?

Why do you want to check the ambient light level?

 

This might help you (and us) a lot:
http://www.catb.org/~esr/faqs/smart-questions.html

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I used a phototransistor and a difference amplifier as an ambient light sensor for my jumbo 7 segment LED demo kit a few years back.  The output of the difference amp fed an AVR's ADC.  THe 8 bit reading fed a timer running in phase correct PWM mode to dim the display.  Worked really well too.  I'll look around the lab for the thing and see if I can reverse engineer the circuit.

 

In case I never get to it this is a god GOOD read:

 

https://www.vishay.com/docs/8415...

 

Jim

 

 

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Sat. Mar 17, 2018 - 04:37 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that with some trickery you can use one of the leds as a light sensor (perhaps not too sensitive) to measure the lighting.  Might be possible to steal 2 ms every so often to take a light reading. 

 

Of course, to the eye, the leds just look lit up.  l once sent internal data out the power ON/OFF  led....the user just saw a led that showed power is on.  But aiming a photodiode at it allowed you to see an internal temperature bit pattern on a scope (among other things) without opening the system (customer would not allow any extra leds, monitoring jacks, etc to be added to the design).

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

Note that with some trickery you can use one of the leds as a light sensor (perhaps not too sensitive) to measure the lighting.  Might be possible to steal 2 ms every so often to take a light reading. 

 

Of course, to the eye, the leds just look lit up.  l once sent internal data out the power ON/OFF  led....the user just saw a led that showed power is on.  But aiming a photodiode at it allowed you to see an internal temperature bit pattern on a scope (among other things) without opening the system (customer would not allow any extra leds, monitoring jacks, etc to be added to the design).

 

Clever!

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Apparently OP already has a light sensor. Why would he need another.

The quality of a LED as a light sensor varies widely.

Some leds produce up to 1.5V if you shine some light on them, others don't.

LED's are not optimized for this, nor have specs in their datasheetsl. Nice for hacks and home experiments, Not good for a commercial product.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not good for a commercial product

They can actually work pretty well, but you need to design/test thoroughly, not just "toss" one in.

 

In 1988, I tried LEDs as sunlight detectors. They worked so well that the first homemade LED sun photometer I began using on Feb. 5, 1990, is still in use today.

 

Why Use LEDs As Sensors?

Silicon photodiodes are widely available and inexpensive. So why use LEDs as light sensors?

  • LEDs detect a narrow band of wavelengths, which is why I call them spectrally selective photodiodes. A silicon photodiode has a very broad spectral response, about 400nm (violet) to 1,000nm (invisible near-IR), and requires an expensive filter for detecting a specific wavelength.
  • The sensitivity of most LEDs is very stable over time. So are silicon photodiodes — but filters have limited life.
  • LEDs can both emit and detect light. This means an optical data link can be established with only a single LED at each end, since separate transmitting and receiving LEDs aren’t needed.
  • LEDs are even more inexpensive and widely available than photodiodes.

 

 I had no idea those simple experiments would lead to two-way communication over a single optical fiber and several kinds of instruments to measure the atmosphere that I’ve been using for more than 23 years.

 

from Forest Mims, a well-known opto guru:

 

https://makezine.com/projects/make-36-boards/how-to-use-leds-to-detect-light/

 

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I'm glad it worked for him - I had to maintain a product that used a led as an opto detector. It 'worked' most of the time but we found it was temperature sensitive and failed at lower temperatures. Of course, a customer finds this out. As Paul mentions, you're relying on parameters that aren't guaranteed. Had the designer of the recalcitrant fibre optic transceiver used a carrier and modulated the data onto it, then we could've avoided the downside of the parameter spread with temperature we experienced.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

avrcandies wrote:
Not good for a commercial product
With this I meant the lack of documentation and the "not designed nor tested" for that purpose. It is this sort of thing that can send large batches of pcb's to rework and possibly make your profit margin negative.

I had not even thought of the temperature dependency Kartman quotes.

 

"Works for me" != "well designed and reliable product".

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

"Works for me" != "well designed and reliable product".
 

I made the comment since our product went through weeks of extensive & vigorous QA, HALT, and HASS testing* from around -40F to 140F...worked fine to control panel dimming (albeit, not a very critical app).

I still see our units working from 1992 (in rugged outdoor environments from Alaska to Arizona).

 

*One time I came back from lunch & checked the vibration stress tester, where we shook the boards & assemblies for 24/7 for several days.  I was surprised to see that they were testing a blank board. I asked someone about it & they gave me a strange look.  We went back and found nearly every smd component on the floor.  Whoever set up the machine entered something like 150g's rather than 15g's of vibration for the test.  I've had a lot of strange things happen after lunch.

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi,

 

i'm back. actually the 30 milliseconds deal, i think it was just to turn off the light, measure the ambient light for that 30 ms period and then just adjust the brightness at that time.  How would that work out?

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From reading your curves, you ought to wait about 50ms from you turn off the LED before you measure ambient light. 50ms is my guess at where the light sensor curve flattens out enough to give a decent measurement accuracy of the ambient light. With less difference between LED on light level and ambient light you can wait a shorter time before you measure.

You can also wait a shorter time, and estimate the ambient light based on the response curve from the light sensor.

 

/bncache

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi again,

 

ok every 30 minutes, turn off light, read ambient light quickly for like 30 or so ms (I see bncache said 50 ms but anyway), then you adjust the brightness at that time.

 

Now, is there any easy or good way to adjust the brightness without making things too complicated ???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now, is there any easy or good way to adjust the brightness without making things too complicated ???

Use pwm, either with a timer or possibly as part of your software loop 

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi,

 

Ambient light goes down and then the brightness of LED goes up to sort of like compensate for the ambient light sensor value change and it's done every 30 minutes (30 second for testing purpose, but 30 min for final). That's what I was looking at now. I see

 

 

was trying to do something like the map function that arduino has, but i'm not using an arduino and i'm not coding in arduino-c language anyway. Sort of a linear interpolation type of mapping to relate ambient light sensor values to pwm values.

 

So for my 8 bit pwm, 0~255 represent the 0~100% duty cycle [brightness]

 

and then 0 ~ 1023 represents the possible values for the ambient light sensor values (or rather, the result that the ADC spits out to me)

 

So after you get the brightness values, you'd just feed the brightness to the LED and be done with it? (or am I confusing myself some more at this time?)

 

 

thanks

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mc0134 wrote:
you'd just feed the brightness to the LED and be done with it?

 

Assuming the LED was linear, yes, but it's not, but no need to worry about that just yet.

Get it working then you can fool with the brightness curve later.

 

Jim

 

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@ki0bk

 

Yes, I did the following already in a 1 second RTC interrupt (the complete ISR that I currently have, MEASURE ALS, and ADC_WAIT are already defined with #define earlier, MEASURE_ALS measures ALS and ADC_WAIT is to wait until ADC conversion totally finished). Now, back to the question on my prev post. What to do now?

 

The code below allows me to cover the ambient light sensor gradually and the LED brightness increases the opposite way, which looks ok and to make smoother I'd probably want to make RTC trigger at a shorter period but anyway. But then what is this brightness curve we talk about? I know that instead of linear, probably some kind of ln or log curve, and alsoTEPT5700 is the ALS phototransistor sensor that I am using)

 

There's also something about a ratio, guessing that means how many steps to increase the brightness in relation to how many steps the ALS decreases. So I'm thinking 1:4 which is how I was doing the code below. Wish this stuff didn't appear to be as complex as I'm making it to be :(

 

ISR(RTC_CNT_vect)
{

	// Measure the ambient light
	MEASURE_ALS;
	ADC_WAIT;

	// ADC result is the sum of 64 combinations so take average
	result = ADC0_RES / 64;

	// Assuming linear relationship between ALS sensor and PWM control
	// which is definitely not true.
	brightness = ALS_BRIGHT_HI - (( result - ALS_LOW ) / STEP_SIZE );


	if( brightness <= 32)
		brightness++;
	else
	{
		delta = ( brightness / 16 ); //.kbv originally 16
		temp = brightness + delta;
	
		if( temp > 255 )
			brightness = ALS_BRIGHT_LOW;
		else
			brightness += delta;
	}

	setBrightness( brightness );

}
	
	// Clear the overflow interrupt flag
	RTC.INTFLAGS = RTC_OVF_bm;
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Later, for linearisation a PWL (Piece wize linear) function is quite effective, and simple.

 

Here is the map function right from the arduino reference:

https://duckduckgo.com/html?q=arduino+map+sourcecode

https://www.arduino.cc/reference/en/language/functions/math/map/

 

Appendix

For the mathematically inclined, here’s the whole function

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Making the links in this post and copying / pasting actually took longer than finding it.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

Last Edited: Wed. Mar 21, 2018 - 06:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You may only need a few brightness levels, rather than a continuum.   In my old car the panel vfds (vacuum fluorescent displays) were either bright, medium dim or very dim.

You could make as many bins as desired to provide a non-linear effect, with no calculations at all.

I suppose it all depends on how non-noticeable the change must be.  You might be able to fit what you want to a single curve (such as pwm=300/(1+K*sens^2) )

 

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
In my old car the panel vfds (vacuum fluorescent displays) were either bright, medium dim or very dim.

Ha ha: we had a car once that had about a dozen speed settings on the windscreen wipers - I only ever used low, medium, and high!

 

frown

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Basically the entire project's like when there's motion detected by a motion detection sensor, the LED lights should turn on gradually if the ambient light sensor detects that it's dark (not enough light). Then there's a delay time (how long the light stays on before it shuts off) And finally after timer expires and no additional motion anymore, the LED light goes down gradually to a low min brightness and after 30 more minutes of no motion, then the LED light turns off completely, and the program starts from the beginning again.

 

 

During delay time and the 30 min of low brightness, if there's additional motion detected, then retriggerability comes into effect to keep lights on like anybody would expect

 

 

Also, in the period where there is delay time, there should be a 30 min calibration (i.e., the turn off light, measure ambient light, and then turn on light thing and then do the brightness adjustment thing I was asking about earlier ).But I've added the condition that as soon as the ambient light sensor reading reaches LIGHT level (LIGHT level means that the ambient light sensor has detected that there's a lot of light above a certain threshold), the LED light is shut down.

 

I've assumed the LED brightness vs ambient light sensor output (via adc) is linear but it's not so.

 

 

So the questions now is:

 

1. how to figure out the proper ratio to relate LED Brightness to ambient light sensor value? (how much should the led light brightness adjust in response to the ambient light sensor value changes). The phototransistor is TEPT5700, and the LED I have right now is a COB-LED with 6 LED's that looks kind of like the one in the image below (the one I have is a 2010 version) but still looks similar:

 

 

 

 

2. would there be a conflict between ambient light sensor calibration and the fact that I had LED light shut down as soon as the ambient light sensor tells me there's plenty of light and no led light should be on?

 

3. The light brightness before it starts the initial delay is always ramped up to maximum brightness so any subsequent ambient light sensor reading value that indicates that ambient light is decreasing would not cause the LED to get any brighter so in that case, the 30 min calibration would not able to do its job for that situation?

 

Thanks.

 

EDIT: added the image of cob LED

Last Edited: Thu. Mar 22, 2018 - 04:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 the LED lights should turn on gradually

Why--so you can stumbled around in pitch-blackness?  Do you mean 2 minutes or 2 seconds? 

 

 ...calibration (i.e., the turn off light, measure ambient light, and then turn on light thing and then do the brightness adjustment thing I was asking about earlier

How is this calibration??...you say you will measure how dark the room is, then turn on light in some inverse proportion the darkness...the is just normal operation.

 

 

 how to figure out the proper ratio to relate LED Brightness to ambient light sensor value?

That's up to you---how bright do you want the leds for a given ambient (non-led) level?   For example you could theoretically have it such that a bright room gets no led pwr,  a somewhat dim room gets huge led pwr,  a dim room gets almost no led power and a very dim room gets  moderate power.  That would be a rather strange experience as the natural lighting faded to black.

 

If you are able to measure the room brightness while the leds are on (without leds impinging on sensor), then of course you can simply adjust the leds to provide a constant setpoint brightness in the room; the setpoint itself may vary in different modes or timeouts.   

 

  

When in the dark remember-the future looks brighter than ever.