Signal conditioning design for light dependent resistor to ADC?

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

Hi all,

 

Sorry if this is a vague question, but here goes:

 

I am building a toy nightlight with software controlled hysteresis, so I need to get changes in light into the MCU via the ADC.  To this end, I am using a cheap light dependent resistor (LDR) but haven't settled on a good circuit.  Could someone recommend the best approach?

 

My design goals are: 

  • output must be between 0 and 5 V for the Atmega328P ADC
  • output should be close to linear
  • not crazy complicated or expensive

 

My ideas so far are:

  • use a simple voltage divider circuit.  I find it hard to get nice output from this, but maybe it's just not enough analysis on my part (I just fiddled with the breadboard so far).
  • use a simple wheatstone bridge, somehow scaling and biasing to fit into 0-5V.  I like that I can adjust the null setting with a pot, but I now have negative values that need to get biased for the uC.
  • use a complex wheatstone bridge with a linearizer circuit and an amplifier.  An in-amp has an offset adjustment (cool), but this might be overkill and it seems hard.
  • do something completely different (a photodiode? a photo transistor?)

 

Really it is just for my practice with uC programming the ADC, but I still want to do something cool and elegant if possible.

 

Please share your ideas and experience!  Thanks!

 

This topic has a solution.
Last Edited: Mon. Apr 16, 2018 - 04:10 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you looked at the various projects for Arduino that use ldrs?

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

Kartman wrote:
Have you looked at the various projects for Arduino that use ldrs?

 

No, I guess I don't really look at much Arduino stuff because I program the uC directly, but I will check them out for the sensor configurations.  Do you recommend any?

 

thanks!

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

I recommend the simple voltage divider approach. Use Vcc as the ADC reference. Choose the second resistor about equal to the LDR resistance at nominal (not bright) light levels.  

 

Be aware that resistance of LDRs changes pretty slowly. You will probably want to do a minimal amount of digital signal processing, maybe only to the extent of taking 8 or so measurements and average them. 

 

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

forkandwait wrote:

  • output should be close to linear

 

You can do that in your software.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

One way to get a bigger dynamic range could be to charge a cap with a IO and then measure the time it take to discharge through the LDR. 

Last Edited: Fri. Apr 13, 2018 - 08:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LDR's are a bit old school. And if I remember right they do not conform to RoHS (Cadmimum Sulphide ?)

 

"modern" light sensors have a pretty lineair response and are often also compensated for human-eye response.

An example of such a sensor is the LLS05-A (Datasheet attached).

They are mass produced and used for the Day/Night Off/On in PIR lamps.

 

But they may not have penetrated the "hobby" electronics market very far.

If it is for single use, the best way to get one might be to buy a cheap night light in a local store devil

 

 

 

 

Attachment(s): 

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

Last Edited: Fri. Apr 13, 2018 - 09:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

forkandwait wrote:
Really it is just for my practice with uC programming the ADC,

For that particular purpose, I would suggest that you just use a potentiometer (aka "variable resistor"):

 

Bourns PDB181-K420K-502B

https://electronicsclub.info/variableresistors.htm

 

That will give you a much more reliable and easily controllable "signal" to work with.

 

You could investigate the effects of both linear & logarithmic versions - and, as Brian said, practice doing the linearisation in software.

 

Once you've done that (and it doesn't have to take ages), then move onto applying it to an LDR ...

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

then just use a photo diode, or even a normal LED.

 

 

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

Or use a few AVR I/O pins to switch between different value series resistors. One of the pins is Output high to supply Vcc to your sensor, the other's are input / high-impedance.

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

Last Edited: Fri. Apr 13, 2018 - 09:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I find it hard to get nice output from this

You need to describe what you mean by this in a little more detail!

 

I agree with the above, use a simple resistor divider, and select the fixed resistor for the LDR and your light levels, or as cleverly suggested, switch in one of several for different ranges.

 

What does "linear" mean in this case?

Actual energy at a given light frequency, or your eye's perception of the light intensity?

Both are very different, as one's eye isn't "linear" at all!

 

Generally, for tinkering around, one would select the resistor so that one had a pretty broad range in Vout going from totally dark to bright light.

 

It actually takes some interesting equipment to "calibrate" such a system for true linearization, but for tinkering and a night light this ought not be needed.

 

Depending upon the sensor you use you can watch light bulbs "flicker" with the 50/60 Hz power cycle.

You eye can't see it, but the sensor and an O'scope, and your ADC readings can.

 

JC 

 

Edit:

Do you have an O'scope, even a cheap "toy" O'scope?

Last Edited: Fri. Apr 13, 2018 - 12:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

forkandwait wrote:
I find it hard to get nice output from this

Hence the suggestion in #8 to start with a pot - because that will certainly give you a "nice" signal to work with ...

 

