PDA

View Full Version : Reading A Photo Resistor using ADC



jessey
- 23rd January 2007, 12:22
Hello,

I'm looking to write some code to detect a varying light level using a photo resistor. I've tried using the pot command but it doesn't seem to be suitable for my application. I would like to be able to detect dawn (sun just beginning to rise) and dusk. Using the pot command works somewhat but not with any kind of repeatable accuracy.

I read in the archives about using the ADC to read a photo sensor but I'm not sure how to get started doing that. I won't have any problems to set up and get the ADC working but what I'm not sure about is how to wire the photo resistor to the adc input. Once I get the photo resistor wired properly and see what the photo resistor is inputting to the ADC then I shouldn't have any problems writing the code for that.

Could anyone here suggest a wiring scheme and possible considerations for accomplishing this? Or another method that would be suitable.

Thanks
jessey

malc-c
- 23rd January 2007, 12:34
I used a LDR to detect when the main lights on my fish tank went out so it would turn on the night lights for a set period of time. This used a 12F675, and the LDR simply connected between a pin on the PIC and GND.

As I was simply using it for "logic" (ie is it dark - yes / no) rather than actually measuring the level, the ADC function of the PIC was disabled.

If its any help the project files can be downloaded from my website via the link in my signature

Bruce
- 24th January 2007, 00:04
RCTIME works pretty well for something like this. Here's an example with
simple schematics http://www.rentron.com/Micro-Bot/CDS_IR_Navigation.htm

You could replace the 0.01uF caps with resistors forming a voltage divider
with your CDS photocell then read the output with an A/D pin just as easily.

DynamoBen
- 24th January 2007, 01:44
I have a related question. What if you wanted to sense the frequency that a light was turning on and off? (nothing more than 60hz)

Bruce
- 24th January 2007, 02:00
You would want a sensor with a much faster response time than a CDS
photocell. Check here http://www.taosinc.com/index.asp

They have a lot of nifty optical gadgets.

DynamoBen
- 24th January 2007, 23:23
makezine.com had a post about using a solar cell. Do you think that might work?

You can find the instructable here:
http://www.instructables.com/id/E5KCEJKPCUEUIFO02Y?ALLSTEPS

TonyA
- 25th January 2007, 00:05
Hi,

I do this all the time. I have found that using adcin is best. Use the adc pins of your pic like the PIC 18F452.

TRISA = %11111111 'Set PORTA to all input
ADCON1 = %10000010 'Set PORTA analog and right justify result

adcin 1, photoVar 'read analog input of the photocell wired as a voltage divider input on pin 3 of Pic 18F452: result is stored in variable "photoVar"

use the result stored in the variable "photoVar" to do something

From what I understand, from the literature, adcin is a much better choice if your PIC has built-in analog to digital conversion such as the RA pins of Pic 18F452

This is how you would wire your photocell:
Scroll down the page and see the photocell, which is wired as a voltage divider.
http://www.phidgetsusa.com/tutorials/analogue_sensors.asp

Oh, I also just wanted to say that using a solar cell would be cool but solar cells are more costly than photocells. If you wanted to store energy I would say use a solar cell, but from what I understand about your application a photocell, which costs maybe $1.00 at the most would be very effective.

The Hot wire to +5
the green to the pic pin
the black to ground

I make MIDI controllers that use a photocell to detect changes in light and dark, and in response produce MIDI notes. MIDI notes (messages) need to be very fast, the photocell and ADCIN are very fast. It's all you need for your application.

If you need help let me know.

DynamoBen
- 25th January 2007, 01:20
Bruce felt that a photocell would be to slow to measure frequency. That is why I asked about a solar cell.

TonyA
- 25th January 2007, 01:40
Yeah, I see. I glanced over what Jessey said about measuring the difference between dawn and dusk... I know the photocell with adcin would work great for measuring changes in light, but dawn and dusk, or measuring the frequency of light. I don't know if the photocell could do that.

(Sorry for any confusion caused by my posts in this thread, I was replying to Jessey's original post about using a photocell. I didn't read the replies posted by others.)

jessey
- 25th January 2007, 10:28
Hello Bruce,

Thanks for the info, I was really getting frustrated with the pot command and finding a proper value of a photo resistor to use.I found that the adc works great with just about any photo resistor value.

Also thanks TonyA and your right that the adc does work best for this type of application and it is very fast too, I'm pleased to see. I'm also using an 18f452 (great micro) but not with the ADCin command and it works great.



Check_The_Light_Level:
adcon1=%00001110 ' AN0 analog, all other pins digital
ADCON0 = $41 ' Set A/D to Fosc/8, Channel 0, On
Pauseus 50 ' Wait for channel to setup
ADCON0.2 = 1 ' Start conversion
Pauseus 50 ' Wait for conversion
Lite_Level = ADRESH
ADCON1 = 7 ' shut off (Disable) ADC
RETURN


Once again thanks everyone for getting me kick started on this.

Thanks
jessey