Understanding this photoresistor circuit

I am quite new to Arduino and I wanted to try to make a night light using a photoresistor, and while I understand the coding part of it perfectly I had to look up how to set up the circuit and this one confused me, specifically how the photoresistor itself is wired. I set it up exactly like it did in the diagram and it works just as it should, but I don’t understand why it works. I was under the impression that electricity wants to get to ground as soon as it can, so why would the A0 pin be connected passed the ground connection? So the 5v runs through the photoresistor which then resists the current depending on how much light there is, but then instead of it being set up so that the current flows to the A0 pin and then ground, they have it so that it flows to ground first then the A0 pin. Why would the electricity bother going to the A0 pin if it could just go straight to ground?

The photoresistor and 10K resistor are set up as a voltage divider. Pin A0 is most likely being used as an analog input sensing the voltage level of the voltage divider. With photoresistors the resistance decreases as the light level increases, causing the voltage divider to output a higher voltage, towards +5. The analog circuitry behind pin A0 converts the voltage level to a digital number going from 0 - 255. Somewhere in the sketch this number is compared to several parameters set up to decide when to switch the LED on or off.

In a voltage divider current is always flowing through it unless one of the elements’ resistance goes to infinity (open circuit) at which point the divider output will assume the voltage of the leg with the lesser resistance. If the resistance of both members is equal then the divider will output one half of the applied voltage.

Your assertion that the current flows through the photoresistor then to ground and then eventually to pin A0 is not backed up by your circuit. The current flows through the photoresistor and then divides between the 10K resistor to ground and pin A0.
You may want to study basic circuits a little more to better understand how they work.