It's always important with any project to take things one step at a time - don't try to solve multiple problems all at once!

 

This is especially true when you're "breaking new ground".

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

One easy way to callibrate the hardware is to make an ambient light level where you want your gadget to switch, and then use a resistor divider with a pot and your sensor. Adjust the pot to get some "decent" input for your ADC. Then optionally replace the potentiometer with a resistor, or use a resistor + potentiometer combination for a user adjustable level.

 

A nice touch is to measure the ambient light difference from before and after you turn the light on/off, and add that difference to the hysteresis in your software algorithm.

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

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

Paulvdh wrote:
use a resistor divider with a pot and your sensor. Adjust the pot to get some "decent" input for your ADC. Then optionally replace the potentiometer with a resistor, or use a resistor + potentiometer combination for a user adjustable level.

As, for example, here:

Circuit diagram for light-sensitive alarm

https://electronicsclub.info/p_lightalarm.htm

 

 

Pots intended to be adjusted infrequently are often known as "preset" or "trimmer" pots; eg,

 

 

 Image result for preset" or "trimmer" pots  Image result for preset" or "trimmer" pots

 

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

... and just to illustrate what some have already mentioned about the lack of linearity of LDR sensors, look at this image.

 

 

Don't be concerned about the vertical scale units, just appreciate that the sensors are not linear across the range of light wavelengths. Nor are human eyes.

 

Ross McKenzie ValuSoft Melbourne Australia

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

For a nightlight, I would suspect desired linearity to be with respect to human visible intensity, not wavelength...

David (aka frog_jr)

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I fail to see why an on-off switch needs linearity. For my money you need only the LDR across the ADC input and ground. The internal pull-up will give enough ADC dynamic range across the range of dark to light. Two other ADC inputs with potentiometers: one to set threshold, one to set hysteresis. Job done.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

joeymorin wrote:
I fail to see why an on-off switch needs linearity.
I agree, but OP states "output should be close to linear"...

David (aka frog_jr)

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

joeymorin wrote:
I fail to see why an on-off switch needs linearity. 

But note the stated purpose of the exercise:

forkandwait wrote:
Really it is just for my practice with uC programming the ADC

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

Thanks everyone who answered, I thought the discussion interesting and helpful.  I have decided to keep it simple and follow (mostly) Joey Morin's advice:

 

joeymorin wrote:
I fail to see why an on-off switch needs linearity.

 

Well, really just because I thought it would be cool.  You are right for my dorking around I don't need much.  

 

joeymorin wrote:
]For my money you need only the LDR across the ADC input and ground.  The internal pull-up will give enough ADC dynamic range across the range of dark to light.

 

I thought I would do the voltage divider thing, with the resistor chosen to match the LDR at ambient light.  Do you mean attach the LDR w/o any other resistor?  How would that work?  I can't visualize the circuit with the pullup, I guess (not saying much, really, though).  I will think on this and try it as well.

 

joeymorin wrote:
Two other ADC inputs with potentiometers: one to set threshold, one to set hysteresis. Job done.

 

That makes for a nice addition and challenge.  Thanks for the idea.  I will also:

 

Paulvdh wrote:
A nice touch is to measure the ambient light difference from before and after you turn the light on/off, and add that difference to the hysteresis in your software algorithm.

 

I am assuming this ambient light difference is that due to the nightlight itself being turned off or on, right?

 

Thanks again to everyone!

Last Edited: Mon. Apr 16, 2018 - 04:22 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I thought I would do the voltage divider thing, with the resistor chosen to match the LDR at ambient light.  Do you mean attach the LDR w/o any other resistor?  How would that work?  I can't visualize the circuit with the pullup, I guess (not saying much, really, though).  I will think on this and try it as well.

With the LDR connected across the ADC input and GND, and the pull-up on that pin enabled, that is a voltage divider.  In the darkness, the LDR will have high resistance and the voltage on the ADC pin will be high-ish.  When illuminated, the LDR will have lower resistance and the voltage on the ADC pin will be low-ish.

 

The exact values you'll read will depend on the value of the internal pull-up, and the specs of the LDR.

 

The pull-up will have a value of between 20K and 50K.  The precise value will vary from unit to unit, and from pin to pin, but should be relatively stable over VCC and temperature.  Values will tend towards a mean of 35K.

 

A look on the DigiKey website shows a wide range of CdS cells.  The cheapest has a dark resistance of 2.5M, and an illuminated resistance of 50K-90K (at 10 lux).  Let's say 70K.

 

Working with those values, the dark ADC value will be approximately:

1024 * (2500000 / (2500000 + 35000)) = 1010

and the illuminated ADC value will be approximately:

1024 * (70000 / (70000 + 35000)) = 683

 

While this is only abut one third of the ADC's full range, it should be sufficient for your purposes.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]