Reading photocell with digital pin example

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
joshuabardwell
 
Posts: 93
Joined: Wed Aug 14, 2013 2:09 pm

Reading photocell with digital pin example

Post by joshuabardwell »

In this tutorial ( http://learn.adafruit.com/photocells/using-a-photocell ) an example is shown of reading a resistor's value with a digital pin by wiring it up with a capacitor. Here's the circuit:

Image

1. It looks to me like the photocell and the capacitor are wired in series directly to ground. So does that mean there will be constant current through them, regardless of whether the digital pin is hooked up? I think it must.

2. If there is constant current through the circuit, then how do we know that the capacitor will start in a discharged state when we pull the digital pin low?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Reading photocell with digital pin example

Post by adafruit_support_rick »

joshuabardwell wrote:1. It looks to me like the photocell and the capacitor are wired in series directly to ground. So does that mean there will be constant current through them, regardless of whether the digital pin is hooked up? I think it must.
Yes, there will be. The amount of current flow will depend on the resistance of the photocell, which, in turn, depends on the level of incident light.
joshuabardwell wrote:2. If there is constant current through the circuit, then how do we know that the capacitor will start in a discharged state when we pull the digital pin low?
Pulling the digital pin low is what causes the capacitor to discharge. With the pin low, there is GND on both legs of the cap. When you set the digital pin to input, it goes into a high-impedance state (i.e., high resistance), meaning that it no longer grounds that leg of the cap, and neither does it supply any voltage to it.

As the cap absorbs the current leaking through the photocell, the voltage on the digital pin slowly rises, until it reaches the threshold level the Arduino recognizes as a logic '1'. The counter in the code times how long that takes to happen.

User avatar
joshuabardwell
 
Posts: 93
Joined: Wed Aug 14, 2013 2:09 pm

Re: Reading photocell with digital pin example

Post by joshuabardwell »

Thanks. That was a great explanation!

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Reading photocell with digital pin example

Post by adafruit_support_bill »

So does that mean there will be constant current through them
I think RIck missed the "constant" part of the question. Just to clarify a little bit: Current will flow until the capacitor is fully charged. The rate of flow and time to charge will depend on the "RC Time Constant" which will change depending on the resistance of the photocell: http://www.electronics-tutorials.ws/rc/rc_1.html

User avatar
joshuabardwell
 
Posts: 93
Joined: Wed Aug 14, 2013 2:09 pm

Re: Reading photocell with digital pin example

Post by joshuabardwell »

Sorry--my wording was sloppy. When I said "constant," I meant to convey, "greater than 0 amps at all times," not, "the same amp value at all times."

Maliron
 
Posts: 2
Joined: Thu Mar 06, 2014 7:58 pm

Re: Reading photocell with digital pin example

Post by Maliron »

Forgive me reviving an old post, but I had a question about this method.

With no measured delay in the RCTime function couldn't this reading be skewed by the processor clock? I am very new to Arduino and don't know exactly how the clock works, or how long a tic is, but it seems to me that just letting it freerun like that could leave you vulnerable to a false reading if the processor where slowed down by another process for some reason. For example in a much more complicated circuit.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Reading photocell with digital pin example

Post by adafruit_support_rick »

The system clock is incremented in a interrupt service routine. It will increment accurately, even if the processor is otherwise heavily loaded. An extremely brief skew can be introduced if the clock interrupt is delayed by a higher-priority interrupt, but a program running at non-interrupt level should never see that.

Maliron
 
Posts: 2
Joined: Thu Mar 06, 2014 7:58 pm

Re: Reading photocell with digital pin example

Post by Maliron »

Awesome! Thanks for the fast response and the clarification. I was mainly curious on how accurately clock timing could be trusted, and I guess that sums it up; very accurately!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”