Reading a Light Dependent Resistor


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007
    Posts
    26

    Default Reading a Light Dependent Resistor

    Greetings,

    I am trying to get some sensible output from the resistance of a light dependent resistor to my SLCD. The following code give me a character that changes when I change the light however I am not sure what the character means other than it is from the ascii chart.

    DEFINE OSC 4
    PAUSE 2000
    PitchCtrl CON 1 ' pitch control (RCTIME) input
    Scale CON $0073 ' divider
    tone VAR Word ' frequency output
    Main:
    HIGH PitchCtrl ' discharge cap
    PAUSE 1 ' for 1 ms
    RCTIME PitchCtrl, 1, tone ' read the light sensor
    Serout 0, 6, [$fe, $1] ' Clear screen and move cursor to start of first line
    Serout 0, 6, [$1b, $52, 1, tone] ' Output tone to SLCD.
    pause 200
    GOTO Main
    END

    Also how can I know what I am getting and then have an if statement that can do something based on the light reading. See second bit of code.

    if tone < 10 Then
    Serout 0, 6, [$fe, $1] ' Clear screen and move cursor to start of first line
    Serout 0, 6, [$1b, $52, 1, "High"] ' Repeat the character B once.
    Endif

    if tone > 10 Then
    Serout 0, 6, [$fe, $1] ' Clear screen and move cursor to start of first line
    Serout 0, 6, [$1b, $52, 1, "Low"] ' Repeat the character B once.
    Endif

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I know this does not answer your questions, but have you looked into using an ADC for this?

    The photo resistor rigged as half a voltage divider. At an eight bit resolution the ADC will give 0 to 255 as an output.

    Then if the ADC var is 128 your code would say
    Code:
    IF ADCVAR = 128 then ???
    The ADC is more predictable than the RCTIME.

    RCTIME is dependent on the OSC , cap and resistance.
    The output will either be calculated or in your case measured at full light and no light.
    Once you have these values,
    Code:
    IF tone = x? then ???
    Hope this helps.
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default

    Rather than outputing an actual single byte numeric which your LCD (correctly) is interpreting and displaying the ASCII character...

    Serout 0, 6, [$1b, $52, 1, tone]

    Try outputing the value as a Decimal String for example like this...

    Serout 0, 6, [$1b, $52, 1, #tone]

    ...there you will see the numeric value change (and not the ASCII character) with the light.


    Your second question is correct... just remember that in your example if tone=10 then you won't get an output... only if it is LESS than 10 or GREATER than 10.

  4. #4
    Join Date
    Oct 2007
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Getting There

    Thanks Melanie, your suggestion worked just fine all working now and giving values between 2 and 19 dependent on the light level. I expect you can also output the Hex or Binary value to the SLCD. Had a hunt around but couldn't figure how to do this.

    For Dave: After I posted last night did a bit of night time reading and was looking at the ADCIN section of the manual. However wasn't entirely comfortable with what they were talking about. However, you post was encouragement enough for me to give it a try.

    I used the code below (pretty much out of the manual). Then put the light dependent resistor from the Vdd to the RA0/AN0 pin. Before putting the resistor in the reading on the SLCD was all over the place. With the resistor in place it sits at 253 - 254. So I guess I have the right pin. However, If I reduce the light I see no change in the reading.

    I am using a PIC18F4620

    DEFINE ADC_BITS 8 ' Set number of bits in result (8, 10 or 12)
    DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
    DEFINE ADC_SAMPLEUS 50 ' Set sampling

    tone var byte

    TRISA = 255 ' Set PORTA to all input
    ADCON1 = 0 ' PORTA is analog

    :start

    ADCIN 0, tone ' Read channel 0 to tone
    Serout 0, 6, [$fe, $1] ' Clear screen and move cursor to start of first line
    Serout 0, 6, [$1b, $52, 1, #tone] ' Output number value to SLCD.
    pause 500

    goto start

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    ADC is not to bad once you get the feel for it. Bruce has good examples at his site www.rentron.com.

    I do not have the data sheet for the chip you are using so I can not tell you how to set it up but it looks like your close.

    Things I would change are to use binary for ADCON1 and TRISA. For example
    Code:
    ADCON1 = %00010111
    TRISA = %11111111
    In the data sheet there should be a table for ADCON1 telling how the bits are to be set.
    Here is an example of the "old way" of doing it. Sometimes this works better than ADCIN. (more control I think).

    Let us know how it goes.
    Dave
    Always wear safety glasses while programming.

  6. #6
    Join Date
    Oct 2007
    Posts
    26


    Did you find this post helpful? Yes | No

    Default Update

    Yes I can see that ADC is definately the way to go. I had a bit of a go with what you suggested and made some headway however could not get totally predictable results. I think the PIC18F4620 is not the best one for me to use to get a good understanding of what is happening. It's datasheet is 130 pages long and it is a bit daunting for a beginner. For every question I research I seem to end up with another long list of questions.

    I am going to work through the examples on http://www.rentron.com using the PIC's and examples they have there. Hopefully, that will give me a good understanding of what is going on then I will try again with the PIC18F4620. Will keep you posted.

    Cheers Bruce

Similar Threads

  1. 12F629 LDR - Light Dependant Resistor
    By Dennis in forum Schematics
    Replies: 15
    Last Post: - 18th February 2010, 22:33
  2. A/D on 16F767 for light tracking.
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th November 2007, 05:56
  3. Reading A Photo Resistor using ADC
    By jessey in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 25th January 2007, 09:28
  4. Using LEDs as light sensors
    By skimask in forum Code Examples
    Replies: 3
    Last Post: - 30th December 2006, 22:19
  5. Replies: 5
    Last Post: - 2nd March 2006, 09:21

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