Reading A Photo Resistor using ADC


Closed Thread
Results 1 to 10 of 10
  1. #1
    jessey's Avatar
    jessey Guest

    Default Reading A Photo Resistor using ADC

    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

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    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

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    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)

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    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/E5KC...FO02Y?ALLSTEPS

  7. #7
    Join Date
    Apr 2006
    Posts
    92


    Did you find this post helpful? Yes | No

    Default

    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...ue_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.
    Last edited by TonyA; - 24th January 2007 at 23:38.

  8. #8
    Join Date
    Jun 2005
    Location
    Wisconsin
    Posts
    382


    Did you find this post helpful? Yes | No

    Default

    Bruce felt that a photocell would be to slow to measure frequency. That is why I asked about a solar cell.

  9. #9
    Join Date
    Apr 2006
    Posts
    92


    Did you find this post helpful? Yes | No

    Default

    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.)
    Last edited by TonyA; - 25th January 2007 at 10:25.

  10. #10
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Works great

    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.

    Code:
    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

Similar Threads

  1. Stable Adc Reading Routine
    By gebillpap in forum General
    Replies: 27
    Last Post: - 13th May 2015, 02:18
  2. Reading (ADC) negative current
    By sougata in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 21st November 2006, 17:38
  3. 12F675 ADC 'Issues'
    By harrisondp in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2005, 01:55
  4. Problem reading multiple ADC ports
    By jswayze in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 4th November 2004, 16:46
  5. Reading multiple ADC channels FAST
    By lwindridge in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st April 2004, 22:37

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts