Go Down

Topic: How to use an LED as a simple light sensor? (Read 1 time) previous topic - next topic

SamFen

Hi all,

I would like to make a very simple example of using an LED as a light sensor (using the fact that an LED can work as a very simple photoresistor -- its resistance changes when light shines on it).

My only question is how to hook up the LED. Can I just plug one leg into +5V and the other into an analog pin? Or does something need to be connected to ground as well?

Sorry for the n00b question!  ::)

AlphaBeta

#1
Jan 23, 2009, 01:53 pm Last Edit: Jan 23, 2009, 01:54 pm by AlphaBeta Reason: 1
I have done something a bit similar. I used two I/P pins, and pinMode(both,OUTPUT) and then digitalWrite(HIGH) on one and digitalWrite(LOW) on the other.

Wait... Light sensor... NOT touch.
Quote
Can I just plug one leg into +5V and the other into an analog pin? Or does something need to be connected to ground as well?


I think that is correct, you can ofcourse always try. :)

Grumpy_Mike

Quote
its resistance changes when light shines on it


No, it is a capacitor and the discharge rate depends on the light incident on it. So you have to charge it up first, cathode to + anode to ground. Then change cathode pin to an input and using pluseIn see how long it takes to drop to a zero.

halley

Quote
Then change cathode pin to an input and using pulseIn see how long it takes to drop to a zero.


Ooooo.  I thought it was proposed to fix a bug in pulseIn(), but your example usage of it won't work with the "fixed" version.

See, pulseIn(pin, HIGH) should wait for a LOW-HIGH transition before starting to time or you may get partial pulse width measurements, but the current version just starts timing it anyway if the pin is already HIGH.  If you are timing the discharge of a fast capacitor-like system, waiting for the LOW-HIGH transition first won't work well.  You might need to roll your own custom dischargeIn(pin) function to do it, if they fix pulseIn().

SamFen

Hurmmm, I think I may just go find a real photoresistor. Measuring the discharge rate and stuff seems a little complicated for me while I'm just learning about this...

If I had a regular photoresistor, would I just stick one leg in +5V and the other in an analog pin? Does something need to be connected to ground as well?

Thanks!


SamFen

Ahhh, the concept of a "voltage divider circuit" helps a lot. Now I know why there is always a resistor connected between the input pin and ground in these examples.

Thanks!  :D

Grumpy_Mike


Nick_de

#8
Feb 02, 2009, 10:59 pm Last Edit: Feb 02, 2009, 11:10 pm by Nick_de Reason: 1
is the dimming effect hard to see? Will a 100ohm resistor make much difference compared to a 150 ohm?
I've been changing things around and changing high to low etc but I fail to see a light-dependant effect...
Does anyone have a clue what I could be doing wrong?
(feels stupid failing something so simple...)

Is there a way I can increase the effect so that I can see it?

Grumpy_Mike

Quote
Will a 100ohm resistor make much difference compared to a 150 ohm

No difference at all.

I found that adding:-
Code: [Select]
   _SFR_IO8(0x35) |= 0x10;   // global disable pull up resistors
in the setup() helps a lot.

Nick_de

where would you put it?
what does it mean?
(thx btw  :))

Nick_de

#11
Feb 02, 2009, 11:17 pm Last Edit: Feb 02, 2009, 11:18 pm by Nick_de Reason: 1
oops sorry in setup, right... :-X

still don't see a difference...

Go Up