PDA

View Full Version : Reading a Light Dependent Resistor



kiwipiper
- 20th October 2007, 05:55
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

mackrackit
- 20th October 2007, 16:36
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


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,


IF tone = x? then ???


Hope this helps.

Melanie
- 20th October 2007, 17:15
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.

kiwipiper
- 20th October 2007, 20:48
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

mackrackit
- 21st October 2007, 00:58
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


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.

kiwipiper
- 21st October 2007, 05:29
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