The photocell and resistor are in series. But the resistance of A0 to ground is in parallel with the resistor. The resistance from A0 to ground is 100Megaohms (that isn't a made up number, it actuall is 100Mohms per the datasheet), much larger than your 10k resistor, so in essence no current flows into or through A0.

Just remember, path of least resistance.

Ecly:
I am quite new to Arduino and I wanted to try to make a night light using a photoresistor, and while I understand the coding part of it perfectly I had to look up how to set up the circuit and this one confused me, specifically how the photoresistor itself is wired. I set it up exactly like it did in the diagram and it works just as it should, but I don't understand why it works. I was under the impression that electricity wants to get to ground as soon as it can, so why would the A0 pin be connected passed the ground connection? So the 5v runs through the photoresistor which then resists the current depending on how much light there is, but then instead of it being set up so that the current flows to the A0 pin and then ground, they have it so that it flows to ground first then the A0 pin. Why would the electricity bother going to the A0 pin if it could just go straight to ground?

Your understanding of how electricity works is totally wrong.

Virtually no current passes to or from the A0 pin - it is a high-impedance input. It's just measuring the voltage on the pin.
The two resistors in series (the fixed resistor and the light-dependent one) form a voltage divider; the current through the entire resistor could be calculated by applying ohm's law to the total resistance. Then, applying ohm's law to one of the two resistors - using that calculated current - gives you the voltage at the mid-point. This voltage will depend on the resistance of the LDR. So by measuring that voltage, you can figure out what the resistance of the LDR must be, and hence how light/dark it is.

The pin is not connected straight to ground, it's connected through a resistor, which as the name implies "resists" the flow of current.

IMO, you need to review the basics. There are lots of guides around the internet that offer information at varying levels that will help you get a better understanding of how electricity works.

There is a series of good introductory circuit tutorials from the folks at SparkFun. Here's the one on voltage dividers:
https://learn.sparkfun.com/tutorials/voltage-dividers
It specifically shows analysis of a photoresistor circuit about halfway down the page.

adwsystems:
The photocell and resistor are in series. But the resistance of A0 to ground is in parallel with the resistor. The resistance from A0 to ground is 100Megaohms (that isn't a made up number, it actuall is 100Mohms per the datasheet), much larger than your 10k resistor, so in essence no current flows into or through A0.

Just remember, path of least resistance.

At room temperature the input pin resistance is probably more like 10^10 ohms. The datasheet value
is the worst-case for all temperatures and supply voltages. You can assume inputs take no current.

First thing to understand is that voltage doesn't flow. Voltage is more alike hydraulic pressure, and resistors are like constrictions in pipes. Wires are like pipes the let water easily flow [big inner diameter], and resistors are like constrictions in the pipe, and resists the flow [smaller inner diameter]. A battery ( or power source) is like a big tank of water.

The higher the resistance value [e.g. the more ohms], the smaller inner diameter, and thus, the more constriction of the flow. If two resistors are in series, like your PhotoCell and 10k resistor, the current [i.e. the "water"] flows from the 5 volt supply [i.e. water "pressure" from the bottom of the tank], through the wire ["fatter pipe"], into the PhotoCell [a pipe who's inner diameter is dependant on the amount of light striking the face of the PhotoCell], then into the next wire [another fat pipe], then into the 10k resistor [a more narrow pipe, with a fixed inner diameter], then into the wire that runs back to the negative side of the power source [or a pipe lying on the ground with a big opening in it -- i.e. so the water can drain away -- not a perfect analogy, but suffices for the discussion -- the important aspect being that the pipe is open to atmospheric pressure -- the same pressure at the top of the tank that supplies the water].

As water flows through a pipe, a pressure gradient develops along the pipe. If it's just one long pipe with the same inner diameter along the entire pipe, the pressure gradient is going to a linear drop from the voltage at one end of the supply [the pressure at the bottom of the tank] to 0 volts at the other end [i.e. atmospheric pressure -- why atmospheric pressure and not zero pressure -- again imperfect analogy, but this illustrates the *relative* nature of voltage readings. If you put the "negative" probe of a voltmeter, on the negative side of a battery, and the "positive" probe on the positive side of the battery, you will read a positive voltage. If you switch the probes [positive probe on negative side of battery, etc], you will read a negative voltage. "Ground" is merely what you choose it to be. If you call the positive side of the battery "ground", then all the voltages in your circuit will be negative, etc. -- it's something that takes some getting used to.

Here's another "thought experiment" to either confuse you, or make this more clear: If you stack two batteries on top of each other -- say, two D-Cells -- such that the positive terminal of one is touching the negative terminal of the other, and then place the negative probe of your meter, at the point where the two batteries touch, then touch the positive probe to the exposed positive terminal [i.e. the positive terminal that is NOT touching another battery], you will read something like 1.5 volts (or even as much as 1.6V if the batteries are fresh). If you touch the positive probe to the exposed negative terminal, you will read something like negative 1.5 volts [also written as -1.5V]. So, basically, the point where the batteries are touching can be considered "Ground", and the two exposed terminals constitute a positive and negative 1.5v supply [also written as +1.5V/-1.5V supply]. Now, if you take that same battery arrangement, and place the negative probe on the exposed negative terminal, then then the positive probe on the exposed positive terminal, you will read something like +3V [also written 3V -- i.e. without the positive sign, because positive is implied]. And, again, if you touch the negative terminal to the exposed positive terminal and read the exposed negative terminal with the negative probe, you will get a -3V reading. When doing that, you can say that you chose the exposed positive terminal to be your "Ground" -- it's all relative!

Now, back to the PhotoCell circuit: If there's a lot of light shining on the PhotoCell, the resistance is lower [the inner diameter is greater]. And if there is very little light shining on the PhotoCell, the resistance is higher [the inner diameter is smaller]. A larger inner diameter is going to allow more current to flow. And a more constricted diameter will reduce current flow. What happens when you connect a smaller pipe [one with a smaller diameter], to a larger pipe? The pressure gradient is no longer linear, and the smaller pipe limits the current flow in the whole circuit [i.e. along the whole pipe system]. The smallest pipe [i.e. the one with the largest resistance to flow] will cause the greatest pressure drop [will have the greatest pressure (or voltage) across it].

Another way of saying this is: In a series circuit, the resistances add up, and the largest resistance always has the greatest voltage across it [i.e *drops* the greatest voltage]. Notice that the 10k resistor is always going to be 10k. The PhotoCell resistance, on the other hand, varies depending on how much light is shining on it. If the PhotoCell resistance is less than 10k, then the 10k resistor will have the most voltage across it. If the PhotoCell resistance is greater than 10k, then the PhotoCell will have the greatest voltage across it. AND, if the PhotoCell has a resistance of 10k [i.e. is *at 10k*], then it will have the same voltage across it as the 10k resistor -- in that case they will both have the same voltage across them.

Another property of a series circuit is, the voltage is always the same across the whole circuit, no matter what the individual resistances are, in the circuit. It's like how, no matter what kinds of varying diameters are going on in a length of pipe, the same amount water pressure exists across the pipe system, no matter how that system of pipes changes [i.e. you stick a valve in the middle of it, and crank it open and closed, the pressure at the bottom of the tank won't change and thus the pressure across the system won't change). Also, the same amount of current that goes into the pipe system, is what comes out of the pipe system. If the value is turned to more closed, the total amount of current will reduce, BUT same rate of water flow at the beginning of the pipe will still be the same that comes out (only at a lower rate). The PhotoCell is like the valve.

And BTW: The series arrangement of the PhotoCell and the 10k resistor form what is called a Voltage Divider -- I know..shocking!

So, if the voltage across our little circuit [composed of a PhotoCell in series with a 10k resistor], never changes, then what is the implication for those voltages across the PhotoCell and the 10k resistor, as the "valve' is changed? The voltage divides across the resistors [i.e. across the PhotoCell and the 10k resistor]. And those voltages always add up to the voltage across the series circuit [i.e. across the two resistors].

So, if they are both at 10k, then they will both have the same voltage across them. Since the supply voltage is 5V, then each would have 2.5V across them. When more light is shining on the PhotoCell, then the PhotoCell, having lower resistance, will have less voltage across it, the rest of the voltage will be across the 10k resistor, etc.

A0 (on the Arduino) is like a pressure gauge. And like a pressure gauge, it doesn't affect the voltages and currents in the circuit [in simplistic terms -- as you study electronics you will learn about caveats to that statement :wink: ] -- but, it does provide a "reading" of what the voltage is at the point where it is connected. And in the case of your circuit, that point is between the PhotoCell and the 10k resistor.

Since ground, in this case, is chosen to be the negative side of the voltage supply [which is conventional for this kind of circuit (and this kind of system)], then the A0 input is measuring the voltage across the 10k resistor. To get the voltage across the PhotoCell, simply subtract the voltage reading from 5V. Or actually, since the ADC that the A0 pin is connected to [internal to the MCU on the Arduino] will give you a number between 0 and 1023. The number will be relative to the voltage across the 10k resistor. Subtract that number from 1023 to get the number corresponding to the PhotoCell. Or, do a conversion: (5/1023) * A0 reading = voltage across the 10k resistor [approximately -- again, further studies will reveal why]. Then subtract that from 5V to get the voltage across the PhotoCell.

1 Like

Also, a note regarding the direction of flow, in a circuit.

It's really arbitrary. And, there are two conventions [because it's arbitrary]. The important thing is:

Pick a convention and stick to it throughout a discussion/project. It also helps if everyone knows which convention was chosen, and is onboard with consistent compliance with that convention.

The two conventions are:

  • Electron flow -- [From negative to positive] -- typically chosen by Physicists and Technicians
  • Conventional Flow -- [from positive to negative] -- typically chosen by Engineers -- fun fact: this convention was established by Ben Franklin -- 'cuz he thought that was how "juice" flowed. It can be thought of as a flow of "charge" -- Another fun fact: the arrow on a Diode points to the direction of Conventional Flow.

Why is it arbitrary? Because, in terms of designing circuits, it really doesn't matter which way the current flows. Truly, in the world of Physics, where the mechanism is king, it does matter, but, unless you're designing Atom Smashers, why fuss about it?

Not sure what's up with technicians, though -- maybe it's about being contrary, like how the rest of the world drives on the Left side of the road :wink:

I prefer conventional flow, because then the diode symbols make sense -- and negative ground is easier to deal with :wink:

1 Like

Another aside: Battery terminals are labeled "+" for Positive and "-" for Negative. But, that's misleading. If the "+" terminal is Positive relative to the other terminal, then how is that other terminal Negative"? Negative relative to what? It should be called the "Zero" terminal. Or "Reference" terminal. Or, even, the "Ground" terminal. But then, that flies in the face of an arbitrary reference point.

So, the names of the terminals should actually be:

  • Positive in relation to the other terminal
  • Negative in relation to the other terminal

Or for short:

  • Positive terminal
  • Negative terminal

And for even shorter:

  • +
  • -

[see what I did there :smiling_imp: ]