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?
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.
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.
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.
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().
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?
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.
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?